017、RUN、CMD、ENTRYPOINT (2019-01-08 週二)

三角形發表於2019-01-10
 
RUN CMD ENTRYPOINT 這三個Dockerfile指令看上去很類似,很容易混淆。
 
簡單的說:
 
    RUN 執行命令並建立新的映象層,經常用於安裝軟體
    CMD 設定容器啟動後預設執行的命令機器引數,但CMD能夠被 docker run 後面跟的命令列引數替換
    ENTRYPOINT 配置容器啟動時執行的命令
 
shell 和exex 格式
 
有shell和exec兩種方式可以指定 RUN CMD ENTRYPOINT 要執行的命令,兩者在使用上有細微的區別
 
shell 格式:
<instruction> <command>
 
例如:
 
RUN apt-get install -y python3
CMD echo "Hello world"
ENTRYPOINT echo "Hello world"
 
當指令執行時,shell格式底層會呼叫 /bin/sh -c <command>
 
root@docker-lab:~/017# cat Dockerfile
FROM busybox
ENV name www1707
ENTRYPOINT echo "Hello $name"
root@docker-lab:~/017# docker build -t test017 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
---> 3a093384ac30
Step 2/3 : ENV name www1707
---> Running in e43df73f1151
Removing intermediate container e43df73f1151
---> 973f9b8b29b7
Step 3/3 : ENTRYPOINT echo "Hello $name"
---> Running in 5efc3b5b3053
Removing intermediate container 5efc3b5b3053
---> c9321b834dbb
Successfully built c9321b834dbb
Successfully tagged test017:latest
root@docker-lab:~/017# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test017             latest              c9321b834dbb        6 seconds ago       1.2MB
myimage             latest              c67728621bdd        33 hours ago        1.2MB
busybox             latest              3a093384ac30        7 days ago          1.2MB
root@docker-lab:~/017# docker run test017
Hello www1707
 
exec格式:
<instruction> ["executable","param1","param2",...]
 
例如:
 
RUN ["apt-get","install","python3"]
CMD ["/bin/echo","Hello world"]
ENTRYPOINT ["/bin/echo","Hello world"]
 
當指令執行時,會直接呼叫command,不會被shell解析
 
root@docker-lab:~/017# cat Dockerfile
FROM busybox
ENV name www1707
ENTRYPOINT ["/bin/echo","Hello,$name"]
root@docker-lab:~/017# docker build -t test-017 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
---> 3a093384ac30
Step 2/3 : ENV name www1707
---> Running in 6a2a8dcbd2ab
Removing intermediate container 6a2a8dcbd2ab
---> 026f3f3c154e
Step 3/3 : ENTRYPOINT ["/bin/echo","Hello,$name"]
---> Running in 92c90db5bbc3
Removing intermediate container 92c90db5bbc3
---> 1f01346313b5
Successfully built 1f01346313b5
Successfully tagged test-017:latest
root@docker-lab:~/017# docker run test-017
Hello,$name
 
 
root@docker-lab:~/017# cat Dockerfile
FROM busybox
ENV name www1707
ENTRYPOINT ["/bin/sh","-c","echo Hello,$name"]
root@docker-lab:~/017# docker build -t test-017 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
---> 3a093384ac30
Step 2/3 : ENV name www1707
---> Using cache
---> 026f3f3c154e
Step 3/3 : ENTRYPOINT ["/bin/sh","-c","echo Hello,$name"]
---> Running in 459792a5b117
Removing intermediate container 459792a5b117
---> d163360f72c5
Successfully built d163360f72c5
Successfully tagged test-017:latest
root@docker-lab:~/017# docker run test-017
Hello,www1707
 
 
CMD 和 ENTYRPOINT 推薦使用exec格式,因為指令的可讀性更強,更容易理解。
 
RUN
 
    RUN 使用兩種格式都可以,但是RUN一般用來安裝應用和軟體包,推薦shell格式
    RUN apt-get update && apt-get install -y vim wget 
    先執行update 保證後面安裝的軟體版本是最新的,要不然會可能會安裝一個早期的版本或者直接安裝失敗
 
CMD
 
    CMD支援三種格式
 
    1、exec格式  CMD ["executable","param1","param2"]
        這是CMD推薦的格式
    2、CMD ["param1","param2"] 
        這是為ENTRYPOINT  提供額外引數時的格式,此時 ENTRYPOINT 必須是exec格式
    3、CMD command param1 param2
        這是shell格式
 
    第二種格式 CMD ["param1","param2"] 要與 exec格式的 ENTRYPOINT 指令配合使用,其使用者是為ENTRYPOINT設定預設引數。
 
    當docker run 後面有引數時,CMD指令將會被忽略
 
root@docker-lab:~/017# cat Dockerfile
FROM busybox
CMD echo "Hello world"
root@docker-lab:~/017# docker build -t test-017 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM busybox
---> 3a093384ac30
Step 2/2 : CMD echo "Hello world"
---> Running in 15bd85e76424
Removing intermediate container 15bd85e76424
---> 13e99c60b00d
Successfully built 13e99c60b00d
Successfully tagged test-017:latest
root@docker-lab:~/017# docker run test-017
Hello world
root@docker-lab:~/017# docker run -it test-017
Hello world
root@docker-lab:~/017# docker run -it test-017 /bin/sh
/ #
/ # exit
root@docker-lab:~/017#
 
 
ENTRYPOINT
 
    該指令可以讓容器以應用程式或者服務執行。ENTYRPOINT和CMD看上去很像,他們都可以指定要執行的命令及引數。但是ENTYRPOINT不會被忽略,一定會被執行,即使docker run 後面指定了其他命令。
 
    exec格式--推薦格式
        ENTYRPOINT ["executable","param1","param2"]
 
root@docker-lab:~/017# cat Dockerfile
FROM busybox
ENTRYPOINT ["/bin/echo","Hello"]
CMD ["world"]
root@docker-lab:~/017# docker build -t test-017 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
---> 3a093384ac30
Step 2/3 : ENTRYPOINT ["/bin/echo","Hello"]
---> Running in 5fca55e81dd9
Removing intermediate container 5fca55e81dd9
---> 60978b547d5d
Step 3/3 : CMD ["world"]
---> Running in b1f9da2b8ecf
Removing intermediate container b1f9da2b8ecf
---> 1957f19e2cb7
Successfully built 1957f19e2cb7
Successfully tagged test-017:latest
root@docker-lab:~/017# docker run test-017
Hello world
root@docker-lab:~/017# docker run -it test-017 www1707
Hello www1707
 
 
    shell格式--該格式會忽略掉CMD和docker run 後面的引數
        ENTRYPOINT command param1 param2
 
root@docker-lab:~/017# cat Dockerfile
FROM busybox
ENTRYPOINT echo hello
CMD ["world"]
root@docker-lab:~/017# docker build -t test-017 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
---> 3a093384ac30
Step 2/3 : ENTRYPOINT echo hello
---> Running in 683c7c95a0bb
Removing intermediate container 683c7c95a0bb
---> 4b76c9e22319
Step 3/3 : CMD ["world"]
---> Running in 3464e96183d6
Removing intermediate container 3464e96183d6
---> d636aed0e6b4
Successfully built d636aed0e6b4
Successfully tagged test-017:latest
root@docker-lab:~/017# docker run test-017
hello
root@docker-lab:~/017# docker run -it test-017 www1707
hello
 
 
 
 
最佳實踐
 
1、RUN 指定用來安裝應用和軟體包
2、ENTRYPOINT用來執行應用或者服務,可以用CMD提供額外的引數,還可以用docker run提供引數或者是替換掉CMD提供的引數
3、CMD可以為容器設定預設的啟動命令,也可以用docker run替換掉
 
 

相關文章