通过 RUN 执行指令

RUN 主要用于在Image里执行指令,比如安装软件,下载文件等。

  1. $ apt-get update
  2. $ apt-get install wget
  3. $ wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.1/ipinfo_2.0.1_linux_amd64.tar.gz
  4. $ tar zxf ipinfo_2.0.1_linux_amd64.tar.gz
  5. $ mv ipinfo_2.0.1_linux_amd64 /usr/bin/ipinfo
  6. $ rm -rf ipinfo_2.0.1_linux_amd64.tar.gz

Dockerfile

  1. FROM ubuntu:20.04
  2. RUN apt-get update
  3. RUN apt-get install -y wget
  4. RUN wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.1/ipinfo_2.0.1_linux_amd64.tar.gz
  5. RUN tar zxf ipinfo_2.0.1_linux_amd64.tar.gz
  6. RUN mv ipinfo_2.0.1_linux_amd64 /usr/bin/ipinfo
  7. RUN rm -rf ipinfo_2.0.1_linux_amd64.tar.gz

镜像的大小和分层

  1. $ docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. ipinfo latest 97bb429363fb 4 minutes ago 138MB
  4. ubuntu 21.04 478aa0080b60 4 days ago 74.1MB
  5. $ docker image history 97b
  6. IMAGE CREATED CREATED BY SIZE COMMENT
  7. 97bb429363fb 4 minutes ago RUN /bin/sh -c rm -rf ipinfo_2.0.1_linux_amd 0B buildkit.dockerfile.v0
  8. <missing> 4 minutes ago RUN /bin/sh -c mv ipinfo_2.0.1_linux_amd64 /… 9.36MB buildkit.dockerfile.v0
  9. <missing> 4 minutes ago RUN /bin/sh -c tar zxf ipinfo_2.0.1_linux_am 9.36MB buildkit.dockerfile.v0
  10. <missing> 4 minutes ago RUN /bin/sh -c wget https://github.com/ipinf… 4.85MB buildkit.dockerfile.v0
  11. <missing> 4 minutes ago RUN /bin/sh -c apt-get install -y wget # bui… 7.58MB buildkit.dockerfile.v0
  12. <missing> 4 minutes ago RUN /bin/sh -c apt-get update # buildkit 33MB buildkit.dockerfile.v0
  13. <missing> 4 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
  14. <missing> 4 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
  15. <missing> 4 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
  16. <missing> 4 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
  17. <missing> 4 days ago /bin/sh -c #(nop) ADD file:d6b6ba642344138dc… 74.1MB

每一行的RUN命令都会产生一层image layer, 导致镜像的臃肿。

改进版Dockerfile

  1. FROM ubuntu:20.04
  2. RUN apt-get update && \
  3. apt-get install -y wget && \
  4. wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.1/ipinfo_2.0.1_linux_amd64.tar.gz && \
  5. tar zxf ipinfo_2.0.1_linux_amd64.tar.gz && \
  6. mv ipinfo_2.0.1_linux_amd64 /usr/bin/ipinfo && \
  7. rm -rf ipinfo_2.0.1_linux_amd64.tar.gz
  1. $ docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. ipinfo-new latest fe551bc26b92 5 seconds ago 124MB
  4. ipinfo latest 97bb429363fb 16 minutes ago 138MB
  5. ubuntu 21.04 478aa0080b60 4 days ago 74.1MB
  6. $ docker image history fe5
  7. IMAGE CREATED CREATED BY SIZE COMMENT
  8. fe551bc26b92 16 seconds ago RUN /bin/sh -c apt-get update && apt-get 49.9MB buildkit.dockerfile.v0
  9. <missing> 4 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
  10. <missing> 4 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
  11. <missing> 4 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
  12. <missing> 4 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
  13. <missing> 4 days ago /bin/sh -c #(nop) ADD file:d6b6ba642344138dc… 74.1MB
  14. $