2

Today I pull an image from the Internet, and I found that the docker networking mode can be detected in the image's startup script. But docker0 only exists in the host not the container. Why? Dockerfile

FROM ubuntu:18.04

RUN apt-get update \
    && apt-get install -y  net-tools   \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*


ADD test/ /etc/test/

RUN chmod +x  /etc/test/*.sh

CMD ["/bin/bash", "/etc/test/start.sh"]

start.sh

#!/bin/sh
set -e

# check for presence of network interface docker0
check_network=$(ifconfig | grep docker0 || true)

# if network interface docker0 is present then we are running in host mode and thus must exit
if [[ ! -z "${check_network}" ]]; then
    echo "[crit] Network type detected as 'Host', this will cause major issues, please stop the container and switch back to 'Bridge' mode" | ts '%Y-%m-%d %H:%M:%.S' && exit 1
fi

I run it with docker run --privileged --net=host will get

[crit] Network type detected as 'Host', this will cause major issues, please stop the container and switch back to 'Bridge' mode
4
  • Could you explain what you are trying to achieve?
    – 030
    Dec 7 '19 at 10:40
  • @030 sorry, at first I wanted to know if this script used some hacks. But now I realize this is a stupid question that container could get the host interface information whenever I use net=host, should I delete this quetion?
    – maP1E bluE
    Dec 7 '19 at 16:27
  • No it is fine to keep the question. If you would like to change something you could edit it.
    – 030
    Dec 7 '19 at 17:53
  • Keeping the question supports the community in that if someone else has a similar question it could help them. we are all learning all the time ... Dec 10 '19 at 17:58
1

You are not accessing the hosts network interface information. Rather you are receiving data of the bridge adapter created for your Docker Containers Network.

Please note the results from my web server:

Container networking info:

# docker exec -it ghost sh
/var/lib/ghost # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:12:00:02  
          inet addr:172.18.0.2  Bcast:172.18.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:20 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1536 (1.5 KiB)  TX bytes:0 (0.0 B)

Host network info:

root@li293-xxx:/data/mydomain.us# ifconfig
br-08d99d9f172e: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
    inet6 fe80::42:57ff:fe74:c49b  prefixlen 64  scopeid 0x20<link>
    ether 02:42:57:74:c4:9b  txqueuelen 0  (Ethernet)
    RX packets 150  bytes 16535 (16.5 KB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 234  bytes 21425 (21.4 KB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
    ether 02:42:82:3d:17:63  txqueuelen 0  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 66.xxx.xxx.205  netmask 255.255.255.0  broadcast 66.xxx.xxx.255

Please review the networking documentation at https://docs.docker.com/v17.09/engine/userguide/networking/

1
  • I also tried it and the result is the same as yours, but this script works. Please see the updated part of the question.
    – maP1E bluE
    Dec 7 '19 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.