常用指令
指令 | 说明 |
---|---|
FROM | 基础镜像,从这里开始构建 |
MAINTAINER | 作者信息(姓名<邮箱>) |
RUN | 镜像构建时候要执行的命令 |
ADD | 添加内容 (会自动解压) |
WORKDIR | 镜像工作目录 |
VOLUME | 挂载目录 |
EXPOSE | 暴露端口 |
CMD | 容器启动时候要执行的命令(只有最后一个会生效,可被替换) |
ENTRYPOINT | 容器启动时候要执行的命令(可以追加命令) |
ONBUILD | 当构建一个被继承 DockerFile 这个时候就回运行 ONBUILD 指令(触发指令) |
COPY | 类似ADD,将文件拷贝到镜像中 |
ENV | 构建的时候设置环境变量 |
CMD和ENTRYPOINT的区别
CMD # 容器启动时候要执行的命令,只有最后一个会生效,可被替换
ENTRYPOINT # 容器启动时候要执行的命令,可以追加命令
测试CMD
# 编写dockerfile文件
root@DESKTOP-RNPD05U:/opt/dockerfiles# vim dockerfile-cmd
FROM centos
CMD ["ls", "-a"]
# 构建镜像
root@DESKTOP-RNPD05U:/opt/dockerfiles# docker build -f dockerfile-cmd -t dcmd .
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : CMD ["ls", "-a"]
---> Running in 4d31ddfbc7d2
Removing intermediate container 4d31ddfbc7d2
---> 86128c64c5d0
Successfully built 86128c64c5d0
Successfully tagged dcmd:latest
# run运行,发现我们的 ls -a 命令生效
root@DESKTOP-RNPD05U:/opt/dockerfiles# docker run --rm 86128c64c5d0
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 想追加一个命令 -l, ls -al
root@DESKTOP-RNPD05U:/opt/dockerfiles# docker run --rm 86128c64c5d0 -l
docker: Error response from daemon: 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.
# CMD的情况下,-l 替换了 CMD ["ls", "-a"] 命令, -l 不是命令所以报错!
# -l 修改为 ls -al,命令执行成功
root@DESKTOP-RNPD05U:/opt/dockerfiles# docker run --rm 86128c64c5d0 ls -al
total 56
drwxr-xr-x 1 root root 4096 May 16 04:09 .
drwxr-xr-x 1 root root 4096 May 16 04:09 ..
-rwxr-xr-x 1 root root 0 May 16 04:09 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 May 16 04:09 dev
drwxr-xr-x 1 root root 4096 May 16 04:09 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 2021 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 212 root root 0 May 16 04:09 proc
dr-xr-x--- 2 root root 4096 Sep 15 2021 root
drwxr-xr-x 11 root root 4096 Sep 15 2021 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 11 root root 0 May 16 04:09 sys
drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
drwxr-xr-x 20 root root 4096 Sep 15 2021 var
测试ENTRYPOINT
# 编写dockerfile文件
root@DESKTOP-RNPD05U:/opt/dockerfiles# vim dockerfile-entrypoint
FROM centos
ENTRYPOINT ["ls", "-a"]
# 构建镜像
root@DESKTOP-RNPD05U:/opt/dockerfiles# docker build -f dockerfile-entrypoint -t dentrypoint .
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls", "-a"]
---> Running in 756a462e46c7
Removing intermediate container 756a462e46c7
---> 19132678cccb
Successfully built 19132678cccb
Successfully tagged dentrypoint:latest
# run运行
root@DESKTOP-RNPD05U:/opt/dockerfiles# docker run --rm 19132678cccb
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 我们的追加命令是直接拼接在 ENTRYPOINT 命令的后面
root@DESKTOP-RNPD05U:/opt/dockerfiles# docker run --rm 19132678cccb -l
total 56
drwxr-xr-x 1 root root 4096 May 16 03:56 .
drwxr-xr-x 1 root root 4096 May 16 03:56 ..
-rwxr-xr-x 1 root root 0 May 16 03:56 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 May 16 03:56 dev
drwxr-xr-x 1 root root 4096 May 16 03:56 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 2021 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 234 root root 0 May 16 03:56 proc
dr-xr-x--- 2 root root 4096 Sep 15 2021 root
drwxr-xr-x 11 root root 4096 Sep 15 2021 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 11 root root 0 May 16 03:56 sys
drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
drwxr-xr-x 20 root root 4096 Sep 15 2021 var
评论区