Docker network create macvlan with DHCP possible?

Hello.

I am running 3 dockers using a macvlan network but the way I am doing it is:

# Get IPv4 for Gateway, Subnet Mask and IP Addresses
while [ -z “$ip4” ]

do
ip4=“$(ip -o -4 addr list eth0 | awk ‘{print $4}’ | cut -d. -f1,2,3)”
done

# Create docker network
docker network create -d macvlan --subnet=“$ip4.0/24” --gateway=“$ip4.1” -o parent=eth0 \
-o macvlan_mode=bridge eth0

# Start dockers
docker run -d --name=‘alpine1’ --net=‘eth0’ --ip=“$ip4.101” ‘alpine’ tail -f /dev/null
docker run -d --name=‘alpine2’ --net=‘eth0’ --ip=“$ip4.102” ‘alpine’ tail -f /dev/null
docker run -d --name=‘alpine3’ --net=‘eth0’ --ip=“$ip4.103” ‘alpine’ tail -f /dev/null

And everything is fine… BUT if I want to run this on a network that uses a different subnet mask and/or gateway and/or IP addresses 101 thru 103 being used, it will not work. Is there a way for me to create the macvlan network using DHCP and avoid having to get the IP and assume gateway is .1 and subnet mask is 0/24?

Thanks! :+1:

I have the same question.
Cheers

The --ip option is not mandatory so your container can get its IP address dynamically, but it will not be from DHCP. I don’t think you could do that. As far as I know, Docker just assigns a new IP address from the configured subnet. If you have existing IP addresses in the subnet, Docker can assign those existing IPs to containers. If you want to avoid that, you can use the --aux-address option or --ip-range to use a subset of the available IP addresses in the configured subnet or as the documentation mentions, use a dedicated subnet just for containers on macvlan. The gateway is optional too, but you need to define at least the subnet.

If you have a server, that is not likely to move frequently to different subnets. If you have a laptop, that is not for long-running containers and you can always use a script to regenerate your Docker networks and and recreate your containers without manually modifying anything except the subnet.