Access host machine details from inside Docker con

2019-08-16 05:50发布

I would like to know the way to get host machine details especially the MAC address from inside Docker container.

4条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-16 06:28

It will depend on your host system, since you didn't give any information I'm just going to assume CentOS

In this article it shows how you can get that info from /prove on the host system. So, if you mount /prove into your container you should be able to read the right file and pull out your info.

This doesn't seem secure though, and I recommend against it.

查看更多
戒情不戒烟
3楼-- · 2019-08-16 06:34

One other way is to use --net=host while starting the container.

$# ifconfig -a | grep -Po 'HWaddr \K.*$'

00:00:00:00:00:00  
9c:b6:54:1d:80:4b  
48:5a:b6:67:9e:11   

$# docker run --net=host -it ubuntu:14.04 bash

$# ifconfig -a | grep -Po 'HWaddr \K.*$'

00:00:00:00:00:00  
9c:b6:54:1d:80:4b  
48:5a:b6:67:9e:11  
查看更多
贼婆χ
4楼-- · 2019-08-16 06:37

Maybe you can approach this problem different way. Eg. pass information you need from host to container via environment variables.

docker run -e HOST_MAC=$(ifconfig -a | grep -Po 'HWaddr \K.*$') image

This requires you to change how you run a container, however it's probably the cleanest method of solving this.

查看更多
该账号已被封号
5楼-- · 2019-08-16 06:44

You have many options.

Аnother option is

docker run -it -v /var/log/:/log --name vmaccess busybox /bin/sh
grep eth0 /log/dmesg

[    2.307760] e1000 0000:00:03.0 eth0: (PCI:33MHz:32-bit) 08:00:27:98:dc:aa
[    2.307783] e1000 0000:00:03.0 eth0: Intel(R) PRO/1000 Network Connection
[    4.186427] IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
[    8.985277] IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
查看更多
登录 后发表回答