Docker data volume management + container interconnection + port mapping

Overview

When a container is used, some logs or other files will be generated, or we need to back up the data in the container, or even share data between multiple containers, which will involve the data management operation of the container.
There are two main ways to manage data in a container: **1. Data volume** 2. Data volume container
Data volume: mount the storage space of the host system
Data volume container: mount the storage space of the container
if the user needs between the containers The easiest way to share some continuously updated data is to use a data volume container. The data volume container is actually an ordinary container, specifically used to provide data volumes for other containers to mount

Insert picture description here

bring it on! Show! !

Data volume operations

Mount the host directory
to share data between the container and the host

[root@5centos /]# docker run -v /tset1:/doctest1 --name web1 -it 20.0.0.5:5000/httpd /bin/bash
[root@d7ef3f247970 /]# cd doctest1/
[root@d7ef3f247970 doctest1]# touch abc.mp4

宿主机验证
[root@5centos /]# cd tset1/
[root@5centos tset1]# ls
abc.mp4

Data volume container

Container and container data sharing
Create data volume container test50

[root@5centos /]# docker run --name test50 -v /data1 -v /data2 -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
[root@de27be0d57bb /]# ls
bin    data2  etc   lib    lost+found  mnt  proc  run	srv  tmp  var
data1  dev    home  lib64  media       opt  root  sbin	sys  usr
[root@de27be0d57bb /]# cd data1
[root@de27be0d57bb data1]# touch 123.avi
[root@de27be0d57bb data1]# cd /data2
[root@de27be0d57bb data2]# touch 456.avi

Create a new container test60 and mount the data volume container directory

[root@5centos ~]# docker run -it --volumes-from test50 --name test60 centos /bin/bash
[root@a40a840da387 /]# ls
bin    data2  etc   lib    lost+found  mnt  proc  run	srv  tmp  var
data1  dev    home  lib64  media       opt  root  sbin	sys  usr
[root@a40a840da387 /]# cd data1
[root@a40a840da387 data1]# ls
123.avi
[root@a40a840da387 data1]# cd /data2
[root@a40a840da387 data2]# ls
456.avi

Port Mapping

-p specifies port 2080 to map internal 80

[root@5centos ~]# docker run -d -p 2080:80 httpd:centos 
6df38d1adcbeb677cd66dc2f25df35a1ba924ab0fad6113860a52481fab45097

-P random port

[root@5centos ~]# docker run -d -P httpd:centos 
0b01169861db122032bb512e23977fd8660fd5c7a7485f0df558cac089b9c5fc

Container interconnection

创建容器 apa01,端口随机
[root@5centos /]# docker run -itd -P --name apa01 centos:8 /bin/bash
4c1f79e9d7cea3f747975b79388ae3a6bb41df81d8bbb31a7f8a5f16f7993c1b

创建容器 apa02,端口随机,a1是别名,随便取,记得住就行
[root@5centos /]# docker run -itd -P --name apa02 --link apa01:a1 centos:8 /bin/bash
ae9d8131a2689be66393174eb9f6a6e1a5c36abe50400bdffe47198a33b0fdb8

Enter the container, ping test
apa01

[root@5centos /]# docker exec -it apa01 /bin/bash
[root@4c1f79e9d7ce /]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
40: eth0@if41: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

apa02

[root@ae9d8131a268 /]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
42: eth0@if43: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

test

[root@ae9d8131a268 /]# 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.111 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from 172.17.0.3: icmp_seq=4 ttl=64 time=0.061 ms
^C
--- 172.17.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.059/0.072/0.111/0.023 ms

Already communicate!

Guess you like

Origin blog.csdn.net/Ora_G/article/details/108700436