49

When I start new containers, Docker automatically assigns some MAC address to them. I am curious if there is a pattern to this assignment. Can the MAC address be changed?

$ docker network inspect bridge

"Containers": {
            "3386a527aa08b37ea9232cbcace2d2458d49f44bb05a6b775fba7ddd40d8f92c": {
                "EndpointID": "647c12443e91faf0fd508b6edfe59c30b642abb60dfab890b4bdccee38750bc1",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "94447ca479852d29aeddca75c28f7104df3c3196d7b6d83061879e339946805c": {
                "EndpointID": "b047d090f446ac49747d3c37d63e4307be745876db7f0ceef7b311cbba615f48",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
monica
  • 653
  • 1
  • 6
  • 12

2 Answers2

88

Docker start assigning always the same mac 02:42:ac:11:00:02 for the first container and then is increasing by one each mac for each different container.

Not sure why they are using that mac address. It seems 02:42:ac doesn't match any real vendor in oui databases. Look at the official documentation about this. They say:

The MAC address is generated using the IP address allocated to the container to avoid ARP collisions, using a range from 02:42:ac:11:00:00 to 02:42:ac:11:ff:ff

Anyway, you can set any mac address on container generation using --mac-address parameter on the docker run command. For example doing a command like this docker run -ti --mac-address 00:00:00:00:00:11 ubuntu:trusty

Hope it helps.

OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51
  • Thank you very much! This is exactly what I was looking for. – monica Mar 22 '17 at 09:27
  • 2
    "It seems 02:42:ac doesn't match any real vendor in oui databases". Bit `0x02` in the fist octect of a MAC address indicates that it is "locally administered" so it will never be set in a vendor OUI. That guarantees that a docker-allocated MAC won't collide with a physical network interface's MAC address. I can only guess what the `00x42` in the second octet is for (life the universe and everything?). As @Norbert pointed out in his answer, the 3rd octect (`0xac`) is taken from the 1st octect of the IP address - 172. – Michael Burr Jul 04 '21 at 10:40
2

If you look at the MAC address of the container interface(s), you can see that the last 4 octets are the hex representation of the IPv4 address. This part is prefixed by 02:42: For example:-

The docker generated MAC address of the container interface with IPv4 address 172.19.0.6 would be 02:42:ac:13:00:06

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
Norbert
  • 21
  • 3
  • Thank you for pointing that out - I was trying to figure out exactly what the phrase "The MAC address is generated using the IP address allocated to the container" meant. – Michael Burr Jul 04 '21 at 10:22