1

I'd like to run a docker-compose made of a few containers one of which should act as a DHCP server and assign IP addresses to a few devices attached to a bridge directly plugged into one of the docker-compose host interface.

I think I could use macvlan or ipvlan - the latter being my preference, ipvlan seems to do exactly what I wish, with the only excpetion of this gateway/ip_range business.

I am a bit confused about why they are needed in the first place, all I'd like to do is to assign a static ip address to the container interface and have the DHCP server listening at that interfacec. I am starting to question myself wether ipvlan/macvlan are the right solution or otherwise.

In short, is there a way to assign a static ip address to a ipvlan interface ? I've found a few solutions over the internet but none works with docker compose v3.

pedr0
  • 2,941
  • 6
  • 32
  • 46

1 Answers1

2

Docker Compose v3 should allow defining custom network plugins and configurations, including Macvlan and IPvlan. Between Macvlan and IPvlan, the latter has advantages such as not needing MAC addresses, which can be useful in large-scale scenarios.

Assigning a static IP to a container using Docker Compose v3 should also be straightforward. You can use the ipv4_address setting under the network.
Create a Docker network outside of Docker Compose, for instance an IPvlan network named ipvlan_net, associated with the eth0 interface on the Docker host:

docker network create -d ipvlan \
  --subnet=192.168.1.0/24 \
  --gateway=192.168.1.1 \
  -o parent=eth0 \
  ipvlan_net

The gateway/ip_range is required for routing. The gateway is used to forward traffic that is not destined for the local network. That becomes especially important if your containers need to access external networks or the internet.

Then configure your docker-compose.yml:

version: '3.8'

services:
  dhcp-server:
    image: your_dhcp_image
    networks:
      ipvlan_net:
        ipv4_address: 192.168.1.10
    cap_add:
      - NET_ADMIN 

networks:
  ipvlan_net:
    external: true
  • The external configuration under networks ensures that Docker Compose uses the pre-existing ipvlan_net network.
  • Make sure the static IP address assigned (192.168.1.10 in this example) does not conflict with any existing addresses or ranges you have configured for DHCP lease.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250