Requiring to connect to a Docker host from inside a container may sound odd and unusual, but it can actually help you in some situations. For instance, when there is a need to have running software in a Docker container, that itself requires control over other containers.

You can use the feature to start, stop, and check the status of other containers: let's say you have scheduled backup software. Before backing up, you need to stop your containers and start them again after the backup process. Or maybe you want to create a simple web interface for more accessible control over your containers? Even simpler: you want to connect to a cloud or database to fetch or alternate the data. See below for how to do this!

The ability to connect to a docker host from inside of a docker container came with the release of version 18.03 for macOS and Windows, although Docker did not support the function for Linux until late 2020. When attempting to connect to a host from a container, you should know that the host either has a changing IP address or none (if there is no network access). We recommend you use the special DNS that resolves to the internal IP of the host: host.docker.internal. You can also use gateway.docker.internal DNS.

You can use the host.docker.internal hostname in macOS and Windows out of the box. However, Linux will require an additional step (works from version 20.10 and newer) because the host cannot be resolved automatically on Linux. Adding the add-hostrun flag to default host.docker.internal gateway will solve the issue and enable you to connect to the docker host from inside of your container:

--add-host=host.docker.internal:host-gateway

You can use Python (if you have it installed) to check how it works. It is rather straightforward and will require several minutes only:

  1. Start a simple HTTP server with port 8000 using the python -m http.server 8000 command. You can use the python -m SimpleHTTPServer 8000 command for Python 2.x.
  2. Build your docker container and install curl using either the apt-get update&& apt-get install -y curl command, apk add curl, or using any other preferred way of installation
  3. Try to connect to your host using the following command:
curl http://host.docker.internal:8000
exit

Please note that if you are using Linux, you need to build your docker with appropriate flags before executing the curl command. Otherwise, the curl command will yield you an error. See the correct build sequence below:

docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash
curl http://host.docker.internal:8000
exit

To have a more consistent docker behavior you can add host.docker.internal:host-gateway into your Docker compose file.

N.B. Please note that this will work for development purposes only; production environments outside of Docker Desktop should use proper DNS.

Request more information today

Discover how our services can benefit your business. Leave your contact information and our team will reach out to provide you with detailed information tailored to your specific needs. Take the next step towards achieving your business goals.

Contact form:
Our latest news
Power of Prometheus & Grafana

Why Prometheus and Grafana?

Monitor microservices effortlessly with Prometheus and Grafana for real-time insights and alerts.