前言:这段时间没有前言哦
目录
管理Docker 容器中数据主要有两种方式:数据卷(Data Volumes)和数据卷容器( DataVolumes Containers) 。
docker pull centos:7
- docker run -v /var/www:/data1 --name web1 -it centos:7 #-v选项可以在容器内创建数据卷
- ls
- echo "this is hehe" > /data1/abc.txt
- exit
-
- #返回宿主机进行查看
- cat /var/www/1.txt
- #创建一个容器作为数据卷容器
- docker run -it --name hehe1 -v /data1 -v /data2 centos:7 bash #创建并进入容器
- echo "this is hehe" > /data1/1.txt #容器内创建测试文件1
- echo "THIS IS jiajia" > /data2/2.txt #容器内创建测试文件1
-
- #使用--volumes-from来挂载test2容器中的数据卷到新的容器
- docker run -it --name hehe3 --volumes-from hehe1 centos:7 bash #创建并进入容器
- cat data1/1.txt #查看测试数据是否同步
- cat data2/2.txt
- docker run -d --name test1 -P nginx #随机映射端口(从32768开始)
- docker run -d --name test2 -p 43000:80 nginx #指定映射端口
- docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- b184b7cde489 nginx "/docker-entrypoint.…" 38 seconds ago Up 38 seconds 0.0.0.0:43000->80/tcp, :::43000->80/tcp test2
- ae1bc1705d3d nginx "/docker-entrypoint.…" 58 seconds ago Up 56 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp test1
-
- 浏览器访问: http://192.168.226.60:43000. http://192.168.226.60:49153
- #查看容器
- [root@zhang ~]# docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS
- 158957809fc2 centos:7 "bash" 13 minutes ago Up 12 minutes c1
- 4523b97e73ad centos:7 "/bin/bash" 42 minutes ago Up 13 minutes c2
- b5bee0279cad nginx:latest "/docker-entrypoint.…" 51 minutes ago Created admiring_golick
-
-
- #进入c1,ping c2发现ping不通
- [root@zhang ~]# docker exec -it c1 bash
- [root@ce7ebeacc41d /]# ping c2
- ping: c2: Name or service not known
- [root@ce7ebeacc41d /]# exit
- exit
-
- #创建并运行接收容器取名c3,使用--1ink选项指定连接容器以实现容器互联
- #--1ink容器名:连接的别名
- [root@zhang ~]# docker run -itd -P --name c3 --link c2:c2 centos:7 /bin/bash
- c4adb8c6e0e802f5119bccebfb1a95b1ca344644aaf54b8501bd98350617ee46
-
- #进入c3,ping c2 可以ping通
- [root@zhang ~]# docker exec -it c3 bash
- [root@c4adb8c6e0e8 /]# ping c2
- PING c2 (172.17.0.3) 56(84) bytes of data.
- 64 bytes from c2 (172.17.0.3): icmp_seq=1 ttl=64 time=0.074 ms
- 64 bytes from c2 (172.17.0.3): icmp_seq=2 ttl=64 time=0.246 ms
- 64 bytes from c2 (172.17.0.3): icmp_seq=3 ttl=64 time=0.167 ms
- ^C
- --- c2 ping statistics ---
- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms
- rtt min/avg/max/mdev = 0.074/0.162/0.246/0.071 ms
- [root@c4adb8c6e0e8 /]# exit
- exit
-
- #进入c3容器,查看ip
- [root@zhang ~]# docker exec -it c3 bash
- [root@c4adb8c6e0e8 /]# ping 172.17.0.3
- PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
- 64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.050 ms
- 64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.120 ms
- 64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.116 ms
- ^C
- --- 172.17.0.3 ping statistics ---
- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms
- rtt min/avg/max/mdev = 0.050/0.095/0.120/0.033 ms
- [root@c4adb8c6e0e8 /]# exit
- exit
- [root@zhang ~]# docker exec -it c2 bash
- [root@48f1749e6be4 /]# yum install -y net-tools
- ...........
-
- [root@48f1749e6be4 /]# ifconfig
- eth0: flags=4163
mtu 1500 - inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
- ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
- RX packets 4468 bytes 24538555 (23.4 MiB)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 3297 bytes 181507 (177.2 KiB)
- TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-
- lo: flags=73
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
本文介绍了通过数据卷和数据卷容器对容器内数据进行共享、备份和恢复等操作,通过这些机制,即使容器在运行中出现故障,用户也不必担心数据发生丢失,只需要快速地重新创建容器即可。在生成容器的同时,加上-v选项,指定把当前服务器的目录映射到容器中。
命令 | 说明 |
docker run -v 数据卷 | 创建数据卷 |
docker run -v 宿主机目录:数据卷 | 挂载宿主机目录 |
docker run --volumes-from 数据卷容器 | 挂载数据卷容器(挂载点路径不变) |
docker run --link 源容器名:别名 | 容器互联 |