libnetwork icon indicating copy to clipboard operation
libnetwork copied to clipboard

macvlan: --ip-range single IP address /32

Open muradm opened this issue 7 years ago • 2 comments

Creating macvlan network with single IP address range works, but attaching container fails.

Create network:

$ docker network create -d macvlan \
--subnet 192.168.34.0/24 \
-o parent=eth0 \
--ip-range 192.168.34.54/32 \
eth0-test
3b413e03b6de671c20c48cdeb07562c42a220b983d464a467a6be8492ff5eb8c

Create container:

$ docker run -it --detach=false --network eth0-test ubuntu:latest
docker: Error response from daemon: no available IPv4 addresses on this network's address pools: eth0-test (3b413e03b6de671c20c48cdeb07562c42a220b983d464a467a6be8492ff5eb8c).

Ultimate goal, to have predefined IP address assigned to container like above and further more to service, like below:

Host1:

$ docker network create --config-only \
--subnet 192.168.34.0/24 \
-o parent=eth0 \
--ip-range 192.168.34.2/32 \
eth0-net

Host2:

$ docker network create --config-only \
--subnet 192.168.34.0/24 \
-o parent=eth0 \
--ip-range 192.168.34.3/32 \
eth0-net

Host-any:

$ docker network create -d macvlan \
--scope swarm --config-from eth0-net eth0-swarm-net
$ docker service create --mode global --name test-service \
--network eth0-swarm-net ubuntu:latest sleep infinity

Such service container will acquire IP 192.168.34.2 when started on host1, and 192.168.34.3 when started on host2. Service not necessarily should be global, it could be replicated with sticky docker host configuration, guaranteeing only one instance attaching to network eth0-swarm-net per docker host. Thus achieving static IP address assignment on per host / container basis in swarm mode.

Docker version:

$ docker version
Client:
 Version:           18.06.0-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        0ffa825
 Built:             Wed Jul 18 19:11:02 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.0-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       0ffa825
  Built:            Wed Jul 18 19:09:05 2018
  OS/Arch:          linux/amd64
  Experimental:     false

muradm avatar Aug 12 '18 16:08 muradm

It works if network is routed, i.e. with --gateway specified:

$ docker network create -d macvlan \
--subnet 192.168.34.0/24 \
--gateway 192.168.34.1 \
-o parent=eth0 \
--ip-range 192.168.34.2/32 \
eth0-test
3b413e03b6de671c20c48cdeb07562c42a220b983d464a467a6be8492ff5eb8c
$ docker run -it --rm --detach=false --network eth0-test \
donch/net-tools:latest /bin/bash
bash-4.3# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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
31: eth0@if3: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 02:42:c0:a8:22:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.34.2/24 brd 192.168.34.255 scope global eth0
       valid_lft forever preferred_lft forever
bash-4.3# ip route show
default via 192.168.34.1 dev eth0 
192.168.34.0/24 dev eth0  src 192.168.34.2 

muradm avatar Aug 13 '18 06:08 muradm

It works if network is routed, i.e. with --gateway specified:

$ docker network create -d macvlan \
--subnet 192.168.34.0/24 \
--gateway 192.168.34.1 \
-o parent=eth0 \
--ip-range 192.168.34.2/32 \
eth0-test
3b413e03b6de671c20c48cdeb07562c42a220b983d464a467a6be8492ff5eb8c
$ docker run -it --rm --detach=false --network eth0-test \
donch/net-tools:latest /bin/bash
bash-4.3# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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
31: eth0@if3: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 02:42:c0:a8:22:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.34.2/24 brd 192.168.34.255 scope global eth0
       valid_lft forever preferred_lft forever
bash-4.3# ip route show
default via 192.168.34.1 dev eth0 
192.168.34.0/24 dev eth0  src 192.168.34.2 

THIS. Thank you so much I've been stuck with a error no available IPv4 addresses on this network's address pools for a couple hours. Adding the --gateway setting to the network config did the trick.

poulti avatar Sep 23 '23 12:09 poulti