الانتقال إلى المحتوى
DevOps والـ CloudDocker· 6 دقائق للقراءة

localhost Inside a Container Isn't Your Machine: Docker Networking Explained

Each container has its own localhost. Other containers are reached by name and container port, your machine through host.docker.internal.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A team moves its API and its PDF renderer into containers. On the laptop, the API called the renderer at http://localhost:8090, and it worked. In Docker, every call fails with "Connection refused", even though docker ps shows the renderer running and the browser opens localhost:8090 just fine. Someone swaps localhost for the container's name and keeps the port. Still refused.

The common misconception

The belief: inside a container, localhost points to the host machine and the other containers. They all run on your machine, the port is published, so localhost:8090 should work from anywhere on it.

It's a reasonable guess. localhost:8090 really does reach the renderer from your browser. On Docker Desktop, containers feel like local processes that start and stop with your tools. Before Docker, every service on the laptop really was on localhost. And there is a mode where the belief holds: the Compose docs describe network_mode: host, where "the container shares the host's network stack."

The half-truth: localhost:8090 works from the host. Inside a container it means something else.

What actually happens

Each container has its own localhost

A container is isolated with Linux namespaces, as the article on containers and VMs showed, and one of them is the . Docker's networking overview describes what that leaves the container with: it "only sees a network interface with an IP address, a gateway, a routing table, DNS services, and other networking details." In our test, ip -4 addr in an Alpine container listed exactly two interfaces: lo with 127.0.0.1 and eth0 with 172.17.0.4. That lo is the container's own . The docs say the same about DNS: --dns=127.0.0.1 "refers to the container's own loopback address." Nothing listens on port 8090 inside the API container, so the connection is refused.

Containers find each other by name on a user-defined network

The default network has a catch. Containers on the default bridge can reach each other by IP, but "they cannot refer to each other by name." On a network you create, "containers can communicate with each other using container IP addresses or container names." Compose does this for you: each service "joins the default network and is both reachable by other containers on that network, and discoverable by its service name." In our Compose test (Compose 5.1), an api service with no network settings at all reached http://renderer:80.

Published ports are for the host, not between containers

-p 127.0.0.1:8090:80 maps port 8090 on the Docker host to port 80 in the container. That's a , and it's a door for traffic from outside. The Compose docs spell out the consequence: "Networked service-to-service communication uses the CONTAINER_PORT. The host port is only used when accessing the service from outside the network." So renderer:8090 fails: inside the renderer, nginx listens on 80.

Your machine has its own name

To reach a service running on your machine, Docker Desktop provides host.docker.internal, which "resolves to the internal IP address of your host." On Docker Engine for Linux you add it yourself with --add-host host.docker.internal=host-gateway; the docker run reference describes host-gateway as a value "that resolves to the internal IP address of the host."

The fix: use the right address for each direction

Both tabs ran on Docker Desktop for Windows (Engine 29.4) with alpine:3.22 as the caller and nginx:1.29.8-alpine as the renderer:

Bash terminal
# renderer is on network shop, published with -p 127.0.0.1:8090:80curl -sI http://localhost:8090 | head -1   # from the laptop# HTTP/1.1 200 OKdocker run --rm --network shop alpine:3.22 \  wget --spider http://localhost:8090# Connecting to localhost:8090 ([::1]:8090)# wget: can't connect to remote host: Connection refuseddocker run --rm --network shop alpine:3.22 \  wget --spider http://renderer:8090# Connecting to renderer:8090 (172.26.0.2:8090)# wget: can't connect to remote host (172.26.0.2): Connection refuseddocker run --rm alpine:3.22 wget --spider http://renderer# wget: bad address 'renderer'

The first tab shows all three mistakes from the hook. localhost:8090 answered from the laptop and was refused from a container, because there it resolved to the container's own ::1. The container name resolved to the renderer's IP, but port 8090 exists only on the host. And a container on the default bridge couldn't resolve the name at all. In the second tab, the name plus the container port worked, and a container went out through host.docker.internal and came back in through the published port.

FromToAddress to use
Your machineA containerlocalhost and the published host port (needs -p)
A containerAnother container on the same networkThe container or service name and the container port
A containerA service on your machinehost.docker.internal (Desktop), or --add-host ...=host-gateway
A containerItselflocalhost and the container port

How to apply it in your project

  1. Search your container config for localhostEvery localhost in a connection string, URL or environment variable that a container reads is a bug, unless it really means the container itself. Replace it with the other service's name.
  2. Put services that talk on one networkIn Compose that's the default. With plain docker run, create a network and pass --network to every container that needs it.
  3. Use the container port between containersHost ports are only for traffic from outside. If you change a published port, nothing inside the network should break.
  4. Check what your app listens onIf the image includes netstat, run docker exec <container> netstat -ltn. An address of 127.0.0.1:80 means only the container itself can connect.

Key takeaways

  • Each container has its own network stack, so localhost inside it is the container itself.
  • Containers on the same user-defined network, or Compose project, reach each other by name.
  • Between containers, use the container port; published ports are for traffic from the host.
  • From a container, reach your machine with host.docker.internal on Docker Desktop, or host-gateway on Linux.
  • Publish only what you need, bound to 127.0.0.1, and make containerized apps listen on 0.0.0.0.

📚 المصادر

  1. Networking overview — Docker Docs
  2. Networking in Compose — Docker Docs
  3. Port publishing and mapping — Docker Docs
  4. Explore networking how-tos on Docker Desktop — Docker Docs
  5. docker container run — Docker Docs

التعليقات (0)