Reaching the host and the outside world

Intermediate · 8 min

Why localhost in a container is the container, how host.docker.internal and host-gateway reach host services, and the proxy, DNS and MTU traps on the way out.

Before thisPublishing ports and the host firewallUser-defined networks and container DNS

anthonywritescode · 8 min

Reaching the host and the outside world

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by anthonywritescodeReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • localhost inside a container is the container's own loopback interface, not the host's. A service on the host's 127.0.0.1 is invisible from a bridge network.
  • Docker Desktop resolves host.docker.internal to the host. On Linux, add it yourself with --add-host host.docker.internal=host-gateway, or extra_hosts in Compose.
  • host-gateway defaults to the default bridge's gateway address, usually 172.17.0.1, and the dockerd host-gateway-ip option changes it.
  • The host service must listen on an address the container can reach. Binding it to 0.0.0.0 to fix that also exposes it to the network unless the firewall blocks it.
  • Outbound traffic is masqueraded behind the host's address and uses the host's DNS servers, through 127.0.0.11 on user-defined networks.
  • Corporate networks add three traps: proxies the container does not know about, TLS-inspecting CAs it does not trust, and VPN MTUs that make large transfers hang.

Two different localhosts

A developer runs Postgres on their laptop and the app in a container, and the app cannot connect to localhost:5432. The fix people reach for, --network host, works on Linux and hides the actual lesson. The container has its own network namespace, and that includes its own loopback interface. localhost inside it means the container, the way localhost on your laptop means your laptop and not the server you are SSHed into.

So reaching the host is a question of which address the host has from the container’s point of view, and whether the host service is listening there.

The host’s address from inside

On a bridge network the host is the gateway. On the default bridge that is usually 172.17.0.1. Hard-coding it is fragile, so Docker has a name for it.

Docker Desktop resolves two special names automatically, per its networking how-tos: host.docker.internal for your machine, and gateway.docker.internal for the gateway of the Docker VM.

Linux needs one flag. --add-host writes a line into the container’s /etc/hosts, and its special value host-gateway is replaced by the host’s address:

docker run --rm --add-host host.docker.internal=host-gateway \
  curlimages/curl -s http://host.docker.internal:8000/

In Compose:

services:
  api:
    image: notes:dev
    extra_hosts:
      - "host.docker.internal:host-gateway"

Using the same name on both platforms means one Compose file works for everyone. What host-gateway turns into I checked in the moby source: unless the host-gateway-ip daemon option is set, the daemon uses the default bridge’s IPv4 gateway, and its IPv6 gateway if it has one.

The host service must be listening there

With the name resolving, the next failure is usually a timeout. A database installed on the host typically listens on 127.0.0.1 only. The container’s packets arrive at 172.17.0.1, an address nothing is listening on. The sockets lesson shows how to check with ss -tlnp.

The quick fix, listening on 0.0.0.0, opens the service on every interface, the public one included. A better one:

  • Listen on loopback and the bridge address, for Postgres listen_addresses = 'localhost,172.17.0.1', and in pg_hba.conf allow only the container subnets.
  • Make sure the host firewall’s INPUT chain accepts traffic from the Docker subnets to that port. Unlike published ports, this traffic is addressed to the host and does go through ufw.

Or skip the host entirely: run the database in a container on the same user-defined network and connect by name. For local development, that is usually simpler than any of the above.

The way out

Outbound traffic from a bridge network is masqueraded behind the host’s address, as the default bridge lesson showed, and name lookups go to the host’s DNS servers, through Docker’s embedded DNS at 127.0.0.11 on user-defined networks. Most of the time that just works. On corporate networks, three things break it.

Proxies. If the host reaches the internet through an HTTP proxy, containers need to know too. The Docker CLI can inject proxy variables into every container and build from a proxies section in ~/.docker/config.json:

{
  "proxies": {
    "default": {
      "httpProxy": "http://proxy.corp.example:3128",
      "httpsProxy": "http://proxy.corp.example:3128",
      "noProxy": "localhost,127.0.0.1,.corp.example"
    }
  }
}

The daemon’s own proxy, used for docker pull, is configured separately. During builds, the proxy variables are predefined build arguments that stay out of the image history.

TLS inspection. A proxy that decrypts HTTPS re-signs certificates with a company CA. The container’s trust store does not know that CA, so every HTTPS call fails with an unknown-authority error. The CA certificates page shows adding the root certificate to the image, for Debian-based images by copying it to /usr/local/share/ca-certificates/ and running update-ca-certificates. It also warns to treat such certificates carefully in production images.

MTU. Containers default to an MTU of 1500. A VPN or some cloud overlays carry smaller packets. Small requests fit and work, large transfers are silently dropped and hang, the most confusing symptom of the three. Set a smaller MTU with com.docker.network.driver.mtu on a network, or "mtu" in daemon.json for the default bridge.

IPv6 is the other thing to know about: bridge networks are IPv4-only unless created with --ipv6, so a container cannot reach an IPv6-only destination by default even when the host can.

About the video

The embed is anthonywritescode’s “docker: connecting to localhost outside the container (intermediate) anthony explains #555”, September 2023, 8 minutes. Its chapters cover the problem, a demonstration, connecting through host.docker.internal, the security implications of listening on 0.0.0.0, and a safer way to do it. It does not cover proxies, CA certificates or MTU, which the lesson adds. English was judged from the channel and the English caption track. LearnCantrill’s video in deeper is a short overview of networking modes and port exposure.

What was run: the default for host-gateway was read in the moby source, setHostGatewayIP in daemon/daemon_unix.go. Nothing else could be run without Docker. The --add-host behaviour is from the docker/cli docker run reference, the special names from the Docker Desktop networking how-tos, the CA steps from the CA certificates page, and the MTU and IPv6 options from the bridge driver page, checked 2026-11-28. The proxy configuration format is from Docker’s CLI proxy documentation.

Where this fits

That ends networking. Next, describing a whole stack of containers, their networks and volumes in one file: why Compose exists.

Check yourself

  1. 01Your app in a container connects to postgres://localhost:5432 and gets connection refused, though Postgres runs on your Linux laptop. Why?

    localhost in the container is the container's own loopback. Nothing listens on 5432 there. Use host.docker.internal, added with --add-host host.docker.internal=host-gateway on Linux.

  2. 02You add host-gateway and the connection now times out instead of being refused. Postgres listens on 127.0.0.1 on the host. What is wrong?

    The container reaches the host at the bridge gateway address, such as 172.17.0.1, but Postgres only listens on loopback. Make it also listen on the bridge address and allow that subnet, rather than on every interface.

  3. 03Why is binding the host service to 0.0.0.0 a risky fix?

    It listens on every interface, including the public one, so anyone who can reach the host can reach the service unless a firewall blocks it. Listen on the docker0 address and allow only container subnets.

  4. 04Inside a container on a corporate VPN, small API calls work but downloading a large package hangs forever. What is a likely cause?

    An MTU mismatch. The VPN path carries smaller packets than the container's 1500-byte default, and large packets are silently dropped. Lower the network's MTU with com.docker.network.driver.mtu or the daemon's mtu setting.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark