HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Why container can use ifconfig to get the host information?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
whycanthecontainergethostifconfiguseinformation

Problem

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

Solution

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  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
    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  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  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/

Code Snippets

# 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)
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

Context

StackExchange DevOps Q#10042, answer score: 1

Revisions (0)

No revisions yet.