Using pipework to configure the Docker container to the local network environment

Source: Internet
Author: User
Tags git clone docker run
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/
1 2 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
1

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
1 2 3 4 5 6 7 8 9 10 11 12-13

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
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
1

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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

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
1 2 3 4 5

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
1 2 3 4 5

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
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 Qlen 1 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 Dynami C eth0 valid_lft 1464sec preferred_lft 1464sec inet6 fe80::6be1:989e:26e9:488e/

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.