Docker data management and network communication, port mapping

Bibliography: 1. Data management of docker 1. Data Volume...
1. Data Volume
2. Data Volume Container
1. Port Mapping
2. Container interconnection

Bibliography:

1. Data management of docker

1. Data Volume

2. Data Volume Container

2. Network communication of docker

1. Port Mapping

2. Container interconnection

3. Deploy and publish MySQL services in containers

IV. Deploy and publish Apache services in containers

1. Data management of docker

In docker, to view the data generated in a container easily or share data among multiple containers, data management operations of containers are involved. There are two main ways to manage data in a docker container: data volume and data volume container.

1. Data Volume

A data volume is a special directory for containers, in which the host's directory can be mounted onto the data volume. Modifications to the data volume are immediately visible, and updating the data does not affect the image, thus enabling data to migrate between the host and the container. The use of data volumes is similar to mounting directories on Linux (note: hosting will be done)Machine-local directories are mounted in containers, for example, if the host-local/data directory is mounted with/dev/sdb1, then when mapping/data to a data volume, the directory specified in the container uses the same file system as/dev/sdb1. I don't know how this is explained, and you can understand how it works.

1) Mount the host directory as an example of a data volume:

With the -v option, you can create a data volume (just create a directory when you run the container), create a data volume and mount the host's directory onto the data volume for use in data migration between the host and the container.

It is important to note that the path to the host local directory must be absolute, and Docker will automatically create the path if it does not exist.

[root@centos01 ~]# Docker images <!--View Docker host mirror--> REPOSITORY TAG IMAGE ID CREATED SIZE hub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 3 years ago 602 MB [root@centos01 ~]# docker run -d --name centos6.701 -v /data1 hub.c.163.com/public/centos:6.7-tools <!--Create a data volume named centos6.701--> b85a2d8419a98756369ddc3b78247d3d42c178e8e563a936fe973f2f6611f951 [root@centos01 ~]# Docker PS <!--View running containers--> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b85a2d8419a9 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 32 seconds ago Up 32 seconds 22/tcp centos6.701 [root@centos01 ~]# Ls/var/lib/docker/volumes/ <!--View running data volumes--> fbc3bde69b9eaf6410d7e52ba412ab07bbfe49e83315d245dbcf44bf2aa6b91a metadata.db [root@centos01 ~]# DD if=/dev/zero of=. /1.iso bs=10M count=10 <!--Create a test data--> [root@centos01 ~]# cp 1.iso /var/lib/docker/volumes/ fbc3bde69b9eaf6410d7e52ba412ab07bbfe49e83315d245dbcf44bf2aa6b91a/_data/ <!--Host shares data with container--> [root@centos01 ~]# Docker exec-it centos6.701/bin/bash <!--Log on to centos6.701 container--> [root@b85a2d8419a9 /]# Ls data1/ <!--Check to see if the data volume has host-shared data--> 1.iso

2. Data Volume Container

If you need to share some data between containers, the easiest way is to use a data volume container.A data volume container is a common container that specifically provides data volumes for mounting other containers.First, you need to create a container to serve as the data volume container, then mount the data volume in the data volume container with--volumes-front when other containers are created.

1) Examples of creating and using data volume containers:

[root@centos01 ~]# docker run -it -d --name datasrv -v /data1 -v /data2 hub.c.163.com/public/centos:6.7-tools /bin/bash 6c2e89e442fba34821f044d1b589ef70cd2c4d775f83084ce0d7bb8a331c0a55 <!--Create a container that runs under the name datasrv,And create two data volumes: data1 and data2--> [root@centos01 ~]# Docker exec-it datasrv/bin/bash <!--Enter the created container--> [root@6c2e89e442fb /]# Ls | grep data <!--Check to see if there is a corresponding data volume--> data1 data2 [root@6c2e89e442fb /]# Exit <!--Exit the current container--> exit [root@centos01 ~]# docker run -it -d --volumes-from datasrv --name lyx hub.c.163.com/public/centos:6.7-tools 56fff3838cb714369d99e30ea6eebc72e0c6ca1aa11b5e66f3c403ca74c2460e <!--Run a program named lyx Container, using--volumes-from To put datasrv Data volumes in containers are mounted to this lyx On New Container--> [root@centos01 ~]# Docker exec-it lyx/bin/bash <!--Enter the newly created lyx container--> [root@56fff3838cb7 /]# Ls | grep data <!--Check to see if the new container can see the data volume provided by datasrv--> data1 data2 [root@56fff3838cb7 /]# echo "www.lyx.com" > /data1/lyx.txt <!--stay lyx In-container direction data1 Directory Write File for Testing--> [root@56fff3838cb7 /]# Exit <!--Exit the container--> exit [root@centos01 ~]# Docker exec-it datasrv/bin/bash <!--Enter the datasrv container that provides the data volume--> [root@6c2e89e442fb /]# Cat/data1/lyx.txt <!--You can see the file you just created in the lyx container--> www.lyx.com [root@6c2e89e442fb /]# Exit <!--Exit the container--> exit [root@centos01 ~]# cd /var/lib/docker/volumes/ <!--stay docker Host view just in lyx Container-created data--> [root@centos01 volumes]# ls cd27325467149a236d16bd8f4dcdf84c7c6018fd800e2f6d7e04980119390428 metadata.db [root@centos01 volumes]# [root@centos01 volumes]# ls cd27325467149a236d16bd8f4dcdf84c7c6018fd800e2f6d7e04980119390428/_data/ lyx.txt

Note that the most important thing in the production environment is the reliability of storage and the dynamic scalability of storage. It is important to take this into account when making data volumes. The GFS file system is also an excellent one. I just made a simple configuration above. If you are in the production environment, you must take good account, such as the mirror volume container above, you can host it.When you mount the GFS file system locally on your machine and then create a mirror volume container, map the directory where you mount the GFS to the mirror volume in the container. This is a qualified container for mirrored volumes.

2. Network communication of docker

1. Port Mapping

docker provides a mechanism to map container ports to host and container interconnection to provide network services to containers.

When starting a container, services inside the container cannot be accessed over the network outside the container without specifying the corresponding port.docker provides a port mapping mechanism to provide services in a container to external networks, essentially mapping the host's ports to the container so that the ports of the external network accessing the host can access the services in the container.

To implement port mapping, you need to use the -P (uppercase) option to implement random mapping when running the docker run command. Dockers are generally randomly mapped to a port that accesses ports between 49,000 and 49,900 to open network ports inside containers, but this is not absolute, and there are exceptions that do not map to this range; you can also use the Docker when runningUse the -p (lowercase) option when running commands to specify the ports to map (this method is commonly used).

1) Examples of port mappings:

[root@centos01 ~]# docker run -d -P --name ssh hub.c.163.com/public/centos:6.7-tools <!--To Container ssh Port 22 maps to any of the hosts IP Address and random port--> ff30d37776877e47a3c39b932610f93b74dd48e73260de092f882ac76e8a052f [root@centos01 ~]# docker run -d -p 49000:22 --name ssh1 hub.c.163.com/public/centos:6.7-tools <!--To Container ssh Port 22 maps to the port specified by the host with a mapping port range of 49000~49900--> 2fb5469b67e01fc3fcf653e31dd0462dd9ca484849c34544a7bb55143adc608f [root@centos01 ~]# docker run -d -p 192.168.100.10:1111:22 --name ssh2 hub.c.163.com/public/centos:6.7-tools <!--To Container ssh Port 22 maps to host specified IP Address and specified port number--> 3ee8614b50a777726f1bd857f2b1c57be70b3a06846c3c35de8610511873c962 [root@centos01 ~]# docker run -d -p 192.168.100.10::22 --name ssh3 hub.c.163.com/public/centos:6.7-tools <!--To Container ssh Port 22 maps to host assignment IP On any port of the address--> 9de4b73e11db5e25fd9511f7708f812de369673bae79c76880e5493723fcd918 [root@centos01 ~]# docker run -d -p 192.168.100.10:1234:22/tcp --name ssh4 hub.c.163.com/public/centos:6.7-tools <!--Map port numbers for container-specific protocols to host-specified IP Address and specified port--> ceec5bc53037345d5822a29830b38158d8a0add7b3584e36371a34a0851de043 [root@centos01 ~]# Docker port ssh4 <!--View specific port mapping information--> 22/tcp -> 192.168.100.10:1234 [root@centos01 ~]# Docker PS <!--Check to see if the mapping was successful--> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ceec5bc53037 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 3 minutes ago Up 3 minutes 192.168.100.10:1234->22/tcp ssh4 9de4b73e11db hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 4 minutes ago Up 4 minutes 192.168.100.10:32769->22/tcp ssh3 3ee8614b50a7 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 4 minutes ago Up 4 minutes 192.168.100.10:1111->22/tcp ssh2 2fb5469b67e0 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 5 minutes ago Up 5 minutes 0.0.0.0:49000->22/tcp ssh1 ff30d3777687 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 5 minutes ago Up 5 minutes 0.0.0.0:32768->22/tcp ssh

2. Container interconnection

Container interconnection is achieved by establishing a dedicated network communication tunnel between containers by the name of the container.Simply put, a tunnel is created between the source container and the receiving container, where the receiving container can see the information specified by the source container.

When running the docker run command, use the --link option to achieve interconnected communication between containers in the following format:

--link name: alias #Where name is the name of the container to connect to and alias is the alias for the connection.

Container interconnection is performed by the name of the container. The --name option creates a friendly name for the container. This name is unique. If you have named a container with the same name, you need to use the docker rm command to delete the container with the same name you created earlier when you want to use this name again.

1) Examples of container interconnection:

[root@localhost ~]# Docker run-it-d-P --name web1 docker.io/httpd/bin/bash <!--run container web1--> c88f7340f0c12b9f5228ec38793e24a6900084e58ea4690e8a847da2cdfe0b [root@localhost ~]# docker run -it -d -P --name web2 --link web1:web1 docker.io/httpd /bin/bash <!--Run Container web2,And associate web1 container--> c7debd7809257c6375412d54fe45893241d2973b7af1da75ba9f7eebcfd4d652 [root@localhost ~]# Docker exec-it web2/bin/bash <!--Enter web2 container--> root@c7debd780925:/usr/local/apache2# cd root@c7debd780925:~# Ping web1 <!--ping web1--> bash: ping: command not found <!--sorry,No hint ping Command, download one--> root@c7debd780925:~#Apt-get update <!--Update--> root@c7debd780925:~#Apt install iputils-ping <!--Install Ping command--> root@c7debd780925:~#Apt install net-tools <!--This is the command to install ifconfig, you can not install it, I'm just taking a note here--> root@c7debd780925:~# ping web1 <!--Then ping web1--> PING web1 (172.17.0.2) 56(84) bytes of data. 64 bytes from web1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.079 ms 64 bytes from web1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.114 ms ..............<!--Omit some content--> <!--ping Yes, so you can say that these two containers must be interconnected--> <!--If it was created at this time web3 This new container needs to be in harmony with web1,web2 Connect with the following commands:--> [root@localhost ~]# docker run -dit -P --name web3 --link web1:web1 --link web2:web2 docker.io/httpd /bin/bash <!--When running a container, Associate web1 and web2. --> <!--Here is the entry web3--> [root@localhost ~]# docker exec -it web3 /bin/bash root@433d5be6232c:/usr/local/apache2# cd <!--The following is the installation ping command--> root@433d5be6232c:~# apt-get update root@433d5be6232c:~# apt install iputils-ping <!--Here are the pairs web1,web2 Conduct ping test--> root@433d5be6232c:~# ping web1 PING web1 (172.17.0.2) 56(84) bytes of data. 64 bytes from web1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.102 ms 64 bytes from web1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.112 ms ..............<!--Omit some content--> root@433d5be6232c:~# ping web2 PING web2 (172.17.0.3) 56(84) bytes of data. 64 bytes from web2 (172.17.0.3): icmp_seq=1 ttl=64 time=0.165 ms 64 bytes from web2 (172.17.0.3): icmp_seq=2 ttl=64 time=0.115 ms ..............<!--Omit some content--> <!--OK,That's all right.-->
3. Deploy and publish MySQL services in containers
[root@centos01 ~]# Ping www.baidu.com <!--Docker host adds NAT network card to connect public network--> PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data. 64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=1 ttl=128 time=17.6 ms 64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=2 ttl=128 time=22.2 ms 64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=3 ttl=128 time=18.8 ms [root@centos01 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf <!--Turn on routing sharing Docker Container surfing--> [root@centos01 ~]# Sysctl-p <!--Refresh Configuration--> net.ipv4.ip_forward = 1 [root@centos01 ~]# Docker run-d-p 192.168.100.10:3306:3306/tcp --name MySQL hub.c.163.com/public/centos:6.7-tools <!--Create installation MySQL container and publish port number--> ff30d37776877e47a3c39b932610f93b74dd48e73260de092f882ac76e8a052f [root@centos01 ~]# Docker PS | grep mysql <!--View the running mysql container--> c63f5cfd4e02 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 13 seconds ago Up 12 seconds 22/tcp, 192.168.100.10:3306->3306/tcp mysql [root@centos01 ~]# Docker exec-it mysql/bin/bash <!--Log on to MySQL container--> [root@ff30d3777687 /]# Yum-y install MySQL mysql-server <!--MySQL container installs MySQL database--> [root@ff30d3777687 /]# Service mysqld start <!--Start MySQL database--> [root@ff30d3777687 /]# Chkconfig --level 35 mysqld on <!--Set boot-up autostart--> [root@ff30d3777687 /]# mysqladmin -uroot password 'pwd@123' <!--Initialization MySQL Database login password--> [root@ff30d3777687 /]# Mysql-uroot-ppwd@123 <!--Log in to MySQL database--> mysql> grant all on *.* to 'liyanxin'@'192.168.100.10' identified by 'pwd@123'; <!--Authorization Specific IP Address and account password login MySQL In container MySQL data base--> [root@centos01 ~]# Yum-y install MySQL <!--Host install MySQL client--> [root@centos01 ~]# mysql -h 192.168.100.10 -uliyanxin -ppwd@123 <!--Host Logon MySQL Container Published MySQL data base--> MySQL [(none)]> create database liyanxin; <!--Create a liyanxin data base--> [root@centos01 ~]# Docker exec-it mysql/bin/bash <!--host login MySQL container--> [root@ff30d3777687 /]# Mysql-uroot-ppwd@123 <!--Check to see if the newly created database is synchronized--> mysql> show databases; <!--view the database--> +--------------------+ | Database | +--------------------+ | information_schema | | liyanxin | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
IV. Deploy and publish Apache services in containers
[root@centos01 ~]# docker run -d -p 192.168.100.10:80:80/tcp --name httpd hub.c.163.com/public/centos:6.7-tools <!--Create Installation Apache Container and Publish Port Number--> 9de4b73e11db5e25fd9511f7708f812de369673bae79c76880e5493723fcd918 [root@centos01 ~]# Docker PS | grep httpd <!--View running httpd containers--> 836394dc1dd3 hub.c.163.com/public/centos:6.7-tools "/usr/bin/supervisord" 8 seconds ago Up 7 seconds 22/tcp, 192.168.100.10:80->80/tcp httpd [root@centos01 ~]# Docker exec-it httpd/bin/bash <!--Log on to httpd container--> [root@9de4b73e11db /]# Yum-y install httpd <!--Install Apache service in httpd container--> [root@9de4b73e11db /]# echo "www.docker.apache.com" > /var/www/html/index.html <!--Modify home page content--> [root@9de4b73e11db /]# Service httpd start <!--Start apache service--> [root@9de4b73e11db /]# Chkconfig --level 35 httpd on <!--Set boot-up autostart--> [root@9de4b73e11db /]# Tail-f/var/log/httpd/access_log <!--Monitor Apache access log dynamically--> [root@centos01 ~]# curl http://192.168.100.10 <!--docker Host Access httpd In container Apache service--> www.docker.apache.com [root@centos01 ~]# Docker exec-it httpd/bin/bash <!--Log on to httpd container--> [root@9de4b73e11db /]# Tail-f/var/log/httpd/access_log <!--View Apache Access Log--> 192.168.100.10 - - [11/May/2020:17:22:07 +0800] "GET / HTTP/1.1" 403 4961 "-" "curl/7.29.0" 192.168.100.10 - - [11/May/2020:17:23:13 +0800] "GET / HTTP/1.1" 200 22 "-" "curl/7.29.0"

_________

16 May 2020, 22:16 | Views: 3417

Add new comment

For adding a comment, please log in
or create account

0 comments