
localhost Inside a Container Isn't Your Machine: Docker Networking Explained
- كاتب موثّق
محتويات المقال
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
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.
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."
localhost:8090 works from the host. Inside a container it means something else.
What actually happens
Each container has its own localhost
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
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
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
alpine:3.22 as the caller and nginx:1.29.8-alpine as the renderer:
# 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'docker network create shop# 734fc117d8b054006e86cc64f5f083ca23a9e49c98bf15ca9377e70b1ad805d4docker run -d --name renderer --network shop \ -p 127.0.0.1:8090:80 nginx:1.29.8-alpine# da288351a2d7942fe077e3ca18d35805321e56de114cf2764031414585e9f164docker run --rm --network shop alpine:3.22 \ wget --spider http://renderer:80 # name + container port# Connecting to renderer:80 (172.26.0.2:80)# remote file existsdocker run --rm alpine:3.22 \ wget --spider http://host.docker.internal:8090 # your machine# Connecting to host.docker.internal:8090 (192.168.65.254:8090)# remote file existslocalhost: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.
localhost and the published host port (needs -p) | ||
host.docker.internal (Desktop), or --add-host ...=host-gateway | ||
localhost and the container port |
How to apply it in your project
Search your container config for localhost Every localhostin 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.Put services that talk on one network In Compose that's the default. With plain docker run, create a network and pass--networkto every container that needs it.Use the container port between containers Host ports are only for traffic from outside. If you change a published port, nothing inside the network should break. Check what your app listens on If the image includes netstat, rundocker exec <container> netstat -ltn. An address of127.0.0.1:80means only the container itself can connect.
Key takeaways
Each container has its own network stack, so localhostinside 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.internalon Docker Desktop, orhost-gatewayon Linux.Publish only what you need, bound to 127.0.0.1, and make containerized apps listen on0.0.0.0.
التعليقات (0)