Getting Mac address from Docker Container

2019-04-08 15:30发布

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

2条回答
老娘就宠你
2楼-- · 2019-04-08 16:12

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
查看更多
对你真心纯属浪费
3楼-- · 2019-04-08 16:15
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 
查看更多
登录 后发表回答