Getting Mac address from Docker Container

岁酱吖の 提交于 2020-01-01 09:49:41

问题


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


回答1:


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 



回答2:


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


来源:https://stackoverflow.com/questions/43275289/getting-mac-address-from-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!