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会自动为它们分配一些MAC地址。我很好奇这个作业是否有规律可循。是否可以更改MAC地址?
$ docker network inspect bridge
$DOCKER网络检查桥
"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": ""
}
更多回答
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.
Docker开始为第一个容器分配始终相同的Mac 02:42:AC:11:00:02,然后为每个不同的容器增加一个Mac。
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:
不确定他们为什么要使用那个Mac地址。似乎02:42:AC与OUI数据库中的任何真实供应商都不匹配。看看关于这一点的官方文件。他们说:
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
无论如何,您可以在docker run命令上使用--mac-Address参数在容器生成时设置任何Mac地址。例如,执行如下命令:docker run-ti--mac-Address 00:00:00:00:00:11 ubuntu:trusty
Hope it helps.
希望有帮助。
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:-
如果您查看容器接口(S)的MAC地址,您可以看到最后4个二进制八位数是IPV4地址的十六进制表示。此部分以02:42为前缀:例如:-
The docker generated MAC address of the container interface with IPv4 address 172.19.0.6
would be 02:42:ac:13:00:06
对接程序生成的IPv4地址为172.19.0.6的容器接口的MAC地址为02:42:AC:13:00:06
The MAC addresses for Docker all begin with 02:42
. The remaining four octets represent the IP address of the container in hex. 0xAC
in hex is 172
in decimal, 0x11
is 17
, 0x00
is, well, 0
, and 0x02
is... 2
. Thus this MAC address represents a container with the IP address 172.17.0.2
. The default Docker bridge
network is always given the IP range 172.17.0.0/16
by default, thus this would be the MAC address of the virtual Ethernet adapter given to the first container you run on a fresh Docker install.
Docker的MAC地址都以02:42开头。其余四个二进制八位数以十六进制表示容器的IP地址。十六进制的0xAC是十进制172,0x11是17,0x00是0,0x02是...2.因此,该MAC地址代表IP地址为172.17.0.2的容器。默认情况下,默认的Docker网桥网络的IP范围始终为172.17.0.0/16,因此这将是您在全新的Docker安装上运行的第一个容器所使用的虚拟以太网适配器的MAC地址。
See https://maclookup.app/faq/how-do-i-identify-the-mac-address-of-a-docker-container-interface for more information.
有关详细信息,请参阅https://maclookup.app/faq/how-do-i-identify-the-mac-address-of-a-docker-container-interface。
更多回答
Thank you very much! This is exactly what I was looking for.
非常感谢!这正是我要找的。
"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.
“似乎02:42:AC与OUI数据库中的任何真实供应商都不匹配”。第一个保护的MAC地址中的位0x02指示它是“本地管理的”,因此永远不会在供应商OUI中设置它。这保证了扩展底座分配的MAC不会与物理网络接口的MAC地址冲突。我只能猜测第二个二进制八位数中的00x42是什么(生命、宇宙和一切?)。正如@Norbert在他的回答中指出的那样,第三个八位(0xac)取自IP地址的第一个八位-172。
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.
谢谢您指出这一点--我正在试图弄清楚短语“MAC地址是使用分配给容器的IP地址生成的”到底是什么意思。
我是一名优秀的程序员,十分优秀!