0

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?

ipeacocks
  • 2,187
  • 3
  • 32
  • 47
  • I've not tried, but I think this is roughly what the docker run argument `--net=host` does. – Adrian Mouat Apr 07 '15 at 11:11
  • This is related to http://stackoverflow.com/questions/35742807/docker-1-10-containers-ip-in-lan and http://stackoverflow.com/questions/22641828/how-to-set-a-docker-containers-ip?lq=1 – Dreamcat4 Mar 12 '16 at 07:59

2 Answers2

1

Not a direct answer but it could help, if your goal to expose docker services to local network.

I run most of my dockerized services tied to own static ips using the next approach:

  1. I create ip aliases for all services on docker host
  2. Then I run each service redirecting ports from this ip into container so each service have own static ip which could be used by external users and other containers.

Sample:

docker run --name dns --restart=always -d -p 172.16.177.20:53:53/udp dns
docker run --name registry --restart=always -d -p 172.16.177.12:80:5000 registry
docker run --name cache --restart=always -d -p 172.16.177.13:80:3142 -v /data/cache:/var/cache/apt-cacher-ng cache
docker run --name mirror --restart=always -d -p 172.16.177.19:80:80 -v /data/mirror:/usr/share/nginx/html:ro mirror
...

As a result everyone (including other docker containers) could use services in containers by well known address or name and you even could move containers (with corresponding alias) to different host and everything will continue to work.

ISanych
  • 21,590
  • 4
  • 32
  • 52
  • so 172.16.177.{20,12,13,19} are on you network interfaces of Host machine? – ipeacocks Apr 07 '15 at 21:26
  • They are ip aliases (http://en.wikipedia.org/wiki/IP_aliasing) for main ip interface. It's like network interface have many addresses. And I could remove alias on one host and add it to another host when I need to move container. – ISanych Apr 08 '15 at 17:39
-1

Docker can't do that. It can set IPs only by own decision.

ipeacocks
  • 2,187
  • 3
  • 32
  • 47