Docker manual Configuration Container Network instance detailed _docker

Source: Internet
Author: User
Tags centos docker run

Docker manually configure the container network

The network of the Docker container is a combination of the net namespace and the virtual device, which creates a pair of virtual interfaces Veth pair at startup, which are placed in the local and container respectively, The local Veth is assigned a name similar to the vethxxxx and is bridged to the specified network bridge (default is Docker0) and can be viewed through the Brctl Show command to view the mounted interface on the network Bridge, and Veth in the container will get an unused address from the Network Bridge. The name of the Veth is changed to eth0 and the default route is configured to vethxxxx,docker allow different network types to be specified through the--net parameter when the container is started.

--net=bridge: Default value, bridge to the default network bridge.

--net=host: Do not put the container network into the isolated namespace, this time Docker does not container the network in the container, so the created container uses a local network with full local host interface access rights.

--NET=CONTIANER:NAME_OR_ID: Use a network stack of existing containers to share network resources such as IP addresses and ports of existing containers.

--net=none: Put the new container on the isolated network stack without network configuration, we need to specify the item for the container configuration network.

My environment: operating system---centos7,docker version---1.7, base mirroring---centos-6-x86_64.tar.gz

1. Start the container

[Black@test ~]$ Docker run-it--rm--name=mynetwork--net=none Centos:latest/bin/bash

Viewing network settings in a container, you can find that only the local loopback interface lo

[Root@99abaecd79ab/]# ifconfig 
lo    Link encap:local loopback 
     inet addr:127.0.0.1 mask:255.0.0.0 Inet6 addr::: 1/128 scope:host up loopback RUNNING mtu:65536 
     metric:1 
     RX packets:0 errors:0 dropped:0 overruns:0 F rame:0 
     TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0  
     RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) 

2. Create net namespaces for containers

[Black@test ~]$ pid=$ (Docker inspect-f ' {{. State.pid}} ' MyNetwork ' 
[black@test ~]$ sudo mkdir-p/var/run/netns 
[black@test ~]$ sudo ln-s/proc/$PID/ns/n et/var/run/netns/$PID 

3. Create a pair of veth interfaces A and B, bind a to a custom network bridge BR0

[black@test ~]$ sudo ip link add a type Veth peer name B 
[black@test ~]$ sudo brctl addif br0 A 
[black@test ~]$ su Do IP link set A up 

4. Put B into the container, named Eth0, start and configure the IP and default gateway

[black@test ~]$ sudo ip link set b netns $PID 
[black@test ~]$ sudo ip netns exec $PID IP link set dev B name eth0 
[black@test ~]$ sudo ip netns exec $PID IP link set eth0 up 
[black@test ~]$ sudo ip netns exec $PID IP addr Add 10.10. 10.25/24 Dev eth0   //ip with BR0 in the same network segment 
[black@test ~]$ sudo ip netns exec $PID IP route add default via 10.10.10.10.1 

The network settings for viewing the container in the container are as follows

[Root@affbcb8747eb/]# ifconfig                                          
eth0   Link encap:ethernet wadded d2:27:3d:9f:e8:aa inet addr:10.10.10.25  
     bcast:0.0.0.0 mask:255.255.255.0 
     inet6 addr:fe80::d 027:3dff:fe9f:e8aa/64 scope:link up 
     broadcast RUNNING Multicast mtu:1500 metric:1 
     RX packets:8 errors:0 dropped:0 overruns:0 frame:0 
     TX packets:8 errors:0 dropped:0 ov erruns:0 carrier:0 
     collisions:0 txqueuelen:1000  
     RX bytes:648 (648.0 b) TX bytes:648 (648.0 b) 
 
lo    Link Encap:local loopback  
     inet addr:127.0.0.1 mask:255.0.0.0 inet6 
     ::: Addr 1/128 up 
     scope:host RUNNING mtu:65536 metric:1 
     RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
     TX packets:0 errors:0 dropped:0 o verruns:0 carrier:0 
     collisions:0 txqueuelen:0  
     RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) 



Thank you for reading, I hope to help you, thank you for your support for this site,

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.