Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Mac address from Docker Container

Is it possible to get MAC address of the host machine from Docker container and write it in a text file?

like image 299
prem Avatar asked Apr 07 '17 10:04

prem


People also ask

How do I find my Docker MAC address?

A default MAC address of Docker container interface is between 02:42:00:00:00:00 and 02:42:ff:ff:ff:ff. Mac address generation follows these guidelines: The first part is prefixed by 02:42. The last four octets are the hex representation of the IPv4 address allocated to the container.

Does Docker container have MAC address?

According to the v1. 7 documentation, all the Docker containers have the same prefix in their MAC addresses – '02:42:' if generated automatically. The remaining 4 octets of the MAC address is a container's IPv4 address printed in hex.

Where are Docker containers saved Mac?

macOS: ~/Library/Containers/com. docker. docker/Data/vms/0/

How do I extract an IP address from a container?

If you run the inspect command on a container, you'll get a bunch of information, in JSON format. Scroll down until you find the key NetworkSettings , there look for the sub-key IPAddress . That's your container's IP address. > docker container inspect ubuntu-ip . . . "NetworkSettings": { . . . "IPAddress": "172.17.


2 Answers

docker inspect <container name or id> |grep MacAddress|tr -d ' ,"'|sort -u

or inside the container:

ifconfig -a

ifconfig is part of the 'net-tools' linux pkg and this is good way to enter the running container:

nsenter -t $(docker inspect --format '{{ .State.Pid }}' <container name or id> ) -m -u -i -n -p -w 
like image 137
Rondo Avatar answered Oct 01 '22 14:10

Rondo


Use docker inspect to pull MacAddress and redirect the results to a file. For example, try this on a container named my-container. This uses range (from the Go template package) to find MacAddress:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' my-container > /path/file.txt

If that doesn't work, first try viewing the metadata available for my-container:

docker inspect my-container

Find MacAddress in those results. Then create a docker inspect command that uses the docker json template function to pull the value from that specific json path. The path to MacAddress may vary, so here's an example that instead uses Status:

docker inspect -f "{{json .State.Health.Status}}" my-container > /path/file.txt
like image 29
Woodchuck Avatar answered Oct 01 '22 15:10

Woodchuck