Is it possible to get MAC address of the host machine from Docker container and write it in a text file?
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.
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.
macOS: ~/Library/Containers/com. docker. docker/Data/vms/0/
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With