Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the MAC address for Docker LXC containers?

Tags:

docker

lxc

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.

like image 340
Victor Lyuboslavsky Avatar asked Aug 28 '13 04:08

Victor Lyuboslavsky


People also ask

Do Docker containers have MAC addresses?

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.

What is the hostname of a Docker container?

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.

How do I create a Docker Macvlan 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.


2 Answers

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

like image 37
Victor Lyuboslavsky Avatar answered Sep 18 '22 05:09

Victor Lyuboslavsky


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/

like image 74
Kevin Smyth Avatar answered Sep 22 '22 05:09

Kevin Smyth