Sub topic 1
Why a Container's Ports Are Not Reachable by Default
Prerequisites
This chapter assumes you have:
- Docker installed, and you can run
docker run. - Basic command-line skills.
- A working idea of images and containers.
- A basic idea of TCP ports and
localhost.
The working model
Every container gets its own network namespace. This is a private copy of the network stack, with its own interfaces, its own loopback address (127.0.0.1), its own IP address, and its own table of ports. A process listening on port 80 inside a container is listening on the container's port 80. It is not listening on the host's port 80.
On the default bridge network, Docker creates a virtual switch on the host and connects each container to it. The container receives a private address, often in the 172.17.0.0/16 range. The host's network rules translate (NAT) traffic that leaves the container, so the container can reach the internet. They do not forward traffic that arrives from outside to the container. Outbound connections work by default. Inbound connections do not.
Seeing it happen
Start nginx without publishing anything, then try to reach it from the host:
docker run -d --name web-noport nginx
curl http://localhost:80The curl command fails with Failed to connect to localhost port 80. The server is running. It is simply on a network the host does not forward to.
You can look up the container's private address:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web-noportOn a Linux host, curl to that address usually works, because the host can route to its own bridge. Do not rely on this:
- Machines on your network cannot reach that address.
- On Docker Desktop (macOS and Windows), Docker runs inside a virtual machine, so the host cannot reach the container address at all.
- The address changes when the container is recreated.
Clean up before moving on:
docker rm -f web-noportWhy Docker works this way
Isolation by default keeps services private unless you decide otherwise. A database container that needs to be reachable only by an application container is not exposed to the whole network by accident. Docker makes you opt in for each port.
Isolation also means port numbers do not collide between containers. Two containers can both listen on port 80, because each has its own port table. A conflict appears only when you ask the host to forward the same host port to two places, which a later chapter covers.
Common errors at this stage
| Symptom | Likely cause |
|---|---|
| `curl: (7) Failed to connect` to `localhost` | The port was never published, so the host does not forward to the container. |
| `curl` works from the Linux host to the container IP, but not from another machine | The bridge address is private. Only a published port is reachable from outside. |
| The container is `Up`, but nothing answers on the host | Check `docker ps`. An empty `PORTS` column means no host port is mapped. |
You can run docker ps to confirm. For a container with nothing published, the PORTS column shows only something like 80/tcp. That entry shows the image's declared port. It is not a mapping to the host. A later chapter explains the difference.
What comes next
To make a container port reachable, you ask Docker to publish it, which adds a forwarding rule from a host port to a container port. The -p flag does this, and the next chapter covers it.
Exercise
You run docker run -d --name api nginx on a laptop and then curl http://localhost:80 fails with a connection error. The container is listed as Up. Explain why the request fails, and name one reason that connecting to the container's private IP address is not a dependable fix.
Show answer
The container has its own network namespace on a private bridge, and no host port is forwarded to it because nothing was published. The server is running, but the host has no rule that sends traffic for localhost:80 to the container. Connecting to the private IP is unreliable because it works only from a Linux host, not from other machines or Docker Desktop's virtual machine, and the address changes when the container is recreated.