CMD # 指定这个容器启动的时候运行的命令,只有最后一个会生效,可替代 ENTRYPOINT # 指定这个容器启动的时候运行的命令,可以追加
1. CMD命令测试
1.1 新建docker-cmd-test
root@docker:/home/dockerfile# vim docker-cmd-test
1.2 输入内容
- 内容说明:启动centos,运行查看目录命令
docker-cmd-test
文件中输入以下内容
FROM centos CMD ["ls","-a"]
1.3 构建
root@docker:/home/dockerfile# docker build -f docker-cmd-test -t cmdtest .
1.4 运行容器
root@docker:/home/dockerfile# docker run 35481d75c5de
run运行,如图,发现ls -a命令生效
想追加命令
-l
实现ls -al
,报错# 1. 追加-l报错 root@docker:/home/dockerfile# docker run 35481d75c5de -l docker: Error response from daemon: 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 # 2. 这样写可以,比较麻烦 root@docker:/home/dockerfile# docker run 35481d75c5de ls -al total 56 drwxr-xr-x 1 root root 4096 Nov 17 11:57 . drwxr-xr-x 1 root root 4096 Nov 17 11:57 .. -rwxr-xr-x 1 root root 0 Nov 17 11:57 .dockerenv lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Nov 17 11:57 dev drwxr-xr-x 1 root root 4096 Nov 17 11:57 etc drwxr-xr-x 2 root root 4096 Nov 3 2020 home lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------ 2 root root 4096 Sep 15 14:17 lost+found drwxr-xr-x 2 root root 4096 Nov 3 2020 media drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt drwxr-xr-x 2 root root 4096 Nov 3 2020 opt dr-xr-xr-x 427 root root 0 Nov 17 11:57 proc dr-xr-x--- 2 root root 4096 Sep 15 14:17 root drwxr-xr-x 11 root root 4096 Sep 15 14:17 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 Nov 17 11:57 sys drwxrwxrwt 7 root root 4096 Sep 15 14:17 tmp drwxr-xr-x 12 root root 4096 Sep 15 14:17 usr drwxr-xr-x 20 root root 4096 Sep 15 14:17 var
2. ENTRYPOINT 命令测试
2.1 编辑测试文件
root@docker:/home/dockerfile# vim dockerfile-entrypoint-test
dockerfile-entrypoint-test
写入如下内容
FROM centos ENTRYPOINT ["ls","-a"]
2.2 构建
root@docker:/home/dockerfile# docker build -f dockerfile-entrypoint-test -t entrypoint-test . Sending build context to Docker daemon 4.096kB Step 1/2 : FROM centos ---> 5d0da3dc9764 Step 2/2 : ENTRYPOINT ["ls","-a"] ---> Running in 93f6826b6784 Removing intermediate container 93f6826b6784 ---> 50eaee991418 Successfully built 50eaee991418 Successfully tagged entrypoint-test:latest
2.3 测试
# 追加命令,是直接拼接在ENTRYPOINT命令后面 root@docker:/home/dockerfile# docker run 50eaee991418 -l total 56 drwxr-xr-x 1 root root 4096 Nov 17 12:04 . drwxr-xr-x 1 root root 4096 Nov 17 12:04 .. -rwxr-xr-x 1 root root 0 Nov 17 12:04 .dockerenv lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Nov 17 12:04 dev drwxr-xr-x 1 root root 4096 Nov 17 12:04 etc drwxr-xr-x 2 root root 4096 Nov 3 2020 home lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------ 2 root root 4096 Sep 15 14:17 lost+found drwxr-xr-x 2 root root 4096 Nov 3 2020 media drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt drwxr-xr-x 2 root root 4096 Nov 3 2020 opt dr-xr-xr-x 430 root root 0 Nov 17 12:04 proc dr-xr-x--- 2 root root 4096 Sep 15 14:17 root drwxr-xr-x 11 root root 4096 Sep 15 14:17 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 Nov 17 12:04 sys drwxrwxrwt 7 root root 4096 Sep 15 14:17 tmp drwxr-xr-x 12 root root 4096 Sep 15 14:17 usr drwxr-xr-x 20 root root 4096 Sep 15 14:17 var