First, Docker's four network modes (host, container, none, Bridge)
1, host mode, use Docker run when using the--net=host specified, Docker uses the same network as the host, the IP card that is seen in the container is the IP on the host
# docker run-it--rm--net=host httpd Bash
2, container mode, using--net=container:container_id/container_name multiple containers using a common network, see the IP is the same
3, the None mode, using--net=none specify: This mode does not configure any network
4, bridge mode, using--net=bridge specified, default mode, do not specify, the default is this mode, this mode will assign a separate network Namespace for each container. Similar to the VMware NAT network mode, all containers on the same host will be able to communicate with each other under the same network segment
Second, external access container (port mapping)
1. Create a container
# Docker RUN-ITD httpd bash
2. Enter the container and install the HTTPD service, then start the service
# Docker exec-it 3e7 Bash
# yum-y Install httpd
#/USR/SBIN/HTTPD
3. Create a new image for the container
# docker Commit-m "Httpd-server"-A "Fansik" 3e7 httpd
4. Create a container with a new image and develop a port mapping
# docker run-itd-p 5123:80 httpd Bash
5, enter the container, start httpd service
# Docker exec-it 0d6 Bash
#/USR/SBIN/HTTPD
6. Edit a page
# vi/var/www/html/index.html
Just write something and you can.
7. Exit the container test
# Curl Localhost:5123
The Ip:port:ip:port format is also supported after-p, such as:
-P 127.0.0.1:8080:80
You can also not write the local port, write only IP, this will be arbitrarily assigned a port:
-P 127.0.0.1::80
Third, the container interconnection
Download a MySQL image
# docker Pull MySQL
Create a new container named MySQL
# docker run-itd-p 3306:3306--name mysql MySQL bash
Create a new container and connect to MySQL
# docker run-itd-p 1725:80--name web--link mysql:mysql httpd Bash
On the web on the female env command you can find out about MySQL environment variables
Iv. Configuring Bridging Networks
How to configure CENTOS7:
1, in order to make the local network of machines and Docker containers more convenient communication, we often have to configure the Docker container to the same network segment of the host needs, this requirement is very easy to implement, we just need to bridge the Docker container and the host's network card, You can also put the Docker container with IP.
2, Installation pipework
git clone https://github.com/jpetazzo/pipework
CP ~/pipework/pipework/usr/local/bin
3. Open a container:
Docker run-itd--net=none--name Fansik Centos/bin/bash
4, # pipework Br0 Fansik 10.10.10.202/[email protected]
10.10.10.202 is the IP of the host for the IP behind the container ip,@
5, # Brctl addif br0 eth0
Eth0 is the NIC for the host, this step is to connect the br0 and eth0 bridges.
6. # Docker exec-it Fansik/bin/bash
When you go in, you can see the newly added IP ifconfig
How to configure CENTOS6:
1. Configure Network card information
# cd/etc/sysconfig/network-scripts/
# CP Ifcfg-eth0 IFCFG-BR0
# Vim Ifcfg-eth0
Device=eth0
Type=ethernet
Onboot=yes
Bootproto=none
Bridge=br0
# Vim Ifcfg-br0
Device=br0
Type=bridge
Onboot=yes
Bootproto=static
ipaddr=10.10.10.201
netmask=255.255.255.0
gateway=10.10.10.1
dns1=192.168.1.1
Restart network card:/etc/init.d/network restart
2, Installation pipework
git clone https://github.com/jpetazzo/pipework
CP ~/pipework/pipework/usr/local/bin
3. Open a container:
# docker RUN-ITD--net=none--name fansik httpd Bash
4, upgrade Iproute otherwise create bridged network will be error: Object "Netns" is Unknown,try "IP Help"
# RPM-UVH https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/ iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm
5, # Pipework Br0 Fansik 192.168.1.250/24//Add IP for container
6. # docker exec-it Fansik Bash//Enter the container with the ifconfig command to see the newly added IP
Docker Network Management