24.1 Docker
在我们开始了解linux运维开始,我们就听说了docker,非常流行,几乎大多数公司都在使用,但docker具体是什么,有什么作用,我们却不太了解。
docker字面意思就是容器,它是一个开源的容器引擎,能够快速地部署环境和发布代码,可以让开发者打包应用以及依赖的库,然后发布到任何流行的linux发行版上,移植很方便。
由go语言开发,基于apache2.0协议发布,官网:www.docker.com ,GitHub:https://github.com/docker/docker.github.io 。版本分为社区版ce和企业版ee,基于年月的时间线形式,当前最新稳定版为18.06。
docker相对于传统虚拟化的优势:
1. 启动非常快,秒级实现;
2. 资源利用率高,一台高配置服务器可以跑上千个docker容器;
3. 更快的部署和交付,一次创建和配置后,可以在任意地方运行;
4. 内核级别的虚拟化,不需要额外的hypevisor支持,会有更高的性能和效率;
5. 容易迁移,平台依赖性不高。
Docker概念:
- 镜像,是一个只读的模板,类似于安装系统时用到的那个iso文件,我们通过镜像来完成各种应用的部署。
- 容器,镜像类似于操作系统,而容器类似于虚拟机本身,它可以被启动、关闭、删除等操作,每个容器都是相互隔离的。
- 仓库,存放镜像的一个场所,仓库分为公开仓库和私有仓库,最大的公开仓库是Docker hub(hub.docker.com)。
Docker安装
- 安装docker:
# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo #curl -o ,把输入写到该文件中,即把下载下来的文件写入/etc/yum.repos.d/docker.repo中
# yum list |grep docker
docker-ce.x86_64 18.06.1.ce-3.el7 @docker-ce-stable
cockpit-docker.x86_64 172-2.el7.centos extras
containerd.io.x86_64 1.2.0-1.2.beta.2.el7 docker-ce-stable
docker.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos
docker-ce-selinux.noarch 17.03.3.ce-1.el7 docker-ce-stable
docker-client.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos
docker-client-latest.x86_64 1.13.1-58.git87f2fab.el7.centos
docker-common.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos
docker-compose.noarch 1.18.0-1.el7 epel
docker-devel.x86_64 1.3.2-4.el7.centos extras
docker-distribution.x86_64 2.6.2-2.git48294d9.el7 extras
docker-forward-journald.x86_64 1.10.3-44.el7.centos extras
docker-latest.x86_64 1.13.1-58.git87f2fab.el7.centos
docker-latest-logrotate.x86_64 1.13.1-58.git87f2fab.el7.centos
docker-latest-v1.10-migrator.x86_64 1.13.1-58.git87f2fab.el7.centos
docker-logrotate.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos
docker-lvm-plugin.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos
docker-novolume-plugin.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos
docker-python.x86_64 1.4.0-115.el7 extras
docker-registry.x86_64 0.9.1-7.el7 extras
docker-unit-test.x86_64 2:1.13.1-68.gitdded712.el7.centos
docker-v1.10-migrator.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos
golang-github-fsouza-go-dockerclient-devel.x86_64
kdocker.x86_64 4.9-1.el7 epel
pcp-pmda-docker.x86_64 3.12.2-5.el7 base
python-docker-py.noarch 1.10.6-4.el7 extras
python-docker-pycreds.noarch 1.10.6-4.el7 extras
python-docker-scripts.noarch 0.4.4-1.el7 epel
python-dockerfile-parse.noarch 0.0.5-1.el7 epel
python2-avocado-plugins-runner-docker.noarch
python2-dockerpty.noarch 0.4.1-9.el7 epel
python34-docker.noarch 2.6.1-1.el7 epel
python34-docker-pycreds.noarch 0.2.1-1.el7 epel
python34-dockerpty.noarch 0.4.1-9.el7 epel
# yum install -y docker-ce
- 启动docker:
# systemctl start docker
# ps aux |grep docker
root 17893 0.5 5.0 598868 50260 ? Ssl 01:46 0:00 /usr/bin/dockerd
root 17899 0.2 2.7 317288 27692 ? Ssl 01:46 0:00 docker-containerd --config /var/run/docker/containerd/containerd.toml
# iptables -nvL
Chain INPUT (policy ACCEPT 108 packets, 7280 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER-USER all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DOCKER-ISOLATION-STAGE-1 all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 59 packets, 5814 bytes)
pkts bytes target prot opt in out source destination
Chain DOCKER (1 references)
pkts bytes target prot opt in out source destination
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
pkts bytes target prot opt in out source destination
0 0 DOCKER-ISOLATION-STAGE-2 all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0
0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
Chain DOCKER-ISOLATION-STAGE-2 (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * docker0 0.0.0.0/0 0.0.0.0/0
0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
Chain DOCKER-USER (1 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 #每次启动docker都会自动生成iptables规则
镜像管理
- 下载镜像:
# docker pull centos #下载镜像
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Pull complete
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest
- 配置docker加速器:
上面下载镜像很慢,所以我们可以配置一个docker加速器。
# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"] #该url为加速器地址,可自行去阿里云申请自己的加速器地址
}
配置完后,重启docker,之后再下载镜像会比之前快很多。
# systemctl restart docker #重启docker
# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
124c757242f8: Pull complete
9d866f8bde2a: Pull complete
fa3f2f277e67: Pull complete
398d32b153e8: Pull complete
afde35469481: Pull complete
Digest: sha256:de774a3145f7ca4f0bd144c7d4ffb2931e06634f11529653b23eba85aef8e378
Status: Downloaded newer image for ubuntu:latest
- 查看本地镜像:
# docker images #查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
- 搜索镜像:
# docker search jumpserver #搜索镜像,可以搜到很多其他人做的镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
jumpserver/jumpserver 10
jiaxiangkong/jumpserver_docker 开源跳板机(堡垒机):认证,授权,审计,自动化运维 10
hhding/jumpserver-docker ssh proxy node 3 [OK]
njqaaa/jumpserver jumpserver 2 [OK]
baselibrary/jumpserver jumpserver 1 [OK]
zhegao/jumpserver Jumpserver 1.4.0 1
jumpserver/allinone jumpserver all in one 1 [OK]
zqiannnn/jumpserver-ansible JumpServer Ansible Addon 1 [OK]
jumpserver/guacamole guacamole for jumpserver 1 [OK]
kubernetesio/sshd-jumpserver sshd-jumpserver 0 [OK]
vikings/jumpserver 0
satoms/jumpserver 0
zsjohny/jumpserver bastion web ui 0 [OK]
jumpserver/python 0
qiwihui/jumpserver jumpserver docker 0 [OK]
jumpserver/coco 0
qq58945591/jumpserver JumpServer集成coco和luna,使用nginx进行反向代… 0 [OK]
jumpserver/luna 0
lc13579443/jumpserver Jumpserver all in one Dockerfile 0 [OK]
ibuler/jumpserver 0
qbtrade/jumpserver 0
jumpserver/core Jumpserver Official Docker Image 0 [OK]
qbtrade/jumpserver_coco 0
jumpserver/base-env-alpine 0
mapsic/jumpserver jumpserver 0 [OK]
- 给镜像打上标签:
# docker tag centos lzx_centos #给镜像打上标签
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
打完标签会重新另生成一份镜像,不过IMAGE ID不会改变,这表明这两个镜像其实是一样的。
此外还可以这样标识,改变TAG。
# docker tag centos test:123
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
test 123 5182e96772bf 5 weeks ago 200MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
- 把镜像启动为容器:
#启动镜像为容器;-i表示让容器的标准输入打开;-t表示分配一个伪终端;-d表示后台启动,参数需要放到镜像名字前面
# docker run -itd centos
3790ac27cf7ef69a389390ebb11045d89cd05693615fddb74fd28dfe5adfbb2e
# docker ps #查看启动状态的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3790ac27cf7e centos "/bin/bash" 5 seconds ago Up 4 seconds epic_beaver
# docker ps -a # -a查看所有容器,包括启动状态和停止状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3790ac27cf7e centos "/bin/bash" 4 minutes ago Up 4 minutes epic_beaver
- 删除镜像:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
test 123 5182e96772bf 5 weeks ago 200MB
# docker rmi test:123 #删除镜像,删除时必须要带上TAG,否则删除不了
Untagged: test:123
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
删除时,后面的参数可以是TAG,如果是TAG时,实际上删除的是该TAG;当后面的参数为IMAGE ID时,则会删除整个镜像,所有标签也会一同删除。
通过容器创建镜像
镜像可以通过docker pull下载镜像,也可以自己自定义创建镜像。
- 进入容器:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3790ac27cf7e centos "/bin/bash" 27 minutes ago Up 27 minutes epic_beaver
# docker exec -it 3790ac bash #进入容器界面,容器ID可以简写
[root@3790ac27cf7e /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
[root@3790ac27cf7e /]# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 18G 1.8G 17G 10% /
tmpfs 64M 0 64M 0% /dev
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 18G 1.8G 17G 10% /etc/hosts
shm 64M 0 64M 0% /dev/shm
tmpfs 489M 0 489M 0% /proc/acpi
tmpfs 489M 0 489M 0% /proc/scsi
tmpfs 489M 0 489M 0% /sys/firmware #大小和宿主机一致
[root@3790ac27cf7e /]# yum install -y net-tools #容器里可以使用的命令数量有限
[root@3790ac27cf7e /]# 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 #容器网卡有自己的ip
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 1009 bytes 12369051 (11.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 793 bytes 46160 (45.0 KiB)
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 1 (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@3790ac27cf7e /]# exit
exit
# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 #多出来一个docker0网卡
inet6 fe80::42:c6ff:fe3a:47c2 prefixlen 64 scopeid 0x20<link>
ether 02:42:c6:3a:47:c2 txqueuelen 0 (Ethernet)
RX packets 793 bytes 35058 (34.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1001 bytes 12368403 (11.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.33.150 netmask 255.255.255.0 broadcast 192.168.33.255
inet6 fe80::84c7:fae0:d9f5:d9c0 prefixlen 64 scopeid 0x20<link>
inet6 fe80::b6f9:83f6:f7f2:ece0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:42:1c:de txqueuelen 1000 (Ethernet)
RX packets 130382 bytes 181561741 (173.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 33277 bytes 2573534 (2.4 MiB)
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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 1145 bytes 133845 (130.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1145 bytes 133845 (130.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth86bb548: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #每开启一个容器就多出来一个虚拟网卡
inet6 fe80::3c54:9eff:fea2:50ee prefixlen 64 scopeid 0x20<link>
ether 3e:54:9e:a2:50:ee txqueuelen 0 (Ethernet)
RX packets 793 bytes 46160 (45.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1009 bytes 12369051 (11.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 生成镜像:
#-m指定改动信息;-a指定作者相关信息;3790ac为容器id,后面接新镜像的名字
# docker commit -m "install net-tools" -a "lzx" 3790ac centos_with_net
sha256:ef7d535f820682b1ed041caf74c79fa32df6c26778f34ee18dd1448ac6f012ab
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_with_net latest ef7d535f8206 16 seconds ago 293MB //多出来一个新镜像centos_with_net
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
启动新镜像就可以直接使用net-tools工具。
# docker run -itd centos_with_net bash #启动新镜像为容器
b74a447962c80b2caf79727d39ee5d965a27aad6c6293554870748ac53408131
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" 53 seconds ago Up 52 seconds vigorous_allen
3790ac27cf7e centos "/bin/bash" 45 minutes ago Up 45 minutes epic_beaver
# docker exec -it vigorous_allen bash #进入容器界面,参数后面可以跟容器ID,也可以跟容器名字
[root@b74a447962c8 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 #重新启动的容器ip与之前并不同
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.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 1 (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@b74a447962c8 /]# ping www.qq.com
PING https.qq.com (58.247.214.47) 56(84) bytes of data.
64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=1 ttl=127 time=13.8 ms
64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=2 ttl=127 time=14.6 ms
64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=3 ttl=127 time=13.5 ms
64 bytes from 58.247.214.47 (58.247.214.47): icmp_seq=4 ttl=127 time=16.9 ms
^C
--- https.qq.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3011ms
rtt min/avg/max/mdev = 13.558/14.758/16.972/1.353 ms #宿主机可以联网,容器就可以联网
[root@b74a447962c8 /]# exit
exit
# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:c6ff:fe3a:47c2 prefixlen 64 scopeid 0x20<link>
ether 02:42:c6:3a:47:c2 txqueuelen 0 (Ethernet)
RX packets 805 bytes 35814 (34.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1009 bytes 12369133 (11.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.33.150 netmask 255.255.255.0 broadcast 192.168.33.255
inet6 fe80::84c7:fae0:d9f5:d9c0 prefixlen 64 scopeid 0x20<link>
inet6 fe80::b6f9:83f6:f7f2:ece0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:42:1c:de txqueuelen 1000 (Ethernet)
RX packets 131136 bytes 181625077 (173.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 33771 bytes 2625917 (2.5 MiB)
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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 1165 bytes 134885 (131.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1165 bytes 134885 (131.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth733e1d0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #新生成了一个虚拟网卡
inet6 fe80::2076:95ff:fe66:903c prefixlen 64 scopeid 0x20<link>
ether 22:76:95:66:90:3c txqueuelen 0 (Ethernet)
RX packets 12 bytes 924 (924.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16 bytes 1378 (1.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth86bb548: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::3c54:9eff:fea2:50ee prefixlen 64 scopeid 0x20<link>
ether 3e:54:9e:a2:50:ee txqueuelen 0 (Ethernet)
RX packets 793 bytes 46160 (45.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1010 bytes 12369093 (11.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
通过模板创建镜像
- 下载模板镜像:
# wget https://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz #下载模板
# du -sh centos-7-x86_64-minimal.tar.gz
139M centos-7-x86_64-minimal.tar.gz
- 导入模板镜像:
# cat centos-7-x86_64-minimal.tar.gz|docker import - centos7 #导入模板镜像
sha256:9934306dddc6b438e960de9523228dcc65b5ecd6692ae6a2f55d33f271dd7829
# docker images #查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 latest 9934306dddc6 14 seconds ago 435MB #有刚刚导入的镜像
centos_with_net latest ef7d535f8206 45 minutes ago 293MB
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
- 启动刚导入的镜像:
# docker run -itd centos7 bash #启动镜像为容器,后面加上bash,否则报错
c4aa635a5defb558bc54877eca0213fa3bf9002651065aef6d1795f3a2f19aef
# docker exec -it c4aa63 bash #进入容器界面
[root@c4aa635a5def /]# cat /etc/redhat-release #查看系统版本
CentOS Linux release 7.2.1511 (Core)
- 导出镜像为文件:
# docker save -o centos7.tar centos7 #前面为文件名,后面跟镜像名字
# docker save -o centos7.tar centos7
# ls
anaconda-ks.cfg centos7.tar centos-7-x86_64-minimal.tar.gz
- 用文件恢复本地镜像:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 latest 9934306dddc6 24 minutes ago 435MB
centos_with_net latest ef7d535f8206 About an hour ago 293MB
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
# docker rmi 9934306 #删除镜像
Error response from daemon: conflict: unable to delete 9934306dddc6 (cannot be forced) - image is being used by running container c4aa635a5def #删除报错,提示镜像正在被容器运行
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c4aa635a5def centos7 "bash" 20 minutes ago Up 20 minutes agitated_tesla
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker rm -f c4aa635a5def #强行删除容器
c4aa635a5def
# docker rmi 9934306 #再删除镜像
Untagged: centos7:latest
Deleted: sha256:9934306dddc6b438e960de9523228dcc65b5ecd6692ae6a2f55d33f271dd7829
Deleted: sha256:788edba9eaa8ade63d8ba9d5747281c5da2b34b12a6c80f4dffd8ad9e05f68c1
# docker images #查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_with_net latest ef7d535f8206 About an hour ago 293MB
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
# docker load --input centos7.tar #用文件导入镜像,也可以使用docker load < centos7.tar 来导入镜像
788edba9eaa8: Loading layer 446.1MB/446.1MB
Loaded image: centos7:latest
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 latest 9934306dddc6 32 minutes ago 435MB #有刚刚新导入的镜像
centos_with_net latest ef7d535f8206 About an hour ago 293MB
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
- 上传个人镜像:
# docker push image_name
可以把个人镜像上传到dockerhub官方网站上去,但前提是需要先注册
容器管理
- 创建容器:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker create -it centos7 bash #创建新的容器,但没有运行
95c783a08d9ef31c66948053bdee8d1bd502e9bf2816492cd840acc787acb1e2
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker ps -a #查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95c783a08d9e centos7 "bash" 10 seconds ago Created nifty_davinci #这里状态也显示created
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
- 启动容器:
# docker start 95c783 #启动容器
95c783
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95c783a08d9e centos7 "bash" 2 minutes ago Up 2 seconds nifty_davinci //容器已经启动
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours
- 重启容器:
# docker restart 95c783 #重启容器
95c783
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95c783a08d9e centos7 "bash" 5 minutes ago Up 2 seconds nifty_davinci
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
- 关闭容器:
# docker stop 95c783 #关闭容器
95c783
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95c783a08d9e centos7 "bash" 6 minutes ago Exited (137) 7 seconds ago nifty_davinci #这里状态提示退出
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours
- 进入容器界面:
# docker run -it centos bash #不加-d选项可直接进入容器界面
[root@ee69e21a9360 /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
[root@ee69e21a9360 /]# ifconfig
bash: ifconfig: command not found
[root@ee69e21a9360 /]# exit #退出容器
exit
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" About an hour ago Up About an hour vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee69e21a9360 centos "bash" 2 minutes ago Exited (127) 57 seconds ago modest_williams #exit退出之后容器就不再运行
95c783a08d9e centos7 "bash" 10 minutes ago Exited (137) 4 minutes ago nifty_davinci
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours
- 自定义容器名字:
# docker run -itd --name lzx123 centos bash #自定义容器名字启动
004494c2c9f7e58cdbc23e19af42dd54b49db931975210702112d8c44ef705e6
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
004494c2c9f7 centos "bash" 8 seconds ago Up 7 seconds lzx123 #名字显示为刚自定义的名字
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
# docker exec -it lzx123 bash #通过容器名字进入对应的容器界面
[root@004494c2c9f7 /]#
- 让容器退出后删除:
# docker run --rm -it centos bash -c "sleep 30" #--rm 让容器执行命令后退出且删除容器;-c指定执行的命令
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
004494c2c9f7 centos "bash" 6 minutes ago Up 6 minutes lzx123
ee69e21a9360 centos "bash" 12 minutes ago Exited (127) 11 minutes ago modest_williams
95c783a08d9e centos7 "bash" 21 minutes ago Exited (137) 14 minutes ago nifty_davinci
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 2 hours ago Up 2 hours epic_beaver
- 查看容器运行的历史信息:
# docker run -itd centos bash -c "echo 123"
0d4528e9ba188610a2685cf64ff8971063f26318be2d18a2c699d181e48ce43c
# docker logs 0d452 #查看容器运行的历史信息
123
- 进入后台运行的容器:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
004494c2c9f7 centos "bash" 13 minutes ago Up 13 minutes lzx123
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
# docker attach 004494 #进入一个后台运行的容器
[root@004494c2c9f7 /]# exit
exit
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
可以看到,docker attach不太好用,因为进入后台容器后,用exit退出后容器直接停止运行。相比之下,使用docker exec -it container_id bash进入容器之后,用exit退出后容器仍在运行。
- 删除容器:
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d4528e9ba18 centos "bash -c 'echo 123'" 7 minutes ago Exited (0) 7 minutes ago suspicious_varahamihira
004494c2c9f7 centos "bash" 18 minutes ago Exited (0) 5 minutes ago lzx123
ee69e21a9360 centos "bash" 24 minutes ago Exited (127) 23 minutes ago modest_williams
95c783a08d9e centos7 "bash" 33 minutes ago Exited (137) 26 minutes ago nifty_davinci
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
# docker rm 0d4528e #删除容器
0d4528e
# docker rm 004494
004494
# docker rm ee69e2
ee69e2
# docker rm 95c783
95c783
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
删除正在运行中的容器会报错,除非加上-f选项。
- 导出容器:
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
# docker export b74a44 > 111.tar #导出容器为文件,这样就可以迁移到其它机器上
# ls
111.tar anaconda-ks.cfg centos7.tar centos-7-x86_64-minimal.tar.gz
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74a447962c8 centos_with_net "bash" 2 hours ago Up 2 hours vigorous_allen
3790ac27cf7e centos "/bin/bash" 3 hours ago Up 3 hours epic_beaver
导出之后,正在运行的容器也不会消失。
- 导入容器:
# cat 111.tar |docker import - lzx_test #导入容器为镜像,最后面为自定义的镜像名字
sha256:5926537d3e30ea6d88f543d466e6418a3aa57679e6bd354eac7b2af1029d8067
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lzx_test latest 5926537d3e30 About a minute ago 272MB #有刚刚导入的容器镜像
<none> <none> 531fcbd0fb9d 2 minutes ago 0B
centos7 latest 9934306dddc6 About an hour ago 435MB
centos_with_net latest ef7d535f8206 2 hours ago 293MB
ubuntu latest cd6d8154f1e1 7 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
仓库管理
对于公司来讲,把自己的镜像推送到docker公共仓库显得不太合适,不过我们可以建立自己本地的docker私有仓库。
- 下载registry镜像:
# docker pull registry #registry镜像是docker官方提供的一个镜像,可用来创建本地私有仓库
Using default tag: latest
latest: Pulling from library/registry
d6a5679aa3cf: Pull complete
ad0eac849f8f: Pull complete
2261ba058a15: Pull complete
f296fda86f10: Pull complete
bcd4a541795b: Pull complete
Digest: sha256:5a156ff125e5a12ac7fdec2b90b7e2ae5120fa249cf62248337b6d04abc574c8
Status: Downloaded newer image for registry:latest
- 启动registry镜像为容器:
# docker run -d -p 5000:5000 registry #-p指定映射端口,左边是宿主机监听端口,右边是容器监听端口
f708d00d51022f9a0ffb55619a0556603db8e1e3bbdf1ab79897ab68973b9803
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f708d00d5102 registry "/entrypoint.sh /etc…" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp pedantic_lamport
- 查看私有仓库:
# curl 127.0.0.1:5000/v2/_catalog #查看私有仓库
{"repositories":[]} #现在没有任何镜像
- 上传镜像到私有仓库:
打上标签
# docker tag centos7 192.168.33.150:5000/centos7 #打标签这一步比较重要,前面需要加上宿主机ip及监听端口
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lzx_test latest 5926537d3e30 17 hours ago 272MB
192.168.33.150:5000/centos7 latest 9934306dddc6 19 hours ago 435MB
centos7 latest 9934306dddc6 19 hours ago 435MB
centos_with_net latest ef7d535f8206 19 hours ago 293MB
registry latest 2e2f252f3c88 47 hours ago 33.3MB
ubuntu latest cd6d8154f1e1 8 days ago 84.1MB
centos latest 5182e96772bf 5 weeks ago 200MB
lzx_centos latest 5182e96772bf 5 weeks ago 200MB
修改配置文件
# vim /etc/docker/daemon.json #更改为下面内容,不能添加,需要删除之前的加速器url
{
"insecure-registries": ["192.168.33.150:5000"]
}
# systemctl restart docker #修改配置文件后需要重启docker
# docker start f708d0
f708d0
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f708d00d5102 registry "/entrypoint.sh /etc…" 21 minutes ago Up 4 seconds 0.0.0.0:5000->5000/tcp pedantic_lamport
上传镜像
# docker push 192.168.33.150:5000/centos7 #上传镜像
The push refers to repository [192.168.33.150:5000/centos7]
788edba9eaa8: Pushed
latest: digest: sha256:c6e83c601856c3260c7e5c9789e22e0f40179739dca6e0feac1f596ca0f9452c size: 529
- 查看私有仓库:
# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos7"]} #多出来刚刚新上传的centos7镜像
再上传一个镜像
# docker tag ubuntu 192.168.33.150:5000/ubuntu
# docker push 192.168.33.150:5000/ubuntu
The push refers to repository [192.168.33.150:5000/ubuntu]
8d7ea83e3c62: Pushed
6a061ee02432: Pushed
f73b2816c52a: Pushed
6267b420796f: Pushed
a30b835850bf: Pushed
latest: digest: sha256:a819482773d99bbbb570626b6101fa37cd93a678581ee564e89feae903c95f20 size: 1357
# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos7","ubuntu"]} #可以看到刚刚上传的ubuntu镜像
- 从私有仓库下载镜像:
这里选另外一台机器,因为创建私有仓库总不可能为一台机器服务。新的机器需要安装docker,且启动docker服务。
# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
# yum install -y docker-ce
# systemctl start docker
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
# vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.33.150:5000"]
}
# docker pull 192.168.33.150:5000/centos7 #下载镜像centos7
Using default tag: latest
Error response from daemon: Get https://192.168.33.150:5000/v2/: http: server gave HTTP response to HTTPS client #有报错
上面有报错,查了下,有可能是配置文件和容器没有启动导致。
- 排错:
检查配置文件,发现两台机器配置文件一致,不存在问题,而且宿主机的容器也启动了,到这里我有点疑惑。
我决定宿主机换个映射端口试一下。
# systemctl restart docker
# docker run -d -p 5001:5000 registry #更换映射端口为5001
a3f011dfe5d39586688701274d0f7cc752593c3a49251348bcfe04616632e8eb
# curl 127.0.0.1:5001/v2/_catalog
{"repositories":[]}
# docker tag ubuntu 192.168.33.150:5001/ubuntu
# vim /etc/docker/daemon.json #修改为下面内容
{
"insecure-registries": ["192.168.33.150:5001"]
}
# systemctl restart docker
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3f011dfe5d3 registry "/entrypoint.sh /etc…" About a minute ago Exited (2) 17 seconds ago vibrant_shannon
bb193bc12150 registry "/entrypoint.sh /etc…" 3 minutes ago Exited (2) 2 minutes ago jolly_joliot
98f9faf1796d centos "/bin/bash" 44 minutes ago Exited (137) 12 minutes ago condescending_northcutt
d0e1e06bc0c7 ubuntu "/bin/bash" About an hour ago Exited (0) 13 minutes ago cocky_turing
503e851a720f centos "/bin/bash" 2 hours ago Exited (137) 2 hours ago amazing_hodgkin
ad4534b0f956 centos_with_net "/bin/bash" 2 hours ago Exited (137) 2 hours ago xenodochial_torvalds
f708d00d5102 registry "/entrypoint.sh /etc…" 2 hours ago Exited (2) 13 minutes ago pedantic_lamport
b74a447962c8 centos_with_net "bash" 21 hours ago Exited (137) 19 hours ago vigorous_allen
3790ac27cf7e centos "/bin/bash" 22 hours ago Exited (137) 19 hours ago epic_beaver
# docker start a3f011
a3f011
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3f011dfe5d3 registry "/entrypoint.sh /etc…" 2 minutes ago Up 3 seconds 0.0.0.0:5001->5000/tcp vibrant_shannon
# docker push 192.168.33.150:5001/ubuntu
The push refers to repository [192.168.33.150:5001/ubuntu]
8d7ea83e3c62: Pushed
6a061ee02432: Pushed
f73b2816c52a: Pushed
6267b420796f: Pushed
a30b835850bf: Pushed
latest: digest: sha256:a819482773d99bbbb570626b6101fa37cd93a678581ee564e89feae903c95f20 size: 1357
# curl 127.0.0.1:5001/v2/_catalog
{"repositories":["ubuntu"]}
新机器上修改配置文件
# systemctl start docker
# ps aux |grep docker
root 1273 0.6 5.2 592596 52092 ? Ssl 01:20 0:00 /usr/bin/dockerd
root 1279 0.5 2.6 382824 26092 ? Ssl 01:20 0:00 docker-containerd --config /var/run/docker/containerd/containerd.toml
root 1419 0.0 0.0 112704 964 pts/0 R+ 01:20 0:00 grep --color=auto docker
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
# vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.33.150:5001"]
}
# systemctl restart docker
# docker pull 192.168.33.150:5001/ubuntu #从私有仓库下载镜像
Using default tag: latest
latest: Pulling from ubuntu
124c757242f8: Pull complete
9d866f8bde2a: Pull complete
fa3f2f277e67: Pull complete
398d32b153e8: Pull complete
afde35469481: Pull complete
Digest: sha256:a819482773d99bbbb570626b6101fa37cd93a678581ee564e89feae903c95f20
Status: Downloaded newer image for 192.168.33.150:5001/ubuntu:latest
# docker images #本地镜像就是刚刚下载的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.33.150:5001/ubuntu latest cd6d8154f1e1 8 days ago 84.1MB
上面可以看到,换了映射端口之后,从私有仓库下载镜像成功。
数据管理
容器是由镜像启动的,如果容器里面产生了新的数据,那么当容器关闭或删除时,这些数据也会一并删除,对于数据是存在一定风险的。
为了避免容器中新产生的数据被删除,我们可以挂载本地的目录到容器里。
- 挂载本地目录到容器:
#挂载本地目录到容器,-v指定挂载目录,左边为宿主机本地目录,右边为容器里目录,会在容器中自动创建
# docker run -itd -v /data:/data centos_with_net bash
f6f4ef99fae3bc2ef9e87304ebb2231a87db0ea3ac20bd6aa6148f0fe0ce7bad
# ls /data/
svnroot
# docker exec -it f6f4e bash
[root@f6f4ef99fae3 /]# ls -l /data/
total 0
drwxr-xr-x 3 root root 23 Aug 31 08:18 svnroot
[root@f6f4ef99fae3 /]# mkdir /data/123 #容器里面创建新目录
[root@f6f4ef99fae3 /]# exit
exit
# ls -l /data/
total 0
drwxr-xr-x 2 root root 6 Sep 14 02:02 123 #宿主机里面有新目录产生
drwxr-xr-x 3 root root 23 Aug 31 04:18 svnroot
挂载目录时可以指定容器名字,如果不指定,会随机生成一个名字。
- 挂载数据卷:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6f4ef99fae3 centos_with_net "bash" 29 minutes ago Up 29 minutes determined_goodall #随机生成的容器名字
a3f011dfe5d3 registry "/entrypoint.sh /etc…" 2 hours ago Up 2 hours 0.0.0.0:5001->5000/tcp vibrant_shannon
开启新的容器
# docker run -itd --volumes-from determined_goodall centos7 bash #挂载数据卷,使用上面的容器作为数据卷容器打开新的容器
4b9edc313afdad51f288605d7c05cade963b474a61afe15e6400661114164075
# docker exec -it 4b9edc bash
[root@4b9edc313afd /]# ls -l /data/ #下面可以看到新容器的目录与数据卷容器的目录是相同的
total 0
drwxr-xr-x 2 root root 6 Sep 14 02:02 123
drwxr-xr-x 3 root root 23 Aug 31 04:18 svnroot
- 定义数据卷容器:
有时候,我们需要多个容器相互共享数据,类似于linux里面的NFS,所以可以搭建一个专门的数据卷容器,然后其它容器直接挂载该数据卷。
- 先建立数据卷容器:
docker run -itd -v /data/ --name tetsvol centos bash
#这里的/data/是容器的/data目录,并非宿主机的/data目录;testvol是自定义的数据卷容器名字
- 然后让其它容器挂载该数据卷:
docker run -itd --volumes-from testvol centos7 bash
#这里是centos7镜像的容器挂载centos镜像的容器目录
另外,如果每台机器想要挂载的数据卷不一样,可以做软链接,对想要挂载的目录做软链接,链接到同一个指定的目录即可。
数据卷备份恢复
- 数据卷备份:
首先我们想要备份的数据在数据卷中,新开一个容器挂载数据卷,同时需要把本地目录挂载到新开的容器目录中,这样在新的容器目录中新建的数据我们就可以直接在本地目录中看到,接下来只要把要备份的数据打包放到新容器中目录下就可以了。
mkdir /data/backup #本地新建挂载目录
docker run --volumes-from testvol -v /data/backup/:/backup centos tar cvf /backup/data.tar /data
# testvol为数据卷容器名字;-v 指定挂载目录;centos 为新的容器镜像;tar打包,/data为要备份的目录
- 数据卷恢复:
过程与上面相反,先建立一个数据卷容器,再建立一个新的容器并挂载该数据卷容器,然后再把tar包放到挂载目录下解包。
docker run -itd -v /data/ --name testvol2 centos bash #新建数据卷容器
docker run --volumes-from testvol2 -v /data/backup:/backup centos tar xvf /backup/data.tar #挂载数据卷新建容器,并解包
更多资料参考: