Docker镜像的分层
- Dockerfile中的每个指令都会创建一个新的镜像层
- 镜像层将被缓存和复用
- 当Dockerfile的指令修改了,复制的文件变化了,或者构建镜像时指定的变量不同了,对应的镜像层缓存就会失效
某一层的镜像缓存失效之后,它之后的镜像层缓存都会失效 - 镜像层是不可变的,如果在某一层中添加一个文件,然后在下一层中删除它,则镜像中依然会包含该文件
镜像的创建
Docker镜像
- 应用发布的标准格式
- 支撑一个Docker容器的运行
Docker镜像的创建方法
- 基于已有镜像创建
- 基于本地模板创建
- 基于Dockerfile创建——DIY
基于已有镜像创建
将容器里面的运行的程序及运行环境打包生成新的镜像
docker commit [选项] 容器ID/名称 仓库名称:[标签]
- -m说明信息
- -a作者信息
- -p生成过程中停止容器的运行
基于本地模板创建
通过导入操作系统模板文件生成新的镜像
使用wget命令导入本地镜像
wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz
导入成功共
docker images | grep new
基于Dockerfile创建
Dockerfile是由一组指令组成的文件Dockerfile结构四部分
- 基础镜像信息
- 维护者信息·镜像操作指令
- 容器启动时执行指令
Dockerfile每行支持一条指令,每条指令可携带多个参数,支持使用以“#”号开头的注释
Dockerfile操作指令
指令 | 含义 |
---|---|
FROM镜像 | 指定新镜像所基于的镜像,第一条指令必须为FROM指令,每创建一个镜像就需要一条FROM指令。 |
MAINTAINER名字 | 说明新镜像的维护人信息 |
RUN命令 | 在所基于的镜像上执行命令,并提交到新的 |
CMD[“要运行的程序“,”参数1,“参数2“] | 指令启动容器时要运行的命令或者脚本,Dockerfile只能有一条CMD命令,如果指定多条则只能最后一条被执行 |
EXPOSE端口号 | 指定新镜像加载到Docker时要开启的端口 |
ENV环境变量变量值 | 设置一个环境变量的值,会被后面的RUN使用 |
ADD 源文件/目录目标文件/目录 | 将源文件复制到目标文件,源文件要与Dockerfile位于相同或者是一个URL(从宿主目录复制到容器中,再复制的过程解压缩) |
COPY 源文件/目录目标文件/目录 | 将本地主机上的文件/目录复制到目标地点,源文件/目录要与Dockerfile在相同的目录中 |
VOLUME[“目录“]数据卷 | 在容器中创建一个挂载点 |
USER用户名/UID | 指定运行容器时的用户 |
WORKDIR路径=cd | 为后续的RUN、CMD、ENTRYPOINT指定工作目录 |
ONBUILD命令 | 指定所生成的镜像作为一个基础镜像时所要运行的命令 |
HEALTHCHECK | 健康检查 |
-v自动创建挂载点
Dockerfile镜像制作
一、创建环境
[root@server1 ~]# mkdir Dockerfile
[root@server1 ~]# cd Dockerfile/
二、准备实验环境
[root@server1 Dockerfile]# vim Dockerfile
FROM centos:7 ###基于的基础镜像
MAINTAINER this is porject ###维护镜像的用户信息
RUN yum -y update ###镜像操作指令:安装Apache软件
RUN yum -y install httpd
EXPOSE 80 ###开启80端口
ADD index.html /var/www/html/index.html ###复制网站首页文件
ADD run.sh /run.sh ###将执行脚本复制到镜像中
RUN chmod 755 /run.sh
CMD ["/run.sh"] ###启动容器时执行脚本
~
[root@server1 Dockerfile]# vim index.html ###编辑网页测试用
<h1> this is zy web </h1>
[root@server1 Dockerfile]# vim run.sh ###编辑启动容器时执行脚本
#!/bin/bash
rm - rf /run/httpd/* ###还原apache环境
exec /usr/sbin/apachectl -D FOREGROUND ###启动apache
[root@server1 Dockerfile]# docker build -t httpd:centos . ###生成镜像(注意末尾有 .),如果没有改镜像文件则会自动下载。
Sending build context to Docker daemon 4.096kB
Step 1/9 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
---> 8652b9f0cb4c
Step 2/9 : MAINTAINER this is porject
---> Running in a28fd90b5c6d
Removing intermediate container a28fd90b5c6d
---> 48821923703e
Step 3/9 : RUN yum -y update
---> Running in aaa37c7c8a99
Successfully built 4931c3f7aacd
Successfully tagged httpd:centos
[root@server1 Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos 4931c3f7aacd 39 seconds ago 476MB
centos 7 8652b9f0cb4c 12 days ago 204MB
镜像制作成功
[root@server1 Dockerfile]# docker run -d -p 1216:80 httpd:centos ###新景象运行容器,开放端口
399e93834f312942de37eebc99bc37bc2f63bc9362f596d521f085e3b3a25b8e
[root@server1 Dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
399e93834f31 httpd:centos "/run.sh" 21 seconds ago Up 21 seconds 0.0.0.0:1216->80/tcp stoic_greider
测试apache网页是否能正常访问
私有仓库建立
[root@server1 Dockerfile]# docker pull registry ###下载私有仓库
Using default tag: latest
latest: Pulling from library/registry
cbdbe7a5bc2a: Pull complete
47112e65547d: Pull complete
46bcb632e506: Pull complete
c1cc712bcecd: Pull complete
3db6272dcbfa: Pull complete
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@server1 Dockerfile]# vim /etc/docker/daemon.json
{
"insecure-registries":["20.0.0.10:5000"], ###添加,私有仓库的IP和对应端口(私有仓库端口号必须为5000)
"registry-mirrors": ["https://2a8dm7qe.mirror.aliyuncs.com"]
}
[root@server1 Dockerfile]# systemctl restart docker ###重启容器
[root@server1 Dockerfile]# docker create -it registry /bin/bash ###创建私有仓库容器
09ecc02c5802fc2b6abb8f1e42aaf5d1b1cbc147c4627096d326882d313138bb
[root@server1 Dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09ecc02c5802 registry "/entrypoint.sh /bin…" 4 seconds ago Created hopeful_lehmann
[root@server1 Dockerfile]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
b4eae6b544cc75078bc64f0252979888991299cb248ca4a0538d80bb24a29dba
宿主机的/data/registry自动创建挂载容器中的/tmp/registry
-v自动创建挂载目录
[root@server1 Dockerfile]# docker tag httpd:centos 20.0.0.10:5000/httpd ###更改镜像名
[root@server1 Dockerfile]# docker push 20.0.0.10:5000/httpd ###上传镜像至私有仓库
The push refers to repository [20.0.0.10:5000/httpd]
ac364b9da6b7: Pushed
bb534f8da52e: Pushed
0b11cdd0f3d9: Pushed
d2a3757bf61d: Pushed
aabf06d4078b: Pushed
174f56854903: Pushed
latest: digest: sha256:442a34230ab7ea8afeca3000325c85a2dd5b269a2ec9dd2c77b96c34690a5401 size: 1574
[root@server1 Dockerfile]# curl -XGET http://20.0.0.10:5000/v2/_catalog ###查看私有仓库日志是否上传成功
{"repositories":["httpd"]}
[root@server1 Dockerfile]# docker images ###删除镜像测试从私有库是否可以下载镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 8652b9f0cb4c 12 days ago 204MB
registry latest 2d4f4b5309b1 5 months ago 26.2MB
[root@server1 Dockerfile]# docker pull 20.0.0.10:5000/httpd ###从私有库下载镜像
Using default tag: latest
latest: Pulling from httpd
2d473b07cdd5: Already exists
23c48aab15a7: Pull complete
f68a0ba39bce: Pull complete
a48475ff9972: Pull complete
ff329e45eae9: Pull complete
320e02795a67: Pull complete
Digest: sha256:442a34230ab7ea8afeca3000325c85a2dd5b269a2ec9dd2c77b96c34690a5401
Status: Downloaded newer image for 20.0.0.10:5000/httpd:latest
20.0.0.10:5000/httpd:latest
[root@server1 Dockerfile]# docker images ###下载成功
REPOSITORY TAG IMAGE ID CREATED SIZE
20.0.0.10:5000/httpd latest 4931c3f7aacd 3 hours ago 476MB
centos 7 8652b9f0cb4c 12 days ago 204MB
registry latest 2d4f4b5309b1 5 months ago 26.2MB
Docker数据卷
[root@server1 Dockerfile]# docker pull centos ###基于centos镜像
Using default tag: latest
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@server1 Dockerfile]# docker run -v /var/www:/data1 --name web1 -it centos /bin/bash
将宿主机目录/var/www挂载到容器中的/data1
[root@0bed8b319709 /]# cd /data1
[root@0bed8b319709 data1]# touch test.txt
[root@server1 ~]# ls /var/www
test.txt
在容器中创建文件,宿主机中查看
Docker数据卷容器
[root@server1 Dockerfile]# docker run --name web11 -v /data1 -v /data2 -it centos:7 /bin/bash ###创建数据卷容器,容器中含两个数据卷提供使用
[root@fac12d2028c4 /]# ll
total 12
-rw-r--r--. 1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Nov 26 06:57 data1
drwxr-xr-x. 2 root root 6 Nov 26 06:57 data2
[root@fac12d2028c4 /]# cd data1
[root@fac12d2028c4 data1]# touch 1.txt
[root@fac12d2028c4 data1]# cd ../data2
[root@fac12d2028c4 data2]# touch 2.txt
[root@server1 Dockerfile]# docker run -it --volumes-from web11 --name db1 centos:7 /bin/bash ###创建另一台容器并关联web100,数据卷使用的为web100,名字为db1
[root@3c6e3ffce155 /]# ll
total 12
-rw-r--r--. 1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x. 2 root root 19 Nov 26 07:03 data1
drwxr-xr-x. 2 root root 19 Nov 26 07:03 data2
[root@server1 Dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c6e3ffce155 centos:7 "/bin/bash" 5 minutes ago Exited (127) 4 seconds ago db1
fac12d2028c4 centos:7 "/bin/bash" 14 minutes ago Exited (0) 7 minutes ago web100
实现容器与容器之间数据共享
端口映射
[root@server1 Dockerfile]# docker run -d -P 20.0.0.10:5000/httpd
84139f6c74f84cce6f705f28a5bdf64a9f3d573982c310690b83cf920723d96b
[root@server1 Dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84139f6c74f8 20.0.0.10:5000/httpd "/run.sh" 20 seconds ago Up 19 seconds 0.0.0.0:32768->80/tcp unruffled_liskov
-P随机端口号,从32768开始延续
容器互联
[root@server1 Dockerfile]# docker run -it -P --name web11 centos:7 /bin/bash ###创建并运行容器取名为web11,端口号自动映射
[root@660ce9dde010 /]# exit
[root@server1 Dockerfile]# docker run -it -P --name web22 --link web11:web11 centos:7 /bin/bash ###创建并运行容器取名为web12,端口号自动映射,并于web11建立链接(--link)
[root@b4e61f55a13b /]# ping web11
PING web11 (172.17.0.5) 56(84) bytes of data.
64 bytes from web11 (172.17.0.5): icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from web11 (172.17.0.5): icmp_seq=2 ttl=64 time=0.032 ms
^C
--- web11 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.032/0.051/0.070/0.019 ms