0.CMD
1.dockerfile中创建包含CMD 命令的文件dockerfile-cmd
FROM centos
CMD ["ls","-a"]
2.通过dockerfile创建镜像
docker build -f dockerfile-cmd -t docker-cmd-test .
3.运行docker-cmd-test镜像
[root@hc dockerFiles]# docker run 21dafa26e85f
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
4.如果在创建的镜像后面直接追加 -l 命令会报如下错误
[root@hc dockerFiles]# docker run 21dafa26e85f -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled
但是重新命令可以覆盖之前的命令
[root@hc dockerFiles]# docker run 21dafa26e85f ls -al
total 56
drwxr-xr-x 1 root root 4096 May 23 03:23 .
drwxr-xr-x 1 root root 4096 May 23 03:23 ..
-rwxr-xr-x 1 root root 0 May 23 03:23 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 May 23 03:23 dev
drwxr-xr-x 1 root root 4096 May 23 03:23 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 Dec 4 17:37 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 161 root root 0 May 23 03:23 proc
dr-xr-x--- 2 root root 4096 Dec 4 17:37 root
drwxr-xr-x 11 root root 4096 Dec 4 17:37 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 Apr 22 08:17 sys
drwxrwxrwt 7 root root 4096 Dec 4 17:37 tmp
drwxr-xr-x 12 root root 4096 Dec 4 17:37 usr
drwxr-xr-x 20 root root 4096 Dec 4 17:37 var
1.ENTRYPOINT
1.dockerfile中创建包含CMD 命令的文件dockerfile-entrypoint
FROM centos
ENTRYPOINT["ls","-a"]
2.通过dockerfile创建镜像
docker build -f dockerfile-entrypoint -t docker-entrypoint-test .
3.运行docker-cmd-test镜像
[root@hc dockerFiles]# docker run 46808b7f9089 -l
total 56
drwxr-xr-x 1 root root 4096 May 23 03:27 .
drwxr-xr-x 1 root root 4096 May 23 03:27 ..
-rwxr-xr-x 1 root root 0 May 23 03:27 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 May 23 03:27 dev
drwxr-xr-x 1 root root 4096 May 23 03:27 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 Dec 4 17:37 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 162 root root 0 May 23 03:27 proc
dr-xr-x--- 2 root root 4096 Dec 4 17:37 root
drwxr-xr-x 11 root root 4096 Dec 4 17:37 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 Apr 22 08:17 sys
drwxrwxrwt 7 root root 4096 Dec 4 17:37 tmp
drwxr-xr-x 12 root root 4096 Dec 4 17:37 usr
drwxr-xr-x 20 root root 4096 Dec 4 17:37 var
总结:
1.CMD 不能追加命令
2.ENTRYPOINT 可以追加命令
3 RUN
说明:运行指定命令 格式:
RUN <command>
RUN ["executable","param1","param2"]
(RUN [“可执行文本”,“参数1”,“参数2”]) 指令会被解析为Json数组,因此必须用双引号。
实例:
RUN echo hello
RUN ["/bin/bash","-c","echo hello"]
RUN默认会在shell终端中运行命令,即/bin/sh -c
RUN ["","",""] 使用exec执行,不会启动shell环境 每条RUN指令将在当前镜像的基础上执行指定命令,并提交为新的镜像。
当命令较长时,可以使用\来换行。
4 CMD
说明:指定启动容器时默认执行的命令。格式:
CMD ["executable","param1","param2"]使用exec执行,推荐使用的方式
CMD command param1 param2 在/bin/sh中执行,提供给需要交互的应用
CMD ["param1","param2"] 提供给ENTRYPOINT的默认参数
每个Dockerfile只能有一条CMD命令,如果指定了多条命令,只有最后一条会被执行。如果用户启动容器时,手动指定了运行的命令(作为run的参数),则会覆盖掉CMD指定的命令。
5 ENTRYPOINT
指定这个容器运行时 要运行的命令,可以追加命令。