This example shows us how to access host OS from within docker container. In this particular example we have a Nginx webserver running on the Linux host OS and want to access it from within a docker container.


Host OS - Nginx status


vagrant@docker:~$ sudo service nginx status
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
Active: active (running) since Thu 2018-05-31 05:45:03 BST; 9s ago
Main PID: 12171 (nginx)
CGroup: /system.slice/nginx.service
├─12171 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─12172 nginx: worker process
├─12173 nginx: worker process
├─12174 nginx: worker process
└─12175 nginx: worker process

Check the IP address of the host OS which is also known by Docker container.


vagrant@docker:~$ ip addr show docker0
4: docker0: mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:4b:18:b4:1b brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:4bff:fe18:b41b/64 scope link
valid_lft forever preferred_lft forever

vagrant@docker:~$ ip addr show docker0 | grep -Po 'inet \K[\d.]+'
172.17.0.1

So 172.17.0.1 is the one we will use in docker container to talk to host OS.


Container


Let's check if the container really knows the IP address of the host OS.


docker-container # ip route show
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 scope link src 172.17.0.2

Let's now test to see if the container can talk to host OS.


docker-container # curl -I 172.17.0.1
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Thu, 31 May 2018 04:45:59 GMT
Content-Type: text/html
Content-Length: 867
Last-Modified: Thu, 31 May 2018 04:45:03 GMT
Connection: keep-alive
ETag: "5b0f7dcf-363"
Accept-Ranges: bytes

Note


For Mac OS you may just need to use docker.for.mac.host.internal or docker.for.mac.localhost instead.