08Dockerfile基本使用
使用Dockerfile创建镜像
Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile赖快速创建自定义的镜像.
Dockerfile由一行行命令组成,#开头为注释.
1:Dockerfile基础内容
1:每条保留字指令都必须为大写字母且后面要跟随至少一个参数
2:指令从上到下,顺序执行
3:#表示注释
4:每条RUN指令都会创建一个新的镜像层,并且提交
2:Dockerfile基本结构和指令
Dockerfile分为四部分:基础镜像信息,维护者信息,镜像操作指令和容器启动时执行指令
指令 | 解释 |
---|---|
FROM | 指明当前镜像是基于哪个镜像 |
MAINTAINER | 镜像维护者姓名和邮箱地址 |
ENV | 设置环境变量 |
WORKDIR | 指定在创建容器后,终端默认登录进来的工作目录 |
ADD | 将宿主机目录下的文件拷贝进镜像,并自动处理URL和解压缩tar包 |
COPY | 拷贝文件和目录到镜像中 |
VOLUME | 容器数据卷 |
RUN | 镜像构建时运行的命令,每条RUN指令都会创建一个新的镜像层,并且提交 |
EXPOSE | 容器对外暴露的端口 |
CMD | 指定一个容器启动时候要运行的命令,有多个CMD时,会覆盖,只有最有一个会生效;CMD会被docker run 后面的命令覆盖 |
ENTRYPOINT | 指定一个容器启动时候要运行的命令,但是不会覆盖,会追加 |
ONBUILD | 此镜像被作为父镜像是,会触发的命令 |
3:示例:自定义镜像
1:centos基本镜像没有vim,ifconfig工具
[root@docker ~]# docker run -it centos /bin/bash
[root@8414a93e2eba /]# vim test.txt
bash: vim: command not found
[root@8414a93e2eba /]# ifconfig
bash: ifconfig: command not found
[root@8414a93e2eba /]#
2:编写Dockerfile文件
[root@docker ~]# vim Dockerfile
FROM centos
MAINTAINER vargant007<923737581@qq.com>
#设置变量
ENV MYPATH /usr/local
#设置工作目录
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 8000
CMD echo $MYPATH
CMD echo "success ------OK"
CMD /bin/bash
3:构建自定义镜像
格式:docker build -f Dockerfile文件路径 -t 新镜像名字:标签 .
#注意有一个.指定镜像构建过程中的上下文环境的目录
[root@docker ~]# docker build -f /root/Dockerfile -t mycentos:1.3 .
Sending build context to Docker daemon 3.976MB
Step 1/10 : FROM centos
---> 0f3e07c0138f
Step 2/10 : MAINTAINER vargant007<923737581@qq.com>
---> Running in b399d6eb2708
Removing intermediate container b399d6eb2708
---> 99b5cde412f4
Step 3/10 : ENV MYPATH /usr/local
---> Running in e2382858994d
Removing intermediate container e2382858994d
---> 031eb583bc70
Step 4/10 : WORKDIR $MYPATH
---> Running in 9fc0ae050026
Removing intermediate container 9fc0ae050026
---> a4778a813e92
Step 5/10 : RUN yum -y install vim
---> Running in 73cc7539a7fc
CentOS-8 - AppStream 629 kB/s | 6.3 MB 00:10
CentOS-8 - Base 726 kB/s | 7.9 MB 00:11
CentOS-8 - Extras 446 B/s | 2.1 kB 00:04
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-10.el8 AppStream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k
vim-common x86_64 2:8.0.1763-10.el8 AppStream 6.3 M
vim-filesystem noarch 2:8.0.1763-10.el8 AppStream 48 k
which x86_64 2.21-10.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-15.el8.x86_64.rpm 32 kB/s | 39 kB 00:01
(2/5): vim-filesystem-8.0.1763-10.el8.noarch.rp 254 kB/s | 48 kB 00:00
(3/5): which-2.21-10.el8.x86_64.rpm 93 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-10.el8.x86_64.rpm 585 kB/s | 1.4 MB 00:02
(5/5): vim-common-8.0.1763-10.el8.x86_64.rpm 969 kB/s | 6.3 MB 00:06
--------------------------------------------------------------------------------
Total 750 kB/s | 7.8 MB 00:10
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-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-10.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-10.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-10.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-10.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-10.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-10.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-10.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-10.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-10.el8.noarch 4/5
Verifying : which-2.21-10.el8.x86_64 5/5
Installed:
vim-enhanced-2:8.0.1763-10.el8.x86_64 gpm-libs-1.20.7-15.el8.x86_64
vim-common-2:8.0.1763-10.el8.x86_64 vim-filesystem-2:8.0.1763-10.el8.noarch
which-2.21-10.el8.x86_64
Complete!
Removing intermediate container 73cc7539a7fc
---> 40dee5128b87
Step 6/10 : RUN yum -y install net-tools
---> Running in 6dff3319a5c8
Last metadata expiration check: 0:00:18 ago on Mon Nov 25 21:34:01 2019.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.51.20160912git.el8 BaseOS 323 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 323 k
Installed size: 1.0 M
Downloading Packages:
net-tools-2.0-0.51.20160912git.el8.x86_64.rpm 178 kB/s | 323 kB 00:01
--------------------------------------------------------------------------------
Total 98 kB/s | 323 kB 00:03
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.51.20160912git.el8.x86_64
Complete!
Removing intermediate container 6dff3319a5c8
---> 38ee719064bf
Step 7/10 : EXPOSE 8000
---> Running in f5d94def858c
Removing intermediate container f5d94def858c
---> cb7a32a28939
Step 8/10 : CMD echo $MYPATH
---> Running in d8849409e238
Removing intermediate container d8849409e238
---> c7aa85692260
Step 9/10 : CMD echo "success ------OK"
---> Running in de18e204d92c
Removing intermediate container de18e204d92c
---> ef8a6e27705a
Step 10/10 : CMD /bin/bash
---> Running in 2d9372a8083a
Removing intermediate container 2d9372a8083a
---> 3942972c5c9c
Successfully built 3942972c5c9c
Successfully tagged mycentos:1.3
4:查看当前镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.3 3942972c5c9c About a minute ago 301MB
centos latest 0f3e07c0138f 7 weeks ago 220MB
5:使用自定义镜像构建容器
[root@docker ~]# docker run -it mycentos:1.3
[root@c390446635c6 local]# 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 7 bytes 586 (586.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@c390446635c6 local]# pwd
/usr/local
6:查看镜像创建历史
[root@docker ~]# docker history mycentos:1.3
IMAGE CREATED CREATED BY SIZE COMMENT
3942972c5c9c 32 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
ef8a6e27705a 32 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
c7aa85692260 32 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
cb7a32a28939 32 minutes ago /bin/sh -c #(nop) EXPOSE 8000 0B
38ee719064bf 32 minutes ago /bin/sh -c yum -y install net-tools 14.3MB
40dee5128b87 32 minutes ago /bin/sh -c yum -y install vim 66.8MB
a4778a813e92 33 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
031eb583bc70 33 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
99b5cde412f4 33 minutes ago /bin/sh -c #(nop) MAINTAINER vargant007<923… 0B
0f3e07c0138f 7 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 7 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 7 weeks ago /bin/sh -c #(nop) ADD file:d6fdacc1972df524a… 220MB