using pipework to configure the Docker container to the local network environment
Demand
In the process of using Docker, we sometimes have the need to configure the Docker container to the same network segment as the host. To achieve this demand, we simply will Docker container and host network card Bridge, and then to Docker container with IP on it.
Here we use the Pipework tool to implement this requirement. Install pipework
$ git clone https://github.com/jpetazzo/pipework
$ sudo cp pipework/pipework/usr/local/bin/
using pipework
First look at the current IP address of my host is 192.168.0.109/24, the gateway is 192.168.0.1, using the ETH0 network card. Suppose I need to assign the Docker container an address of 192.168.0.200.
First create a container
$ sudo docker run-itd--name test Ubuntu/bin/bash
At this point, view the container's IP address information, where only one Eth0,ip address is 172.17.0.3/16 is Docker the default assigned address, as follows:
$ sudo docker exec Test IP addr show
1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue qlen 1
LINK/LOOPB Ack 00:00:00:00:00:00 BRD 00:00:00:00:00:00
inet 127.0.0.1/8 Scope host lo
valid_lft forever Preferred_lft Forever
Inet6:: 1/128 scope host
Valid_lft forever Preferred_lft Forever 25:eth0@if26
: <broadcast, Multicast,up,lower_up,m-down> MTU 1500 Qdisc noqueue
link/ether
02:42:ac:11:00:03 BRD ff:ff:ff:ff:ff:ff inet 172.17.0.3/16 Scope global eth0
valid_lft forever Preferred_lft forever Inet6 fe80::42:acff:fe11:3/64
Scope link
Valid_lft forever preferred_lft Forever
The following configures the network for container test and connects to the Network Bridge BR0, where the gateway address is followed by @
$ sudo pipework br0 test 192.168.0.200/24@192.168.0.1
In this step, pipework first checks whether the host has a BR0 network bridge, if it does not exist, create one on its own. This starts with "BR", so it creates a Linux bridge. If you start with "OvS", you will create a openvswitch bridge.
In addition, if there is a DHCP service in the host environment, IP can also be obtained by DHCP
$ sudo pipework br0 test DHCP
At this point to view the container's IP address information, found that a new network card eth1, the assigned IP address is 192.168.0.200/24, as follows:
$ sudo docker exec Test IP addr show 1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue Qlen 1 link/loopback
00:00:00:00:00:00 BRD 00:00:00:00:00:00 inet 127.0.0.1/8 scopes host lo Valid_lft forever Preferred_lft Forever INET6:: 1/128 scope host Valid_lft forever Preferred_lft forever 25:eth0@if26: <broadcast,multicast,up,low Er_up,m-down> MTU 1500 Qdisc noqueue link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff inet 172.17.0.3/16 SCO PE global eth0 Valid_lft forever Preferred_lft forever inet6 fe80::42:acff:fe11:3/64 Scope link valid_l FT Forever Preferred_lft Forever 27:eth1@if28: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> MTU 1500 Qdisc noqueue-Qle N 1000 link/ether 4e:ab:e0:c5:a7:81 brd ff:ff:ff:ff:ff:ff inet 192.168.0.200/24 BRD 192.168.0.255 Scope Global ETH 1 Valid_lft Forever Preferred_lft forever inet6 fe80::4cab:e0ff:fec5:a781/64 scope link Valid_lft forev Er preferred_lft Forever
The host Eth0 Bridge to the Br0, and the Eth0 IP is configured on the BR0. This is a remote operation, and the intermediate network is broken, so it is executed in a single command.
$ sudo ip addr add 192.168.0.109/24 dev br0
$ sudo ip addr del 192.168.0.109/24 dev eth0
$ sudo brctl addif br0 et H0
$ sudo ip route del default
$ sudo ip route add default via 192.168.0.1 Dev br0
The above command will break the net once in execution, so you can put the above command in one step, as follows:
$ sudo ip addr add 192.168.0.109/24 dev br0; \ sudo
ip addr del 192.168.0.109/24 dev eth0;
sudo brctl addif br0; \
sudo ip eth0 del default; \
sudo IP route add default via 192.168.0.1 Dev br0
Note: The above step only in the first time binding the container address will be used, once executed, will be on the host to the original eth0 IP address assigned to BR0, and then eth0 and Br0 connected. So it's not necessary to create a container later. And you just need to execute the following command of the binding container address.
$ sudo pipework br0 test 192.168.0.200/24@192.168.0.1
After running the above command, view the host's IP address information as follows:
$ IP Addr Show eth0
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU 1500 Qdisc pfifo_fast Master br0 State up Qle N 1000
link/ether 76:e0:49:4f:00:ac brd ff:ff:ff:ff:ff:ff inet 192.168.0.109/24 brd 192.168.0.255
Scope Global Dynamic eth0
valid_lft 1464sec preferred_lft 1464sec
inet6 fe80::6be1:989e:26e9:488e/64 scope link
valid_ LfT Forever Preferred_lft Forever
$ IP addr Show br0
12:br0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU 1500 Qdi SC Noqueue State up Qlen 1000 link/ether 76:e0:49:4f:00:ac brd ff:ff:ff:ff:ff:ff inet 192.168.0.109/24
Scope glob Al br0
Valid_lft forever Preferred_lft Forever inet6 fe80::fc31:97ff:fe9e:872c/64
scope link
valid_lft Forever Preferred_lft Forever
Now, open a different terminal, you can ping the 192.168 0.200来 Test Container network. or ping the container address from another physical host.