How to set the MAC address for Docker LXC containers?

跟風遠走 提交于 2019-11-30 14:40:15

问题


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.


回答1:


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/




回答2:


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




回答3:


The above answer worked for me and helped me very much, but I needed a little more detail to get it to work. This is very helpful when you have an app that licenses of the mac-address. As this was the only post I could find on this topic, I thought I expand on it.

For me, it required a little more to work - else the switch will be silently discarded. For ubuntu 12.04:

  1. edit /etc/default/docker: DOCKER_OPTS="--dns 8.8.8.8 -e lxc"
  2. apt-get install lxc (lxc-docker is not enough)
  3. docker run --lxc-conf="lxc.network.hwaddr=92:20:de:b0:6b:61" -t myimage


来源:https://stackoverflow.com/questions/18479242/how-to-set-the-mac-address-for-docker-lxc-containers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!