Answers: <p>You are not accessing the hosts network interface information. Rather you are receiving data of the bridge adapter created for your Docker Containers Network.</p>
  
  <p>Please note the results from my web server:</p>
  
  <p>Container networking info:</p>
  
  <pre><code># 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)
  </code></pre>
  
  <p>Host network info:</p>
  
  <pre><code>root@li293-xxx:/data/mydomain.us# ifconfig
  br-08d99d9f172e: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  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&lt;link&gt;
      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&lt;UP,BROADCAST,MULTICAST&gt;  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&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
      inet 66.xxx.xxx.205  netmask 255.255.255.0  broadcast 66.xxx.xxx.255
  </code></pre>
  
  <p>Please review the networking documentation at <a href="https://docs.docker.com/v17.09/engine/userguide/networking/" rel="nofollow noreferrer">https://docs.docker.com/v17.09/engine/userguide/networking/</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-07T05:45:01.357Z
Answers: I also tried it and the result is the same as yours, but this script works. Please see the updated part of the question.
Answers: maP1E bluE
Answers: CC BY-SA 4.0
Answers: 2019-12-07T01:14:58.410Z
Answers: 2019-12-07T01:14:58.410Z
Answers: Steven K7FAQ
Body: <p>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</p>
  
  <pre><code>FROM ubuntu:18.04
  
  RUN apt-get update \
      &amp;&amp; apt-get install -y  net-tools   \
      &amp;&amp; apt-get clean &amp;&amp; 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"]
  </code></pre>
  
  <p>start.sh</p>
  
  <pre><code>#!/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' &amp;&amp; exit 1
  fi
  </code></pre>
  
  <p>I run it with  <code>docker run --privileged  --net=host</code> will get </p>
  
  <pre><code>[crit] Network type detected as 'Host', this will cause major issues, please stop the container and switch back to 'Bridge' mode
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2019-12-07T10:40:42.980Z
Comments: Could you explain what you are trying to achieve?
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-12-07T16:27:47.387Z
Comments: @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?
Comments: maP1E bluE
Comments: CC BY-SA 4.0
Comments: 2019-12-07T17:53:06.373Z
Comments: No it is fine to keep the question. If you would like to change something you could edit it.
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-12-10T17:58:07.777Z
Comments: 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 ...
Comments: Steven K7FAQ
Tags: <docker>
Title: Why container can use ifconfig to get the host information?
UserDisplayName: maP1E bluE
ThreadText: # Why container can use ifconfig to get the host information?
  
  (Asked by: username_0 on 2019-12-06)
  
  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
  
  ```
  
  **Tags:** docker
  
  > **Comments:**
  > **username_1**: Could you explain what you are trying to achieve?  
  > **username_0**: @username_1 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?  
  > **username_1**: No it is fine to keep the question. If you would like to change something you could edit it.  
  > **username_2**: 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 ...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-12-07. Score: 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/>
  
  > **Comments:**
  > **username_0**: I also tried it and the result is the same as yours, but this script works. Please see the updated part of the question.  
  
  ---
  
  