CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
# 测试 CMD
# 编写DockerFile的文件
[root@localhost dockerfile]# cat dockerfile-cmd-test
FROM centos
CMD [“ls”,”-a”]
# 根据 DockerFile 构建镜像
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t testcmd:0.1 .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
—> 0d120b6ccaa8
Step 2/2 : CMD [“ls”,”-a”]
—> Running in b3f8ba72222b
Removing intermediate container b3f8ba72222b
—> 561e47f88730
Successfully built 561e47f88730
Successfully tagged testcmd:0.1 # 构建成功
# 查看镜像
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
testcmd 0.1 561e47f88730 6 seconds ago 215MB
centos latest 0d120b6ccaa8 2 days ago 215MB
# 启动镜像 发现ls -a命令生效
[root@localhost dockerfile]# docker run -it testcmd:0.1
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
# 启动命令中 追加一个 -l, 我们期望的是 ls -a -l,但是 报错,这里将 ls -a 替换成了 -l,最终的命令是 -l 所以报错。
[root@localhost dockerfile]# docker run -it 561e47f88730 -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused “exec: \”-l\”: executable file not found in $PATH”: unknown.
# 将启动命令替换成 docker run -it testcmd:0.1 ls -al, 成功打印详细信息;
[root@localhost dockerfile]# docker run -it testcmd:0.1 ls -al
total 0
drwxr-xr-x. 1 root root 6 Aug 13 08:20 .
drwxr-xr-x. 1 root root 6 Aug 13 08:20 ..
-rwxr-xr-x. 1 root root 0 Aug 13 08:20 .dockerenv
lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Aug 13 08:20 dev
drwxr-xr-x. 1 root root 66 Aug 13 08:20 etc
drwxr-xr-x. 2 root root 6 May 11 2019 home
lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx——. 2 root root 6 Aug 9 21:40 lost+found
drwxr-xr-x. 2 root root 6 May 11 2019 media
drwxr-xr-x. 2 root root 6 May 11 2019 mnt
drwxr-xr-x. 2 root root 6 May 11 2019 opt
dr-xr-xr-x. 123 root root 0 Aug 13 08:20 proc
dr-xr-x—. 2 root root 162 Aug 9 21:40 root
drwxr-xr-x. 11 root root 163 Aug 9 21:40 run
lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 May 11 2019 srv
dr-xr-xr-x. 13 root root 0 Aug 11 09:58 sys
drwxrwxrwt. 7 root root 145 Aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 Aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 Aug 9 21:40 var
# 测试二:现在我们把 DockerFile 中 CMD 替换成 ENTRYPOINT 后重新构建,运行 看看结果
[root@localhost dockerfile]# cat dockerfile-cmd-test
FROM centos
ENTRYPOINT [“ls”,”-a”]
# 重新构建镜像
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t testcmd:0.2 .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
—> 0d120b6ccaa8
Step 2/2 : ENTRYPOINT [“ls”,”-a”]
—> Running in c634ca09fabe
Removing intermediate container c634ca09fabe
—> 52d295395f08
Successfully built 52d295395f08
Successfully tagged testcmd:0.2
# 查看镜像
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
testcmd 0.2 52d295395f08 7 seconds ago 215MB
testcmd 0.1 561e47f88730 12 minutes ago 215MB
centos latest 0d120b6ccaa8 2 days ago 215MB
# 运行testcmd:0.2镜像 并追加 -l ,发现 打印出了详细信息
[root@localhost dockerfile]# docker run -it testcmd:0.2 -l
total 0
drwxr-xr-x. 1 root root 6 Aug 13 08:17 .
drwxr-xr-x. 1 root root 6 Aug 13 08:17 ..
-rwxr-xr-x. 1 root root 0 Aug 13 08:17 .dockerenv
lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Aug 13 08:17 dev
drwxr-xr-x. 1 root root 66 Aug 13 08:17 etc
drwxr-xr-x. 2 root root 6 May 11 2019 home
lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx——. 2 root root 6 Aug 9 21:40 lost+found
drwxr-xr-x. 2 root root 6 May 11 2019 media
drwxr-xr-x. 2 root root 6 May 11 2019 mnt
drwxr-xr-x. 2 root root 6 May 11 2019 opt
dr-xr-xr-x. 121 root root 0 Aug 13 08:17 proc
dr-xr-x—. 2 root root 162 Aug 9 21:40 root
drwxr-xr-x. 11 root root 163 Aug 9 21:40 run
lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 May 11 2019 srv
dr-xr-xr-x. 13 root root 0 Aug 11 09:58 sys
drwxrwxrwt. 7 root root 145 Aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 Aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 Aug 9 21:40 var