Dockerfile 记录
Author: Xin Pan
Date: 2022.10.05
Dockerfile是用于构建docker镜像的文件。它是一个命令参数脚本。
基础知识
- 保留关键字都是大写字母;
- 从上到下执行;
- 每行指令都是一个层(layer);
- 注释用#开头。
Dockerfile指令
FROOM # 从基础镜像构建
MAINTAINER # 镜像谁维护的
RUN # 镜像构建时运行的命令
ADD # 将文件添加进容器,而且可以自动解压
COPY # 类似ADD,将文件拷贝到容器
WORKDIR # 和cd 一样
VOLUME # 挂载的目录位置
EXPOSE # 指定暴露端口和 -p 一样
CMD # 指定容器运行的时候运行的指令,只有最后一个生效,而且被替代
ENTRYPOINT # 指定容器运行的时候运行的指令,会去追加命令
ONBUILD # 当构建一个被继承Dockerfile会运行ONBUILD的指令。是触发指令。
ENV # 设置环境变量 -e 一样的。
可以通过docker history <image_id>
列出本地镜像进行的变更历史。
区分CMD和ENTRYPOINT
首先CMD命令
我的dockerfile
很简单
FROM centos
CMD ["ls","-a"]
下边是运行结果
panxin@ubuntu-gtx3080:~/docker2$ docker build -f dockerfile .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
---> Running in 9a1ddaf46855
Removing intermediate container 9a1ddaf46855
---> d232c56b9b9d
Successfully built d232c56b9b9d
panxin@ubuntu-gtx3080:~/docker2$ docker run -it d232c56b9b9d
. .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
panxin@ubuntu-gtx3080:~/docker2$ docker run -it d232c56b9b9d -l
docker: Error response from daemon: failed to create shim: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled
panxin@ubuntu-gtx3080:~/docker2$ docker run -it d232c56b9b9d ls -la
total 56
drwxr-xr-x 1 root root 4096 Oct 5 09:21 .
drwxr-xr-x 1 root root 4096 Oct 5 09:21 ..
-rwxr-xr-x 1 root root 0 Oct 5 09:21 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Oct 5 09:21 dev
drwxr-xr-x 1 root root 4096 Oct 5 09:21 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 693 root root 0 Oct 5 09:21 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 13 root root 0 Oct 5 09:21 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
也就是这里的-l
替换了dockerfile里边的CMD ["ls","-a"]
这个命令,所以会报那个错误。
测试ENTRYPOINT
这里的dockerfile_entry包含下边内容
FROM centos
ENTRYPOINT ["ls","-a"]
它运行的结果就是下边所示。
panxin@ubuntu-gtx3080:~/docker2$ docker build -f dockerfile_entry -t entrypoint .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in 24678f09322a
Removing intermediate container 24678f09322a
---> c44b1733b2bf
Successfully built c44b1733b2bf
Successfully tagged entrypoint:latest
panxin@ubuntu-gtx3080:~/docker2$ docker run c44b1733b2bf
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
panxin@ubuntu-gtx3080:~/docker2$ docker run c44b1733b2bf -l
total 56
drwxr-xr-x 1 root root 4096 Oct 5 09:29 .
drwxr-xr-x 1 root root 4096 Oct 5 09:29 ..
-rwxr-xr-x 1 root root 0 Oct 5 09:29 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 Oct 5 09:29 dev
drwxr-xr-x 1 root root 4096 Oct 5 09:29 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 677 root root 0 Oct 5 09:29 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 13 root root 0 Oct 5 09:29 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
命令后边的。
和CMD
的区别就是在这里。
改镜像版本和名字
docker tag <image_id> <new/image_name>:<version>
就可以了。
总结课程的知识。这个图来自视频,我觉得画的很好。