First, data management
1. Mount the local directory into the container
[email protected] ~]# Docker run-tid-v/data/:/data CentOS Bash
0f1093aadf6ec85224520a303f92282d12443ecf0305bbf15eb2f3526fd55212
[email protected] ~]# Docker exec-it 0F10 Bash
[Email protected]/]# df-h
Filesystem Size used Avail use% Mounted on
/dev/mapper/docker-253:0-34406889-
Tmpfs 490M 0 490M 0% /sys/fs/cgroup
/dev/mapper/centos-root 18G 4.8G 13G 28% /data
-V is used to specify the Mount directory: the previous/data/is a local directory: The following/data/is the directory in the container
2. Mount the data volume
In fact, when we mount the directory, we can specify that the container name is randomly defined if it is not specified. For example, we didn't specify it to create a name named Loving_kowalevski. You can use the command Docker PS to see the right column
Docker run-itd--volumes-from Loving_kowalevski Aming/centos Bash
This allows us to create a new container using the Szk/centos image and use the data volume of the Loving_kowalevski container
Custom container Name
Docker run-itd-v/data/:/mnt--name datavol aming/centos Bash
3. Defining Data Volume containers
Sometimes we need to share data among multiple containers similar to NFS in Linux so we can build a special data volume container and then mount the data volume directly from other containers.
First, create a data volume container
Docker run-itd-v/data/--name insane_hopper CentOS Bash
#注意这里的/data/is the/data directory of the container, not the local/data/directory if you want to use a container that is already running, use the--volumes-from to specify the last row name of the container through Docker PS and then have the other containers mount the data volume
[email protected] ~]# Docker run-itd--volumes-from insane_hopper CentOS Bash
9c296da6550be50bbe90cce23bbebf1590245c114d572dea1ec4f9441f70dd07
[email protected] ~]# Docker run-itd--volumes-from insane_hopper--name web2 CentOS Bash #还可以继续创建
88e24c0d19efb8c152271b0c07e3f262fbb6d03252b72b2fbc3433c30930f6eb
[email protected] ~]# Docker exec-it 88e2 Bash
[Email protected]/]# ls/data/
Szk.txt
4. Backup and recovery of data volumes
1 backup
[Email protected] ~]# Mkdir/vol_data_backup
[email protected] ~]# Docker run-itd--volumes-from insane_hopper-v/vol_data_backup/:/backup CentOS Bash
ad6c5945f7509cf952f29be51733eddbdd588295c820f01e000b3346ce8b2779
[email protected] ~]# Docker exec-it ad6c Bash
[Email protected]/]# ls/backup/
[Email protected]/]# tar Cvf/backup/data.tar/data
[Email protected] ~]# ls/vol_data_backup/
Data.tar
Note First we need to use insane_hopper data volume to open a new container at the same time we also need to mount the local/vol_data_backup/directory to the container/backup so that in the container/backup directory inside the new file we can directly in the/ See in the vol_data_backup/directory. Then, package the files under the/data/directory into a Data.tar file and place them under the/backup directory.
2 Recovery
The idea is to create a new data volume container and then build another container and mount the data volume container and unpack the tar package.
New Data volume container Docker run-itd-v/data/--name data CentOS Bash
Mount the data volume new container and unpack the Docker run--volumes-from testvol2-v/vol_data_backup/:/backup CentOS tar Xvf/backup/data.tar
Second, network management
Four types of network modes
The host mode uses Docker run when using--net=host to specify that the network that Docker uses is actually the same as the host computer. The IP that is seen in the container is the IP on the host
Container mode uses--net=container:container_id/container_name multiple containers using a common network to see the same IP
The None mode uses--net=none to specify that no network is configured in this mode
Bridge mode uses--net=bridge to specify the default mode without specifying the default is this network mode. This mode assigns a separate network Namespace to each container. A NAT network mode similar to VMware. All containers on the same host will be able to communicate with each other under the same network segment.
External Network access container
First create a new container using the CentOS image and then install the HTTPD service in that container and start
[email protected] vol_data_backup]# Docker commit-m "CENTOS_WITH_HTTPD"-A "Szk" 8d5192bdf660 centos_with_httpd
216d5f8f0c57f49c311e7b7a07c2767b14a1bd78cf3a11c7f923bea9df6a7b57
[email protected] vol_data_backup]# Docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
CENTOS_WITH_HTTPD latest 216d5f8f0c57 seconds ago 337.7 MB
Then make the container into a new mirror centos-httpd) and then use the new image to create the container and specify the port mapping
Docker run-itd-p 5123:80 centos-httpd bash//-p can specify port mapping This example maps the container's port 80 to a local 5123 port
[email protected] vol_data_backup]# Docker run-itd-p 8080:80 centos_with_httpd:latest
1cf3b634f537d6f2bfc8b17461e92dfa0f7370b91607f125f58cdbf2e545314b
[email protected] vol_data_backup]# Docker exec-it 1CF3 Bash
[Email protected]/]#/USR/SBIN/HTTPD
[Email protected]/]# vi/var/www/html/1.html
[Email protected] ~]# Netstat-ntlup | grep 8080
TCP6 0 0::: 8080:::* LISTEN 4069/docker-proxy
[Email protected] ~]# Curl localhost:8080/1.html
Szk
Docker exec-it container_id Bash
Start httpd httpd-k Start
Edit 1.html vi/var/www/html/1.html just write something.
Exit this container exit
Test Curl 127.0.0.1:5123/1.html
The IP:port:ip:port format is also supported after-p such as
-P 127.0.0.1:8080:80
You can also do not write local port-only IP so it will be arbitrarily assigned a port
-P 127.0.0.1::80
Container interconnection
Download a MySQL image
Docker pull MySQL
Create a new container named DB
Docker run-it-d-P 13306:3306--name db MySQL Bash
Create a new Web container and connect to the DB
Docker run-it-d-P 12308:80--name web--link db:db centos-httpd Bash
Configure Bridging network CENTOS7)
To make it easier for machines and Docker containers to communicate on the local network we often have the need to configure Docker containers to the same network segment as the host. This requirement is actually very easy to implement. We just need to bridge the Docker container and the host's Nic and give the Docker container an IP.
Installing Pipworkgit clone Https://github.com/jpetazzo/pipework
CP ~/pipework/pipework/usr/local/bin/
Open a container docker run-itd--net=none--name aming123 Centos/bin/bash
Pipework br0 aming123 172.7.15.201/[email protected] #201为容器的 [email protected] The IP behind is the host IP
Brctl addif br0 eth0 #eth0为宿主机网卡这一步为把br0和eth0桥接起来
IP addr Add 172.7.15.107/24 br0 #把107的ip绑定在br0上
Docker exec-it Aming123/bin/bash #进去后ifconfig查看就可以看到新添加的ip
CENTOS6:
cd/etc/sysconfig/network-scripts/; CP Ifcfg-eth0 IFCFG-BR0
VI ifcfg-eth0//Add bridge=br0 Delete ipaddr,netmask,gateway,dns1
VI ifcfg-br0//Modify the device to Br0,type for Bridge, set the Eth0 network settings here
Service Network restart
Installing Pipwork:git clone Https://github.com/jpetazzo/pipework
CP ~/pipework/pipework/usr/local/bin/
Open a container: Docker run-itd--net=none--name aming123 Centos/bin/bash
RPM-UVH RPM-UVH https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/ iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm #不安会报错Object "Netns" is unknown, try "IP Help"
Pipework Br0 aming123 172.7.15.201/24
Docker exec-it Aming123/bin/bash #进去后ifconfig查看就可以看到新添加的ip
This article is from the "Fuqin Wine" blog, please make sure to keep this source http://szk5043.blog.51cto.com/8456440/1827183
Docker Data and network management