docker数据卷
2023-12-13 09:48:30
容器如何与宿主机之间进行数据共享
容器如何和宿主机进行数据共享
数据卷是一个供容器使用的特殊的目录,在容器中,和宿主机的目录进行映射,主机和宿主机之间都可以对目录中的文件进行修改,双方同步生效,对镜像也没有影响,宿主机到容器实现数据迁移
宿主机的目录和容器中目录进行挂载(映射关系)
[root@docker1 ~]# docker run -itd --name test1 -v /opt/test1:/opt/test nginx:1.22.0 /bin/bash
a335cb5c1211cebe0c06ffaa64de73f3771a17743958d23b273117aedd2e2df4
[root@docker1 ~]# cd /opt/test1/
[root@docker1 test1]# ls
[root@docker1 test1]# echo 123 > 123.txt
[root@docker1 test1]# docker exec -it test1 bash
root@a335cb5c1211:/#
root@a335cb5c1211:/# cd /opt/test/
root@a335cb5c1211:/opt/test# cat 123.txt
123
root@a335cb5c1211:/opt/test#
加上ro(容器只能读)
[root@docker1 test1]# docker run -itd --name test2 -v /opt/test2:/opt/test1:ro nginx:1.22.0 /bin/bash
68390c72c3169ab251db210f605fa277516e61b373477ac8bf831f624e39aa17
[root@docker1 test1]# cd /opt/test2/
[root@docker1 test2]# echo 123 > 1.txt
[root@docker1 test2]# docker exec -it test
test1 test2
[root@docker1 test2]# docker exec -it test2 bash
root@68390c72c316:/# cd /opt/test1/
root@68390c72c316:/opt/test1# cat 1.txt
123
root@68390c72c316:/opt/test1# echo 1 > 1.txt
bash: 1.txt: Read-only file system
root@68390c72c316:/opt/test1#
容器和容器之间实现数据共享
test1 test2 可以有一个或者多个映射目录,实现数据互传,数据同步
创建挂载卷容器
docker run -itd --name test3 -v /opt/data1 -v /opt/data2 nginx:1.22.0 /bin/bash
创建容器使用挂载卷
docker run -itd --volumes-from test3 --name test4 nginx:1.22.0 /bin/bash
[root@docker1 test2]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
583801dc6745 nginx:1.22.0 "/docker-entrypoint.…" 8 seconds ago Up 8 seconds 80/tcp test4
8dc4ec61f469 nginx:1.22.0 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp test3
68390c72c316 nginx:1.22.0 "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp test2
a335cb5c1211 nginx:1.22.0 "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 80/tcp test1
[root@docker1 test2]# docker exec -it test4 bash
root@583801dc6745:/# cd /opt/
root@583801dc6745:/opt# sl
bash: sl: command not found
root@583801dc6745:/opt# ls
data1 data2
root@583801dc6745:/opt# cd data2
root@583801dc6745:/opt/data2# echo 1 > 12
[root@docker1 ~]# docker exec -it test3 bash
root@8dc4ec61f469:/# cd /opt/
root@8dc4ec61f469:/opt# ls
data1 data2
root@8dc4ec61f469:/opt# cd data
bash: cd: data: No such file or directory
root@8dc4ec61f469:/opt# cd data1/
root@8dc4ec61f469:/opt/data1# ls
root@8dc4ec61f469:/opt/data1# cd ..
root@8dc4ec61f469:/opt# cd data2
root@8dc4ec61f469:/opt/data2# ls
12
容器互联
两个容器之间实现网络通信
方法一:
[root@docker1 test2]# docker run -itd --name host1 -P centos:7 /bin/bash
61f66eb7446be8d9e4bd16d25c8e9c2d3987f687866d4c2c859f6615f8e6e97b
[root@docker1 test2]# docker run -itd --name host2 -P --link host1:host2 centos:7 /bin/bash
e0040bcc515510f809dd2d338de1dac74867d5be1ecacce6cbe3573f7b602a69
[root@docker1 test2]# docker exec -it host1 bash
[root@61f66eb7446b /]# yum install -y net-tools
[root@docker1 ~]# docker exec -it host2 bash
[root@e0040bcc5155 /]# yum install -y net-tools
方法二:
先创建一个网段
[root@docker1 ~]# docker network create --subnet 190.0.0.0/16 --opt "com.docker.network.bridge.name"="docker" mydocker
ddd311276e782cefee5f0ab62885a72b64f292f03be8e8a93ea3d0a09fc75951
[root@docker1 ~]# ifconfig
docker: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 190.0.0.1 netmask 255.255.0.0 broadcast 190.0.255.255
ether 02:42:76:bb:56:6e txqueuelen 0 (Ethernet)
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@docker1 ~]# docker run -itd --name xiaobu1 -P --network mydocker centos:7 /bin/bash
435604edf1e3501db53268574adcf62cff1daae59df13b92de6ad81efd1d4483
[root@docker1 ~]# docker run -itd --name xiaobu2 -P --network mydocker centos:7 /bin/bash
1831431ae1fba8e12fcbf1cd0f88853431d74f06b72e87e58ad73d95e9ea9579
通过ping的方式
[root@docker1 ~]# docker exec -it xiaobu1 bash
[root@435604edf1e3 /]# ping xiaobu2
PING xiaobu2 (190.0.0.3) 56(84) bytes of data.
64 bytes from xiaobu2.mydocker (190.0.0.3): icmp_seq=1 ttl=64 time=0.030 ms
64 bytes from xiaobu2.mydocker (190.0.0.3): icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from xiaobu2.mydocker (190.0.0.3): icmp_seq=3 ttl=64 time=0.047 ms
64 bytes from xiaobu2.mydocker (190.0.0.3): icmp_seq=4 ttl=64 time=0.048 ms
64 bytes from xiaobu2.mydocker (190.0.0.3): icmp_seq=5 ttl=64 time=0.047 ms
^C
--- xiaobu2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.030/0.043/0.048/0.009 ms
[root@docker1 test2]# docker exec -it xiaobu2 bash
[root@1831431ae1fb /]# ping xiaobu1
PING xiaobu1 (190.0.0.2) 56(84) bytes of data.
64 bytes from xiaobu1.mydocker (190.0.0.2): icmp_seq=1 ttl=64 time=0.072 ms
64 bytes from xiaobu1.mydocker (190.0.0.2): icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from xiaobu1.mydocker (190.0.0.2): icmp_seq=3 ttl=64 time=0.048 ms
^C
--- xiaobu1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.048/0.056/0.072/0.013 ms
文章来源:https://blog.csdn.net/qq_71147683/article/details/134862885
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!