DockerFile构建过程
基础知识:
1、每个保留关键字(指令)都是必须大写字母
2、指令从上到下顺序执行
3、# 表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交。
dockerfile是面向开发的,我们要发布项目,做镜像,需要编写dockerfile文件。这个文件十分简单!
Docker镜像逐渐成为企业交付标准,必须要掌握!
步骤:开发,部署,上线运维,缺一不可!
DockerFile:构建文件,定义了一切的步骤,源代码。
DockerImages:通过DockerFile构建生成的镜像,最终发布和运行的产品。
Docker容器:容器就是镜像运行起来提供服务
DockerFile指令
1、知道这些指令后, 我们可以自己构建镜像。
FROM # 基础镜像,一切从这里开始构建
MAINTAINER # 镜像是谁写的,姓名+邮箱 (国际通用标准)
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤:例如,tomcat镜像,这个tomcat压缩包就是添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载目录
EXPOSE # 暴露端口配置
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,而且可被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承的dockerfile,这时候就会运行ONBUILD的指令,触发指令
COPY # 类似ADD,将我们文件拷贝到镜像中
ENV # 构建的时候设置环境变量
实战测试
1、Docker Hub 中99%镜像都是从这个基础镜像开始,FROM scratch(白手起家,从头做起),然后配置需要的软件和配置进行构建。
# 自己创建一个centos
# 1.编写Dockerfile文件
FROM centos
MAINTAINER bruce_liu<1134593154@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
# 2.通过这个dockerfile文件构建镜像
# 命令docker build -f dockerfile文件路径 -t 镜像名:[tag] .
# 3.测试运行
[root@31a3aaa44ff1 local]# ifconfig # 对比之前的原生centos,可以执行ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 11 bytes 946 (946.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# docker history 容器id 可以显示镜像的形成过程
root@liuxiaoweideAir dockerfile#docker history c20987f18b13
2、通过这个命令,我们可以研究一下别的镜像怎么生成的。
3、CMD和ENTRPOINT区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # 指定这个容器的时候要运行的命令,可以追加命令
4、测试CMD
root@liuxiaoweideAir dockerfile # vim dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
root@liuxiaoweideAir dockerfile # docker build -f dockerfile-cmd-test -t cmd-test . # 通过dockerfile-cmd-test构建镜像
[+] Building 1.1s (5/5) FINISHED
=> [internal] load build definition from dockerfile-cmd-test 0.3s
=> => transferring dockerfile: 76B 0.0s
=> [internal] load .dockerignore 0.4s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> CACHED [1/1] FROM docker.io/library/centos 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:7d202bdf002be182b794b7f2b4c90c4fe3560c3ac4f8cebc27f1c8a94ab10a9a 0.0s
=> => naming to docker.io/library/cmd-test 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
root@liuxiaoweideAir dockerfile # docker run -it cmd-test # 通过dockerfile-cmd-test构建镜像,直接执行最后一个CMD,“ls -a”
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
root@liuxiaoweideAir dockerfile #
# 想追加一个命令 -l ,也就是我们希望执行ls -al
root@liuxiaoweideAir dockerfile # docker run cmd-test -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[0002] error waiting for container: context canceled
# 出现这个错误提示,因为CMD的情况下,-l替换了CMD ["ls","-a"]命令,-l不是完整命令,所以报错
# 如下用ls -al 完整命令替换,就不会报错
root@liuxiaoweideAir dockerfile # docker run cmd-test ls -al
total 56
drwxr-xr-x 1 root root 4096 Jan 9 11:15 .
drwxr-xr-x 1 root root 4096 Jan 9 11:15 ..
-rwxr-xr-x 1 root root 0 Jan 9 11:15 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 Jan 9 11:15 dev
drwxr-xr-x 1 root root 4096 Jan 9 11:15 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 170 root root 0 Jan 9 11:15 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 Jan 9 11:15 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
5、测试ENTRYPOINT
# 构建entrypoint dockerfile 文件
root@liuxiaoweideAir dockerfile # vim dockerfile-cmd-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
# 生成镜像文件
root@liuxiaoweideAir dockerfile # docker build -f dockerfile-cmd-entrypoint -t entry-centos .
[+] Building 1.0s (5/5) FINISHED
=> [internal] load build definition from dockerfile-cmd-entrypoint 0.2s
=> => transferring dockerfile: 90B 0.0s
=> [internal] load .dockerignore 0.3s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> CACHED [1/1] FROM docker.io/library/centos 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:b325f5b972337e763ad3b2c0f1a720eb2d5b11a39b3d88008cc5a0e42393b617 0.0s
=> => naming to docker.io/library/entry-centos 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
root@liuxiaoweideAir dockerfile # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0 60d5f5ace1c7 About an hour ago 312MB
nginx latest 605c77e624dd 10 days ago 141MB
tomcat 9.0 b8e65a4d736d 2 weeks ago 680MB
mysql 5.7 c20987f18b13 2 weeks ago 448MB
docker/getting-started latest 26d80cd96d69 5 weeks ago 28.5MB
ubuntu latest ba6acccedd29 2 months ago 72.8MB
entry-centos latest b325f5b97233 3 months ago 231MB
root@liuxiaoweideAir dockerfile # docker run -it b325f5b97233 # 运行 entrypoint生成的容器
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
6、对比CMD
# 追击参数-l,正常执行,直接拼接ENTRYPOINT命令后面,没有报错
root@liuxiaoweideAir dockerfile # docker run -it b325f5b97233 -l
total 56
drwxr-xr-x 1 root root 4096 Jan 9 11:36 .
drwxr-xr-x 1 root root 4096 Jan 9 11:36 ..
-rwxr-xr-x 1 root root 0 Jan 9 11:36 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jan 9 11:36 dev
drwxr-xr-x 1 root root 4096 Jan 9 11:36 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