Whenever I run a docker container, I see that it uses a random MAC address:
eth0 Link encap:Ethernet HWaddr de:6f:de:74:bd:d9
How do I set a specific MAC address for a container run?
Will I be able to have multiple containers running simultaneously with the same MAC address? These containers do not need to access the outside network and do not need to talk to each other.
By default, the MAC address is generated using the IP address allocated to the container. You can set the container's MAC address explicitly by providing a MAC address via the --mac-address parameter (format: 12:34:56:78:9a:bc ). Be aware that Docker does not check if manually specified MAC addresses are unique.
In the same way, a container's hostname defaults to be the container's ID in Docker. You can override the hostname using --hostname . When connecting to an existing network using docker network connect , you can use the --alias flag to specify an additional network alias for the container on that network.
To create a macvlan network which bridges with a given physical network interface, use --driver macvlan with the docker network create command. You also need to specify the parent , which is the interface the traffic will physically go through on the Docker host.
The MAC address is set using the LXC configuration option lxc.network.hwaddr
.
Here is an example of how to set MAC address using Docker 0.6.1:
docker run --lxc-conf="lxc.network.hwaddr=92:20:de:b0:6b:61" my_image ifconfig
In the output, you will see the HWaddr that was set:
eth0 Link encap:Ethernet HWaddr 92:20:de:b0:6b:61
Update:
The previous switch -lxc-conf
(with 1 dash) has been deprecated.
To use the above switch, you docker must be using the LXC driver: -e lxc
Newer versions of docker
take a --mac-address=12:34:56:78:9a:bc
switch to docker run
.
root@kevin-VirtualBox:~# sudo docker run --rm --mac-address"=12:34:de:b0:6b:61" ubuntu ifconfig | grep HWaddr
eth0 Link encap:Ethernet HWaddr 12:34:de:b0:6b:61
See https://docs.docker.com/reference/run/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With