Docker Network detailed

Source: Internet
Author: User
Tags redis docker run

Docker Network detailed

Reprint please specify from: http://blog.csdn.net/wsscy2004 Network Foundation

Docker uses Linux bridging, in the host virtual one Docker0 network interface, runs the command view in the host:

# List Host Bridges
$ sudo brctl showbridge      name bridge    ID               STP enabled     Interfacesdocker0             8000.000000000000       No

# show Docker0 IP address
$ sudo ifconfig docker0docker0   Link encap:ethernet  hwaddr xx:xx:xx:xx:xx:xx
     inet addr:172.17.42.1  bcast:0.0.0.0  mask:255.255.0.0

Docker when a container is started, it is divided according to the DOCKER0 Network segment container IP,DOCKER0 is the gateway for each container. Customizing the network scope

Although Docker is using Linux Brigde will find the most appropriate. But sometimes we still need to plan for ourselves.
Using the-b=<bridgename> parameter setting

# First close Docker $ sudo service Docker stop # Close the bridge docker0# add your own network Bridge BRIDGE0 $ sudo ifconfig docker0 down $ sudo brctl addbr Brid GE0 $ sudo ifconfig bridge0 192.168.227.1 netmask 255.255.255.0 # Add startup Custom bridge parameters to Docker startup file $ echo "docker_opts=\" -b=bridge0\ "" >>/etc/default/docker # start Docker $ sudo service Docker start # see if the Custom Network Bridge started successfully, IP, etc. configured correctly $ sudo ifcon Fig bridge0 bridge0 Link encap:ethernet hwaddr xx:xx:xx:xx:xx:xx inet addr:192.168.227.1 bcast:192.168.227.2 mask:255.255.255.0 # Start container docker run-i-T Base/bin/bash # you can see container IP 192.168.227/24 within the network segment root@261c272 cd7d5:/# ifconfig eth0 eth0 Link encap:ethernet hwaddr xx:xx:xx:xx:xx:xx inet addr:192.168.227.5 bcast:19 2.168.227.255 mask:255.255.255.0 # BRIDGE0 IP as the default gateway# view routing information root@261c272cd7d5:/# route-n Kernel IP rou Ting table Destination Gateway genmask Flags Metric Ref use iface 0.0.0.0 192.168.227.1 0  .0.0.0 UG 0    0 0 eth0 192.168.227.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 # hits ctrl+p then CTRL +             Q to Detach # View bridge information $ sudo brctl show bridges name Bridge ID STP enabled interfaces BRIDGE0
 8000.FE7C2E0FAEBD No VETHAQI2QT
Container interworking

Docker default is to allow container interoperability through-icc=false shutdown interworking.
Once interoperability is turned off, you can only specify container through the-link name:alias command connection.

Container separate from each other, suppose we have a webapp container, a redis contianer need to interoperate.
First start Redis container:

sudo docker run-d--name Redis Crosbymichael/redis

Then start WebApp and connect to Redis.

#将redis取别名为dbsudo Docker run-t-i--link redis:db--name webapp Ubuntu bash

You can see the network information for DB in WebApp:

$ root@4c01db0b339c:/# env

hostname=4c01db0b339c
db_name=/webapp/db
term=xterm
db_port=tcp:// 172.17.0.8:6379
db_port_6379_tcp=tcp://172.17.0.8:6379
db_port_6379_tcp_proto=tcp
DB_PORT_6379_TCP _addr=172.17.0.8
db_port_6379_tcp_port=6379
path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/ Sbin:/bin
pwd=/
shlvl=1
home=/
container=lxc
_=/usr/bin/env

After version 0.11, the alias for link REDIS:DB generates the corresponding IP mappings in/etc/hosts:

root@6541a75d44a0:/# cat/etc/hosts172.17.0.3  6541a75d44a0172.17.0.2  db
What is vethxxxx
# View bridge information
$ sudo brctl show bridges      name bridge    ID               STP enabled     interfaces
Docker0             8000.FE7C2E0FAEBD       No              vethaqi2qt

Vethxxx is a conduit that connects the host to the internal eth0 of the container. See IP link and namespaces infrastructure more

Pipework can create a variety of complex containers interworking scenarios. For more details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.