How to connect to the Docker host from inside a Docker container?

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

Docker version

docker -v
Docker version 20.10.8, build 3967b7d

Why do I even want to reach my Docker host from inside a Docker container?

Simple example

  • We run a simple Spring Boot application that exposes a REST endpoint returning a ‘Hello World’ message.
  • We start a Ubuntu container in Docker and execute a REST call to the Spring Boot application running on the host OS(Linux) using curl.
  • expose a REST endpoint
  • start at port 8080
spring run helloWorld.groovy
docker run -it ubuntu bash
  • it is short for --interactive plus --tty
  • executing the command take you directly inside the container
  • allows you to interact with /bin/bash inside the container
  • Long story short: you will have abash session inside an Ubuntu Docker container
apt update & apt install curl -y
curl http://host.docker.internal:8080
root@8024db7c38fe:/# ping host.docker.internal
ping: host.docker.internal: Name or service not known

How to make it work?

--add-host=host.docker.internal:host-gateway
docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash
root@c1d299ee554d:/# curl http://host.docker.internal:8080
Hello world container. You are able to reach the Docker host!

How about Docker compose?

Other special DNS names

  • gateway.docker.internalto reach the gateway
  • docker.for.mac.host.internal (replaces docker.for.mac.localhost)
  • docker.for.win.host.internal (replaces docker.for.win.localhost)

Conclusion

  • It’s easy to connect to the host OS from inside a Docker container
  • Avoid the use of OS-specific DNS names flavors like: docker.for.mac.host.internal
  • In Docker for Mac and Docker for Windows, you can connect to the host out of the box by using the special DNS name:host.docker.internal
  • For Linux, you need the magic string host-gateway to map to the gateway inside the container. This allows you to use the hostname host.docker.internal to access the host from inside a container.
  • To get a consistent behavior on all platforms (Mac, Windows, and Linux) use host.docker.internal:host-gateway in your Docker compose file.
  • Play around yourself! Clone my Github repo.

Resources

--

--

--

I’m a creative and passionate software developer living in the Netherlands. Occasional meetup & conference speaker.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What Multi-Cloud Type are You?

Why should you learn programming during the COVID-19 pandemic (2020–21)?

Getting Started with Dart on GCP

Being Static or Dynamic? Our Libraries

Keeping the code growth under control

CodeRoad#2

How to plan and configure tasks in Odoo V15 Field Service Module

What’s new in V8 8.6? Better WebAssembly!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tim van Baarsen

Tim van Baarsen

I’m a creative and passionate software developer living in the Netherlands. Occasional meetup & conference speaker.

More from Medium

How to persist and backup data of a PostgreSQL Docker container

Create and Run AWS ECS Task programmatically using .NET 5, C# & AWS ECS SDK

Initialize MongoDB running on a Docker container

Deploying An ASP.NET Core API To AWS Fargate Using CDK