CMD 命令演示
编写 Dockerfile
[root@sail dockerfile]# vim Dockerfile-cmd-test
[root@sail dockerfile]# cat Dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
构建镜像
[root@localhost home]# docker build -f dockerfile-cmd-test -t cmd .
[+] Building 0.0s (5/5) FINISHED doc
=> [internal] load build definition from dockerfile-cmd-test
=> => transferring dockerfile: 133B
=> [internal] load metadata for docker.io/library/centos:lat
=> [internal] load .dockerignore
=> => transferring context: 2B
=> CACHED [1/1] FROM docker.io/library/centos:latest
=> exporting to image
=> => exporting layers
=> => writing image sha256:2b06f96416291f005301cc48ccf63a8fa
=> => naming to docker.io/library/cmd
运行一下
先看id
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos latest c7defb0511c1 19 minutes ago 312MB
centos latest 5d0da3dc9764 2 years ago 231MB
cmd latest 2b06f9641629 2 years ago 231MB
cmdtest latest 2b06f9641629 2 years ago 231MB
[root@localhost home]# docker run 2b06f9641629
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
此时 Dockerfile 中编写的命令生效了。
追加
-l
命令
[root@localhost home]# docker run cmd -l
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
没有达到预期的 ls -al
命令。
CMD
是替换的方式,-l
不是命令,所以报错。
ENTRYPOINT 命令演示
编写 Dockerfile
[root@sail dockerfile]# vim dockerfile-entrypoint-test
[root@sail dockerfile]# cat dockerfile-entrypoint-test
FROM centos
ENTRYPOINT ["ls","-a"]
构建镜像
[root@localhost home]# docker build -f docker-entrypoint-test -t entrypoint .
[+] Building 0.0s (5/5) FINISHED docker:default
=> [internal] load build definition from docker-entr 0.0s
=> => transferring dockerfile: 144B 0.0s
=> [internal] load metadata for docker.io/library/ce 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/1] FROM docker.io/library/centos:latest 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:a58b53c50da3074855495de14 0.0s
=> => naming to docker.io/library/entrypoint 0.0s
运行镜像
[root@localhost home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE cmd latest 2b06f9641629 2 years ago 231MB entrypoint latest a58b53c50da3 2 years ago 231MB [root@localhost home]# docker run entrypoint . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
此时 Dockerfile 中编写的命令也生效了。
追加
-l
命令[root@localhost home]# docker run entrypoint -l total 4 drwxr-xr-x. 1 root root 6 May 30 12:44 . drwxr-xr-x. 1 root root 6 May 30 12:44 .. -rwxr-xr-x. 1 root root 0 May 30 12:44 .dockerenv lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x. 5 root root 340 May 30 12:44 dev drwxr-xr-x. 1 root root 66 May 30 12:44 etc drwxr-xr-x. 2 root root 6 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 6 Sep 15 2021 lost+found drwxr-xr-x. 2 root root 6 Nov 3 2020 media drwxr-xr-x. 2 root root 6 Nov 3 2020 mnt drwxr-xr-x. 2 root root 6 Nov 3 2020 opt dr-xr-xr-x. 342 root root 0 May 30 12:44 proc dr-xr-x---. 2 root root 162 Sep 15 2021 root drwxr-xr-x. 11 root root 163 Sep 15 2021 run lrwxrwxrwx. 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Nov 3 2020 srv dr-xr-xr-x. 13 root root 0 May 30 00:36 sys drwxrwxrwt. 7 root root 171 Sep 15 2021 tmp drwxr-xr-x. 12 root root 144 Sep 15 2021 usr drwxr-xr-x. 20 root root 4096 Sep 15 2021 var
运行了预期的 ls -al
命令。
ENTRYPOINT
是追加的方式。
Docker 中许多命令都十分相似,我们需要了解他们的区别,最好的方式就是这样对比测试。
标签:区别,0.0,drwxr,CMD,ENTRYPOINT,xr,home,docker,root From: https://www.cnblogs.com/muxinq/p/18223215