Get Mac-Address of Host from Within Docker Container

How to fetch MAC address of remote machine from docker container

If you are able to ping given remote machine from docker container but unable to get the mac address that means either

  1. MAC address is not available in the routing table
  2. The host network interface is not available inside a Docker container.

For #1 case you can ping the machine which can add MAC address of that machine in the routing table.

For #2 case you can add a host network interface to Docker container.


eg. docker run --network="host" -it network_service:latest

How to get the IP address of the docker host from inside a docker container

/sbin/ip route|awk '/default/ { print $3 }'

As @MichaelNeale noticed, there is no sense to use this method in Dockerfile (except when we need this IP during build time only), because this IP will be hardcoded during build time.

How does the Docker assign MAC addresses to containers?

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.



Related Topics



Leave a reply



Submit