Docker assign static IP to the container for the specified physical network segment

Source: Internet
Author: User
Tags docker ps docker run
Docker assign static IP to the container for the specified physical network segmentPony 2015-02-06 theRead Docker Linux OS

The official information about the Network bridge and IP configuration is the address of the document: https://docs.docker.com/articles/networking/

1, host (System using UBUNTU-14.04.1-SERVER-AMD64) network using bridging mode (the default is DHCP mode, there is a mode is static IP), Network Bridge network segment and the same as the physical network segment. The current physical network segment is 192.168.5.1/24, the gateway is 192.168.1.1, the physical network card device is eth0
Edit the configuration file/etc/network/interfaces, custom Network Bridge BR0

root@ubuntu-docker:~# cat/etc/network/interfaces
# This file describes the network interfaces available on your syste M # and how to
activate them. For more information, interfaces (5).

# The Loopback network interface
Auto lo
iface lo inet Loopback

# The Primary network interface
#auto eth0
#iface eth0 inet dhcp
#
auto br0
iface br0 inet static address
    192.168.5.116
    netmask 255.255.255.0
    Gateway 192.168.5.1
    bridge_ports eth0
    bridge_stp off
    dns-nameservers 192.168.1.12 192.168.1.13
root@ubuntu-docker:~#

Restart network after configuration save exit

root@ubuntu-docker:~# ifdown-a && ifup-a

After configuration is complete, you can see the status information of Br0 and eth0 as follows (host IP address is 192.168.5.116) with Ifconfig:

Br0 Link encap:ethernet hwaddr 00:0c:29:dc:47:11 inet addr:192.168.5.116 bcast:192.168.5.255 mask:255.2  55.255.0 Inet6 addr:fe80::20c:29ff:fedc:4711/64 scope:link up broadcast RUNNING multicast mtu:1500
          Metric:1 RX packets:737 errors:0 dropped:0 overruns:0 frame:0
          TX packets:226 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:70000 (70.0 KB) TX bytes:26394 (26.3 kb) eth0 Link encap:ethernet hwaddr 00:0c:29:dc:47:11 up B
          Roadcast RUNNING multicast mtu:1500 metric:1 RX packets:70065 errors:0 dropped:151 overruns:0 frame:0 TX packets:19621 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX txqueuelen:1000 126 (23.9 MB) TX bytes:2321651 (2.3 MB)

2, the Network Bridge configuration is good, the rest is Docker related network configuration.
A the network mode initiated by the container must be none, specified with –net=none, for example

Docker run-it--rm--net=none Eeed74b237f9/bin/bash

b Get the ID of the target container, mine is dfe83012cda2.

root@ubuntu-docker:~# Docker ps-a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
dfe83012cda2        centos:centos6.6    /bin/bash about a           hour ago up   minutes                           test
root@ubuntu-docker:~#

c The steps to configure IP are a bit cumbersome, I wrote a script. You can then set the IP by passing related parameters to the script. For example, set static IP address for container dfe83012cda2 192.168.5.123, mask 255.255.255.0, Gateway 192.168.5.1. One problem is that when the container is restarted, the set IP is lost, and the script needs to be rerun again.

root@ubuntu-docker:~# sh manual_con_static_ip.sh dfe83012cda2 192.168.5.123 24 192.168.5.1

The contents of the script are as follows (refer to official documentation)

#/bin/bash if [-Z $] | | [Z $] | | [-Z $] | |
[Z $]; Then echo "*****input the necessary Parameters:containerid IP MASK GATEWAY" echo "*****call the script Li Ke:sh manual_con_static_ip.sh b0e18b6a4432 192.168.5.123 192.168.5.1 "Exit fi containerid=$1 setip=$2 Setmas K=$3 gateway=$4 pid= ' Docker inspect-f ' {{. State.pid}} ' $CONTAINERID ' mkdir-p/var/run/netns find-l/var/run/netns-type l-delete ln-s/proc/$pid/ns/net/var/run /netns/$pid IP link add A type Veth peer name B brctl addif br0 A IP link set A up IP link set B netns $pid IP netns exec  $pid IP link Set dev B name eth0 IP netns exec $pid IP link set eth0 up IP netns exec $pid IP addr add $SETIP/$SETMASK Dev eth0 IP netns exec $pid IP route add default via $GATEWAY

The script contains a large amount of information, the following branch to solve the function of the statement:

Pid= ' Docker inspect-f ' {{. State.pid} ' $CONTAINERID '

To operate the container you need to get the process number of the container Pid,docker inspect can view the bottom information of the container, view all the underlying information about the container dfe83012cda2, and view it with Docker inspect dfe83012cda2. The-f parameter can format the output of the given information, such as viewing the state of the container

root@ubuntu-docker:~# Docker inspect-f ' {{. State.running}} ' Dfe83012cda2
true
root@ubuntu-docker:~#

Create a net namespace for the container, establish a point-to-point connection (the container namespace network card and the network adapter generated on the host), ensure that the directory/var/run/netns exists for the net namespace, then delete the failed link for the directory, and then soft link the container's net namespace file to/var/ Run/netns in order to perform IP netns to read.

Mkdir-p/var/run/netns
find-l/var/run/netns-type l-delete ln-s/proc/
$pid/ns/net/var/run/netns/$pid

Create 2 direct-attached network cards (A and B) on the host, use B as the NIC in the container, and a as the host's network card.

IP link Add A type Veth peer name B

Connect the network card a bridge to the BR0 and start the network card a

Brctl addif br0 a
IP link set a up

Add network card B to the appropriate container net namespace, and the host hosts will not be able to see the network card information (the directory read by default when IP Netns is/var/run/netns) when Nic B is added to the container's net namespace.

IP link set B netns $pid

IP netns exec can enter the container's net namespace, which can be used to configure network parameters for the container net namespace, and to configure NIC B within the container

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.