Docker镜像简介
Docker的镜像概念类似于虚拟机里的镜像,是一个只读的模板,一个独立的文件系统,包括运行容器所需的数据,可以用来创建新的容器。
例如:一个镜像可以包含一个完整的 ubuntu 操作系统环境,里面仅安装了Mysql或用户需要的其它应用程序。
Docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统被称为UnionFS。镜像可以基于Dockerfile构建,Dockerfile是一个描述文件,里面包含若干条命令,每条命令都会对基础文件系统创建新的层次结构。
Docker 提供了一个很简单的机制来创建镜像或者更新现有的镜像,用户甚至可以直接从其他人那里下载一个已经做好的镜像来直接使用。
Docker镜像的分层
基于已有镜像创建
[root@localhost ~]# docker create -it centos:7 /bin/bash
3c828e25c6c0d1915e416b69be0b8fd2a18d721a3efb9b7f3a74d48317db2ce8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c828e25c6c0 centos:7 "/bin/bash"
[root@localhost ~]# docker commit -m "new" -a "yang" 3c828e25c6c0 yang:new
sha256:c59e1db4fa34fa800c67bfbea73cc6cc7a053b4c99e71589ca25547434506ee3
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yang new c59e1db4fa34 1 second ago 204MB
基于Dockerfile创建
[root@localhost opt]# mkdir apache
[root@localhost opt]# cd apache
[root@localhost apache]# vim Dockerfile
FROM centos:7
MAINTAINER alis
RUN yum -y update
RUN yum -y install httpd
EXPOSE 80
ADD index.html /var/www/html/index.html
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
[root@localhost apache]# vim run.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND
[root@localhost apache]# echo "this is web test" > index.html
[root@localhost apache]# docker build -t httpd:centos .
[root@localhost apache]# docker run -d -p 1218:80 httpd:centos
616f0ecafa7e8df442574a7b9ff888a4538f1963969d6266bbce55d1c0c0e15d
docker: Error response from daemon: driver failed programming external connectivity on endpoint quirky_stonebraker (2bc24ad42bd3bbd336e31bb8e9bcb72d4b01ad398de70e55546941a8d2713a39): Bind for 0.0.0.0:1218 failed: port is already allocated.
这里报错了
报错处理:
Error response from daemon: driver failed programming external connectivity on endpoint quirky_stonebraker (2bc24ad42bd3bbd336e31bb8e9bcb72d4b01ad398de70e55546941a8d2713a39): Bind for 0.0.0.0:1218 failed: port is already allocated.
这里可以看见1216端口已经被占用了
[root@localhost apache]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbe739848eaf httpd:centos "/run.sh" About a minute ago Created inspiring_galileo
c253d408cb01 httpd:centos "/run.sh" 3 minutes ago Created magical_bell
3c828e25c6c0 centos:7 "/bin/bash" About an hour ago Created busy_banach
f075c5779cc3 nginx:latest "/docker-entrypoint.…" 3 hours ago Up 3 hours 80/tcp quirky_rubin
7b0f72d38fcb compose_nginx_nginx "/run.sh" 5 hours ago Up 5 hours 0.0.0.0:1216->80/tcp, 0.0.0.0:1218->443/tcp compose_nginx_nginx_1
这里要换个端口映射
[root@localhost apache]# docker run -d -p 1219:80 httpd:centos
d498ece9e3aabe7555187f9af0346792927c65d874559b3b0f29e41f44089fca
测试
私有仓库
优点:
1.节省网络带宽,针对于每个镜像不用每个人都去中央仓库上面去下载,只需要从私有仓库中下载即可;
2.提供镜像资源利用,针对于公司内部使用的镜像,推送到本地的私有仓库中,以供公司内部相关人员使用。
搭建私有仓库
[root@localhost apache]# docker pull registry ##下载registry
[root@localhost apache]# vim /etc/docker/daemon.json
[root@localhost apache]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://z6o7pl02.mirror.aliyuncs.com"]
"insecure-registries": ["192.168.100.100:5000"]
}
[root@localhost apache]# systemctl restart docker.service ##重启docker服务
[root@localhost apache]# docker create -it registry /bin/bash ##创建容器
启动私有仓库服务
[root@localhost apache]# docker create -it registry /bin/bash
a43ab33e9e22ad087ee22b020cff28294fa3afea905d80667b557a5e34422d8a
[root@localhost apache]# docker run -d -p 5000:5000 registry
617ca873d7bce466ddce57ba2a38abce95698d23755b9959e8918be31297223b
[root@localhost apache]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
617ca873d7bc registry "/entrypoint.sh /etc…" 9 seconds ago Up 8 seconds 0.0.0.0:5000->5000/tcp keen_goldberg
a43ab33e9e22 registry "/entrypoint.sh /bin…" 2 minutes ago Created awesome_robinson
将宿主机的/data/registry自动创建挂载容器中的/tmp/registry
[root@localhost apache]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
2e1e74f712a434296045ad5910b947d024e552ee44c8ebd3a118a5f4b1e435d0
更改标记为192.168.100.13:5000/abc
[root@localhost ~]# docker tag httpd:centos 192.168.100.100:5000/abc
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.100.100:5000/abc latest 3a653fe8d252 2 hours ago 476MB
httpd centos 3a653fe8d252
上传镜像至私有仓库
[root@localhost ~]# docker push 192.168.100.100:5000/abc
The push refers to repository [192.168.100.100:5000/abc]
ca6f3f93a94f: Pushed
90ca45a892e3: Pushed
600504cf2b74: Pushed
e169fe73bb76: Pushed
7b50fcf808b9: Pushed
174f56854903: Pushed
latest: digest: sha256:9892751776f805c657316cc203be1fa338542905c80c48cccb0af5d5f9a47f41 size: 1574
私有仓库下载镜像
[root@localhost ~]# docker pull 192.168.100.100:5000/abc
Using default tag: latest
latest: Pulling from abc
Digest: sha256:9892751776f805c657316cc203be1fa338542905c80c48cccb0af5d5f9a47f41
Status: Image is up to date for 192.168.100.100:5000/abc:latest
192.168.100.100:5000/abc:latest
创建数据卷
[root@localhost ~]# docker run -v /var/www:/data1 --name web1 -it centos:7 /bin/bash
[root@bfd347a190e1 /]# 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 30 10:43 data1
测试
[root@bfd347a190e1 /]# cd data1/
[root@bfd347a190e1 data1]# touch yang
[root@localhost ~]# ll /var/www/
total 0
-rw-r--r--. 1 root root 0 Nov 30 02:46 yang
数据卷容器
#运行一个容器yang并创建两个数据卷目录
[root@localhost ~]# docker run --name yang -v /data1 -v /data2 -it centos:7 /bin/bash
[root@de3532025934 /]# 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 30 10:53 data1
drwxr-xr-x. 2 root root 6 Nov 30 10:53 data2
创建测试文件
[root@de3532025934 /]touch /data1/testa
[root@de3532025934 /]# touch /data2/testb
#运行容器yangzihan,挂载使用yang的数据卷
[root@localhost ~]# docker run -it --volumes-from yang --name yangzihan centos:7 /bin/bash
[root@8170dae8eb52 /]# 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 30 10:53 data1
drwxr-xr-x. 2 root root 19 Nov 30 10:55 data2
#查看是否同步了文件
[root@8170dae8eb52 ~]# ls /data2
testb
[root@8170dae8eb52 ~]# ls /data1
testa
容器互联
使用link可以连接两个容器,使两个容器互通,可以ping通
#运行容器ky06,放入后台,-P,随机指定端口
[root@localhost ~]# docker run -itd -P --name ky06 centos:7 /bin/bash
0369dc7dc2fb84533f218231deb05e28d65c9330378730332d70790aaa754b5a
#运行容器yangzi,链接ky06
[root@localhost ~]# docker run -itd -P --name ky06 centos:7 /bin/bash
0369dc7dc2fb84533f218231deb05e28d65c9330378730332d70790aaa754b5a
[root@localhost ~]# docker run -it -P --name yangzi --link ky06 centos:7 /bin/bash
[root@2ce126bfc8e2 /]# ping ky06
PING ky06 (172.17.0.5) 56(84) bytes of data.
64 bytes from ky06 (172.17.0.5): icmp_seq=1 ttl=64 time=0.113 ms
64 bytes from ky06 (172.17.0.5): icmp_seq=2 ttl=64 time=0.066 ms
64 bytes from ky06 (172.17.0.5): icmp_seq=3 ttl=64 time=0.057 ms