ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**结论:** * CMD命令和ENTRYPOINT命令一样,都是容器启动时运行 * CMD 会被docker run的命令行参数所替换,而ENTRYPOINT则不会,它会将该参数追加拼接至 ENTRYPOINT的命令参数后执行 **验证实例:** 1、创建第一个dockerfile:dockerfile-cmd ``` FROM centos:7 CMD ["ls", "-a"] ``` 2、构建dockerfile-cmd并执行 ``` [root@iZbp1bum6107bp8mgzkeunZ example-cmd-entrypoint]# docker build -f dockerfile-cmd -t testcmd . Sending build context to Docker daemon 3.072kB Step 1/2 : FROM centos:7 ---> eeb6ee3f44bd Step 2/2 : CMD ["ls", "-a"] ---> Running in 2c595aa2252b Removing intermediate container 2c595aa2252b ---> e488f842206e Successfully built e488f842206e Successfully tagged testcmd:latest // 构造完成后,run执行,发现ls -a生效并执行了 [root@iZbp1bum6107bp8mgzkeunZ example-cmd-entrypoint]# docker run e488f842206e . .. .dockerenv anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var // 重新运行时,追加 -l 参数发现报错 // 这是因为CMD 情况下,docker run中的参数 替换了CMD中的命令["ls","-a"],而 -l不是完整命令,因此报错 [root@iZbp1bum6107bp8mgzkeunZ example-cmd-entrypoint]# docker run e488f842206e -l docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown. ERRO[0000] error waiting for container: context canceled // docker run中写上完整命令参数:“ls -al” ,它会替换dockerfile中的cmd命令执行 [root@iZbp1bum6107bp8mgzkeunZ example-cmd-entrypoint]# docker run e488f842206e ls -al total 64 drwxr-xr-x 1 root root 4096 Apr 10 09:22 . drwxr-xr-x 1 root root 4096 Apr 10 09:22 .. -rwxr-xr-x 1 root root 0 Apr 10 09:22 .dockerenv -rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Apr 10 09:22 dev drwxr-xr-x 1 root root 4096 Apr 10 09:22 etc drwxr-xr-x 2 root root 4096 Apr 11 2018 home lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64 drwxr-xr-x 2 root root 4096 Apr 11 2018 media drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt drwxr-xr-x 2 root root 4096 Apr 11 2018 opt dr-xr-xr-x 122 root root 0 Apr 10 09:22 proc dr-xr-x--- 2 root root 4096 Nov 13 2020 root drwxr-xr-x 11 root root 4096 Nov 13 2020 run lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Apr 11 2018 srv dr-xr-xr-x 13 root root 0 Apr 10 09:22 sys drwxrwxrwt 7 root root 4096 Nov 13 2020 tmp drwxr-xr-x 13 root root 4096 Nov 13 2020 usr drwxr-xr-x 18 root root 4096 Nov 13 2020 var ``` 3、创建第二个dockerfile:dockerfile-entrypoint ``` FROM centos:7 ENTRYPOINT ["ls", "-a"] ``` 4、构建dockerfile-entrypoint并执行 ``` [root@iZbp1bum6107bp8mgzkeunZ example-cmd-entrypoint]# docker build -f dockerfile-entrypoint -t testentrypoint . Sending build context to Docker daemon 3.072kB Step 1/2 : FROM centos:7 ---> eeb6ee3f44bd Step 2/2 : ENTRYPOINT ["ls", "-a"] ---> Running in 89ab820cbc50 Removing intermediate container 89ab820cbc50 ---> 3a10abeda8be Successfully built 3a10abeda8be Successfully tagged testentrypoint:latest // ls -a 同样运行成功 [root@iZbp1bum6107bp8mgzkeunZ example-cmd-entrypoint]# docker run 3a10abeda8be . .. .dockerenv anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var // 重新运行时,在docker run后 追加命令参数,它是直接拼接在 ENTRYPOINT命令后面 ,因此完整命令为: ls -al [root@iZbp1bum6107bp8mgzkeunZ example-cmd-entrypoint]# docker run 3a10abeda8be -l total 64 drwxr-xr-x 1 root root 4096 Apr 10 09:25 . drwxr-xr-x 1 root root 4096 Apr 10 09:25 .. -rwxr-xr-x 1 root root 0 Apr 10 09:25 .dockerenv -rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Apr 10 09:25 dev drwxr-xr-x 1 root root 4096 Apr 10 09:25 etc drwxr-xr-x 2 root root 4096 Apr 11 2018 home lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64 drwxr-xr-x 2 root root 4096 Apr 11 2018 media drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt drwxr-xr-x 2 root root 4096 Apr 11 2018 opt dr-xr-xr-x 119 root root 0 Apr 10 09:25 proc dr-xr-x--- 2 root root 4096 Nov 13 2020 root drwxr-xr-x 11 root root 4096 Nov 13 2020 run lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Apr 11 2018 srv dr-xr-xr-x 13 root root 0 Apr 10 09:22 sys drwxrwxrwt 7 root root 4096 Nov 13 2020 tmp drwxr-xr-x 13 root root 4096 Nov 13 2020 usr drwxr-xr-x 18 root root 4096 Nov 13 2020 var ```