寫在前面
本文一起看下Dockerfile中經(jīng)常用到的幾個類似命令,RUN,CMD,ENTRYPOINT。
1:容器是怎么來的?
想要有容器我們就必須先創(chuàng)建鏡像,而想要有鏡像,我們則必須寫一個用來描述想要創(chuàng)建的鏡像的文件,這個文件我們一般叫做Dockerfile(非強制)
,則容器怎么來的就如下圖:
經(jīng)過上圖的過程1
我們就有了鏡像,經(jīng)過過程2
我們就有了容器了,知道了這個過程,我們就可以來正式開始分析RUN,CMD,ENTRYPOINT命令了。
2:RUN
RUN命令是在上圖過程1中執(zhí)行的,如下的Dockerfile:
FROM busybox
WORKDIR /var
RUN pwd
則執(zhí)行docker build時就會輸出pwd
的結(jié)果:
dongyunqi@dongyunqi-virtual-machine:~$ docker build -t test-push-dockerhub:0.1 -f howPwd.txt helloworld-app/
Sending build context to Docker daemon 8.738kB
Step 1/3 : FROM busybox
---> 827365c7baf1
Step 2/3 : WORKDIR /var
/var # 這里就是pwd的輸出,其實輸出的就是工作目錄,不過輸出的到底是什么此時不重要,我們主要是驗證pwd在docker build時執(zhí)行了
Step 3/3 : RUN pwd
---> Using cache
---> 7975c01019bd
Successfully built 7975c01019bd
3:CMD
在容器啟動時執(zhí)行的命令,一般用來啟動應(yīng)用等,如下Dockerfile:
dongyunqi@dongyunqi-virtual-machine:~/test$ cat testEntrypoint.txt
FROM busybox
CMD echo "hello world"
構(gòu)建:
dongyunqi@dongyunqi-virtual-machine:~/test$ docker build -t test-entrypoint:v0.1 -f testEntrypoint.txt .
...
運行容器:
dongyunqi@dongyunqi-virtual-machine:~/test$ docker run --rm test-entrypoint:v0.1
hello world
可以看到正常輸出了。另外如果是有多個CMD只會保留最后一個,如下:
FROM busybox
CMD echo "hello world"
CMD echo "hello world1111"
測試:
dongyunqi@dongyunqi-virtual-machine:~/test$ docker build -t test_entrypoint:v0.1 -f testEntrypoint.txt .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
---> 827365c7baf1
Step 2/3 : CMD echo "hello world"
---> Using cache
---> c596d2e7bfb5
Step 3/3 : CMD echo "hello world1111"
---> Running in 9369670af208
Removing intermediate container 9369670af208
---> d7d08688d050
Successfully built d7d08688d050
Successfully tagged test_entrypoint:v0.1
dongyunqi@dongyunqi-virtual-machine:~/test$ docker run --rm test_entrypoint:v0.1
hello world1111
dongyunqi@dongyunqi-virtual-machine:~/test$
可以看到只輸出了hello world1111
。以上的是shell格式,我們也可以用Exec 格式,如下:
dongyunqi@dongyunqi-virtual-machine:~/test$ cat testEntrypoint.txt
FROM busybox
CMD ["/bin/echo", "Hello world222"]
測試:
dongyunqi@dongyunqi-virtual-machine:~/test$ docker build -t test_entrypoint:v0.1 -f testEntrypoint.txt .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM busybox
---> 827365c7baf1
Step 2/2 : CMD ["/bin/echo", "Hello world222"]
---> Running in d69c8fdbb1a4
Removing intermediate container d69c8fdbb1a4
---> 5e0920f3e988
Successfully built 5e0920f3e988
Successfully tagged test_entrypoint:v0.1
dongyunqi@dongyunqi-virtual-machine:~/test$ docker run --rm test_entrypoint:v0.1
Hello world222
可以看到正常輸出了Hello world222
。但是如果是在docker run時指定了啟動命令,則會覆蓋CMD,如下:
dongyunqi@dongyunqi-virtual-machine:~/test$ docker run --rm test_entrypoint:v0.1 /bin/echo "hey man!"
hey man!
4:ENTRYPOINT
ENTRYPOINT在Dockerfile中的表現(xiàn)和CMD是一樣的,但是ENTRYPOINT不會被docker run中指定的命令覆蓋,但是也分為兩種情況,如果是使用shell格式的話則docker run中指定的命令將會被忽略,如下:
dongyunqi@dongyunqi-virtual-machine:~/test$ cat testEntrypoint.txt
FROM busybox
ENTRYPOINT echo "Hello world44444"
dongyunqi@dongyunqi-virtual-machine:~/test$ docker build -t test_entrypoint:v0.1 -f testEntrypoint.txt .
Sending build context to Docker daemon 2.048kB
...
dongyunqi@dongyunqi-virtual-machine:~/test$ docker run --rm test_entrypoint:v0.1 11111111111/bin/echo "hey man!"
Hello world44444
如果是Exec格式的話則會被當(dāng)做參數(shù)追加到后面,如下:
dongyunqi@dongyunqi-virtual-machine:~/test$ cat testEntrypoint.txt
FROM busybox
ENTRYPOINT ["echo", "Hello world555"]
dongyunqi@dongyunqi-virtual-machine:~/test$ docker build -t test_entrypoint:v0.1 -f testEntrypoint.txt .
Sending build context to Docker daemon 2.048kB
...
dongyunqi@dongyunqi-virtual-machine:~/test$ docker run --rm test_entrypoint:v0.1 11111111111/bin/echo "hey man!"
Hello world555 11111111111/bin/echo hey man!
相當(dāng)于是在Dockerfile中寫的是ENTRYPOINT ["echo", "Hello world555", "11111111111/bin/echo", "hey man!"]
。
4:比較
RUN:在docker build生成鏡像時執(zhí)行。
CMD:在docker run時執(zhí)行,但其會被docker run中指定的命令覆蓋
ENTRYPOINT:在docker run時執(zhí)行,但不會被docker run中指定的命令覆蓋,如果是ENTRYPOINT使用shell格式則會忽略,使用Exec格式則會作為參數(shù)追加后執(zhí)行
所以RUN比較獨立,CMD和ENTRYPOINT幾乎一樣,只是在docker run指定指定命令時的表現(xiàn)不同。
寫在后面
參考文章列表:文章來源:http://www.zghlxwxcb.cn/news/detail-427645.html
docker精簡入門(五)run&cmd&enterpoint區(qū)別 。文章來源地址http://www.zghlxwxcb.cn/news/detail-427645.html
到了這里,關(guān)于docker的run,cmd,entrypoint分析和對比的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!