Docker Community Forums

Share and learn in the Docker community.

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: