DockerFile
DockerFile介绍
dockerfile是用来构建docker景象的文件!命令参数脚本!
构建步骤:
- 编写一个dockerfile 文件
- docker build 构建成为一个镜像
- docker run 运行镜像
- docker push 发布镜像(Docker Hub ,阿里云镜像仓库!)
查看一下官方是怎么做的?
很多官方镜像都是基础包,很多功能都没有,我们通常会自己搭建自己的镜像!
官方既然可以制作镜像,那我们也可以!
DockerFile构建过程
基础知识:
- 每一个保留关键字(指令)都是必须是大写字母
- 执行从上到下顺序执行
- #表示注释
- 每一个指令都会创建提交一个新的镜像成,并提交!
dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单!
Docker镜像逐渐成为了企业交付的标准,必须要掌握!
步骤:开发,部署,运维…缺一不可!
DockerFile:构建文件,定义了一切的步骤,源代码
DockerImages:通过DockerFile构建生成的镜像,最终发布和运行的产品!
Docker容器:容器就是镜像运行起来提供服务器
DockerFile的指令
以前的话我们就是使用别人的,现在我们知道了这些指令后,我们来联系自己写一个镜像
FROM # 基础镜像,一切从这里开始构建 centos
MAINTAINER # 镜像是谁写的 姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤,tomcat镜像,这个tomcat压缩包!添加内容,add命令会自动解压
WORKIR # 镜像的工作目录
VOLUME # 挂载到那个位置-》挂在目录
EXPOST # 保留暴露端口=-p
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYOINT # 指定这个容器启动的时候要运行的命令.可以追加命令
ONBUILD # 当构建一个被继承DockerFile这个是欧就会运行ONBUILD的指令,触发命令.
COPY # 类似ADD,将我们文件拷贝到镜像中
ENV # 构建的时候设置环境变量!
实战测试
Docker Hub中99%镜像都是从这个镜像中过来的 FROM scratch,然后配置需要的软件和配置来进行的构建
创建一个自己的centos
# 1.编写Dockerfile的文件
[root@aZang dockerfile]# cat mydockerfile
FROM centos
MAINTAINER azang
ENV MYPATH /usr/local
WORKDIR SMYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "---end---"
CMD /bin/bash
[root@aZang dockerfile]#
# 2.通过这个文件构建镜像
# 命令docker build -f dockerfile文件路径 -t 镜像名:[tag]
[root@aZang dockerfile]# docker build -f mydockerfile -t azang/centos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 300e315adb2f
Step 2/10 : MAINTAINER azang
---> Running in a5bac01270e6
Removing intermediate container a5bac01270e6
---> cfcec5f1b7ef
Step 3/10 : ENV MYPATH /usr/local
---> Running in 70c56fc1338e
Removing intermediate container 70c56fc1338e
---> e1e827378f1c
Step 4/10 : WORKDIR SMYPATH
---> Running in 15dd5d7c4457
Removing intermediate container 15dd5d7c4457
---> 644b206ba7a3
Step 5/10 : RUN yum -y install vim
---> Running in 3924f2666a7f
CentOS Linux 8 - AppStream 1.9 MB/s | 7.4 MB 00:03
CentOS Linux 8 - BaseOS 82 kB/s | 2.6 MB 00:32
CentOS Linux 8 - Extras 3.3 kB/s | 9.6 kB 00:02
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-15.el8 appstream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-17.el8 appstream 39 k
vim-common x86_64 2:8.0.1763-15.el8 appstream 6.3 M
vim-filesystem noarch 2:8.0.1763-15.el8 appstream 48 k
which x86_64 2.21-12.el8 baseos 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 283 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 624 kB/s | 48 kB 00:00
(3/5): which-2.21-12.el8.x86_64.rpm 321 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 1.5 MB/s | 1.4 MB 00:00
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm 1.7 MB/s | 6.3 MB 00:03
--------------------------------------------------------------------------------
Total 1.3 MB/s | 7.8 MB 00:06
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : which-2.21-12.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-15.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-15.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-15.el8.noarch 4/5
Verifying : which-2.21-12.el8.x86_64 5/5
Installed:
gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-15.el8.x86_64
vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
which-2.21-12.el8.x86_64
Complete!
Removing intermediate container 3924f2666a7f
---> 08c47fd7e777
Step 6/10 : RUN yum -y install net-tools
---> Running in 505a3d75b432
Last metadata expiration check: 0:00:13 ago on Thu Jun 10 15:08:41 2021.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 1.5 MB/s | 322 kB 00:00
--------------------------------------------------------------------------------
Total 367 kB/s | 322 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
Removing intermediate container 505a3d75b432
---> 3d184c88a241
Step 7/10 : EXPOSE 80
---> Running in fed7c7edc87c
Removing intermediate container fed7c7edc87c
---> f9ca05896d03
Step 8/10 : CMD echo $MYPATH
---> Running in 50c7eda21ca9
Removing intermediate container 50c7eda21ca9
---> c98f98fd1080
Step 9/10 : CMD echo "---end---"
---> Running in b5e7c57e570e
Removing intermediate container b5e7c57e570e
---> f5b00198661e
Step 10/10 : CMD /bin/bash
---> Running in e43c86088525
Removing intermediate container e43c86088525
---> 2b1119b73bdd
Successfully built 2b1119b73bdd
Successfully tagged azang/centos:0.1
# 3.测试运行
[root@aZang dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
azang/centos 0.1 2b1119b73bdd 3 minutes ago 295MB
redis latest bc8d70f9ef6c 4 weeks ago 105MB
mysql latest c0cdc95609f1 4 weeks ago 556MB
azang/centos 1.0 639ccb37770a 4 weeks ago 209MB
mysql 5.7 87eca374c0ed 7 weeks ago 447MB
nginx latest 62d49f9bab67 8 weeks ago 133MB
centos latest 300e315adb2f 6 months ago 209MB
[root@aZang dockerfile]# docker run -it azang/centos:0.1
[root@a7cf78c85e3c SMYPATH]# pwd
/SMYPATH
[root@a7cf78c85e3c SMYPATH]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.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
[root@a7cf78c85e3c SMYPATH]# vim test
[root@a7cf78c85e3c SMYPATH]#
对比:之前原生的
我们可以列出本地镜像的变更历史
# 查看历史构内容
# 命令: docker history 镜像id
[root@aZang ~]# docker history 2b1119b73bdd
IMAGE CREATED CREATED BY SIZE COMMENT
2b1119b73bdd 10 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
f5b00198661e 10 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
c98f98fd1080 10 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
f9ca05896d03 10 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
3d184c88a241 10 minutes ago /bin/sh -c yum -y install net-tools 24.8MB
08c47fd7e777 10 minutes ago /bin/sh -c yum -y install vim 60.4MB
644b206ba7a3 11 minutes ago /bin/sh -c #(nop) WORKDIR /SMYPATH 0B
e1e827378f1c 11 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
cfcec5f1b7ef 11 minutes ago /bin/sh -c #(nop) MAINTAINER azang 0B
300e315adb2f 6 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 6 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 6 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
我们平时拿到一个镜像,可以研究一下他是怎么做到的
CMD 和 ENTRYPOINT 区别
CMD 和 ENTRYPOINT 区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYOINT # 指定这个容器启动的时候要运行的命令.可以追加命令
- 测试CMD
# 编写dockerfile文件
[root@aZang dockerfile]# vim dockerfile-cmd-test
# FROM centos
# CMD ["ls","-a"]
# 构建镜像
[root@aZang dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 300e315adb2f
Step 2/2 : CMD ["ls","-a"]
---> Running in 939dde5445e9
Removing intermediate container 939dde5445e9
---> 93f2c68b8848
Successfully built 93f2c68b8848
Successfully tagged cmdtest:latest
# run运行,发现我们的ls -a 命令生效
[root@aZang dockerfile]# docker run 93f2c68b8848
.
..
.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@aZang dockerfile]# docker run 93f2c68b8848 -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
# cmd的情况下 -l替换了CMD ["ls","-a"]命令,-l不是命令所以报错!
# 正确使用方式时使用完整的命令替换
[root@aZang dockerfile]# docker run 93f2c68b8848 ls -al
total 56
drwxr-xr-x 1 root root 4096 Jun 16 11:13 .
drwxr-xr-x 1 root root 4096 Jun 16 11:13 ..
-rwxr-xr-x 1 root root 0 Jun 16 11:13 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 Jun 16 11:13 dev
drwxr-xr-x 1 root root 4096 Jun 16 11:13 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 2020 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 107 root root 0 Jun 16 11:13 proc
dr-xr-x--- 2 root root 4096 Dec 4 2020 root
drwxr-xr-x 11 root root 4096 Dec 4 2020 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 May 11 14:05 sys
drwxrwxrwt 7 root root 4096 Dec 4 2020 tmp
drwxr-xr-x 12 root root 4096 Dec 4 2020 usr
drwxr-xr-x 20 root root 4096 Dec 4 2020 var
- 测试ENTRYPOINT
# 编写dockerfile文件
[root@aZang dockerfile]# vim dockerfile-cmd-entrypoint
# FROM centos
# ENTRYPOINT ["ls","-a"]
# 构建镜像
[root@aZang dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint-test .
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos
---> 300e315adb2f
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in cfff9236df9c
Removing intermediate container cfff9236df9c
---> 5dee28b973dd
Successfully built 5dee28b973dd
Successfully tagged entrypoint-test:latest
# run运行,发现我们的ls -a 命令生效
[root@aZang dockerfile]# docker run 5dee28b973dd
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 想追加一个命令-l ls -al
# 发现和CMD的不同不会顶替掉原有的,而是进行追加
[root@aZang dockerfile]# docker run 5dee28b973dd -l
total 56
drwxr-xr-x 1 root root 4096 Jun 16 11:27 .
drwxr-xr-x 1 root root 4096 Jun 16 11:27 ..
-rwxr-xr-x 1 root root 0 Jun 16 11:27 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 Jun 16 11:27 dev
drwxr-xr-x 1 root root 4096 Jun 16 11: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 2020 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 110 root root 0 Jun 16 11:27 proc
dr-xr-x--- 2 root root 4096 Dec 4 2020 root
drwxr-xr-x 11 root root 4096 Dec 4 2020 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 May 11 14:05 sys
drwxrwxrwt 7 root root 4096 Dec 4 2020 tmp
drwxr-xr-x 12 root root 4096 Dec 4 2020 usr
drwxr-xr-x 20 root root 4096 Dec 4 2020 var
[root@aZang dockerfile]#
Dockerfile中很多的命令都十分的相似,我们需要了解他们的区别,我们最好的学习就是对比他们然后测试效果!
Docker其他学习笔记
Docker学习笔记(一)—安装启动hello world
Docker学习笔记(二)—底层原理
Docker学习笔记(三)—常用命令
Docker学习笔记(四)练习—安装nginx
Docker学习笔记(五)练习—安装tomcat
Docker学习笔记(六)—可视化
Docker学习笔记(七)----镜像
Docker学习笔记(八)—数据容器卷
Docker学习笔记(九)—DockerFile
Docker学习笔记(十)—发布自己的镜像
Docker学习笔记(十一)–docker网络