Docker Community Forums

Share and learn in the Docker community.

Docker. Assign IP from the same range as Host


(Ipeacocks) #1

I am playing with docker. Now I want to assign IP to container from the same IPs range as my host OS has.

My Host has IP address 192.168.1.50 (192.168.1.0/24 network). And I want to use for example 192.168.1.51 from the same network for Docker container.

For this I`ve installed bridge-utils (I am using Ubuntu 14.04) and reconfigured my interfaces:

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
        address 192.168.1.50
        network 192.168.1.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.1.1
        dns-nameservers 8.8.8.8 8.8.4.4
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

Now I have such configuration:

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 08:00:27:a5:22:5d brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::5484:7aff:fefe:9799/64 scope link 
       valid_lft forever preferred_lft forever
4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 08:00:27:a5:22:5d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.50/24 brd 192.168.0.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fea5:225d/64 scope link 
       valid_lft forever preferred_lft forever

How to create every new dockers with IPs from the same IP-range 192.168.1.0/24 as Host machine? How to specify for every new docker container to use br0 interface?

Tried this doc https://docs.docker.com/articles/networking/#bridge-building but w/o success.
My host vm interfaces after trying this https://docs.docker.com/articles/networking/#bridge-building (I`ve renamed br0 to bridge0):

root@docker# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bridge0 state UP group default qlen 1000
    link/ether 08:00:27:a5:22:5d brd ff:ff:ff:ff:ff:ff
3: bridge0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 08:00:27:a5:22:5d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.50/24 brd 192.168.1.255 scope global bridge0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fea5:225d/64 scope link
       valid_lft forever preferred_lft forever

And routes:

root@docker:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 bridge0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 bridge0
root@docker:~#

Docker default settings:

root@docker:~# cat /etc/default/docker
# Docker Upstart and SysVinit configuration file

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
DOCKER_OPTS="-b=bridge0"

Settings in docker container:

root@docker:~# docker run -i -t ubuntu /bin/bash
root@cea826723b4f:/#
root@cea826723b4f:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 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
4: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:c0ff:fea8:102/64 scope link
       valid_lft forever preferred_lft forever
root@cea826723b4f:/# ping i.ua
^C
root@cea826723b4f:/# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 192.168.1.50: icmp_seq=2 Redirect Host(New nexthop: 192.168.1.50)
From 192.168.1.50: icmp_seq=3 Redirect Host(New nexthop: 192.168.1.50)
From 192.168.1.50: icmp_seq=4 Redirect Host(New nexthop: 192.168.1.50)
^C
--- 8.8.8.8 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3024ms

root@cea826723b4f:/# ip r
default via 192.168.1.50 dev eth0
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.2
root@cea826723b4f:/#

Thank you in advance.


Assign Container IP adress from same network as host
(Jerry Baker) #2

Hi ipeacocks,

I just successfully ran through/tested the steps for creating a custom bridge on a new Ubuntu install: https://docs.docker.com/articles/networking/#bridge-building

As far as specifying an IP when you launch a container, there is a current Github request open that you’ll want to keep an eye on: https://github.com/docker/docker/issues/6743

Thanks,

Jerry


(Ipeacocks) #3

I just successfully ran through/tested the steps for creating a custom bridge on a new Ubuntu install: https://docs.docker.com/articles/networking/#bridge-building

Do you use the same ip range as host machine?

But anyway there is no benefits to use such schemes in cases when you cant set direct Ip to container.