Skip to content
Theory · Your first container · lesson 4 of 7

Ports: -p host:container

Picture an apartment building. Visitors know only the street address and the entrance number, while every flat inside has its own door with its own number. From the street you cannot reach a flat's door: you need a doorman with a rule, "whoever comes to entrance 8080, take them to the flat, to door 80".

Your computer is the building, and its ports are the entrances. A container is a flat, and the port the program in the container listens on is the flat's door. The -p flag is the doorman's rule.

Without publishing, the port is closed

Every container has its own network: its own address and its own ports, separate from the host's. A container started without flags joins the default bridge network, and no host port leads to its ports. Let's check, removing the old web first:

Shell
docker rm -f web
docker run -d --name web nginx:1.27.3
curl localhost:8080
TEXT
web
29459f666c58a3d9d0f50cd00e611e15d3fd9195a9dd763b37e650b3568715ee
curl: (7) Failed to connect to localhost:8080 after 0 ms: Could not connect to server

Connection refused: nothing listens on port 8080 of the host. nginx is running, but its port 80 stayed inside the container.

In docker ps, such a container shows 80/tcp with no arrow in the PORTS column. That is the port the image's author declared with the EXPOSE instruction. A declaration is only a hint: on its own it publishes nothing.

Publishing: -p host:container

Publishing a port is a rule by which Docker listens on a host port and forwards everything that arrives there to a container port. In -p 8080:80 the host port is on the left and the container port on the right. The request goes from the host into the container: localhost:8080 on the host leads to port 80, where nginx listens.

LiveWhere -p host:container leads200 OK
  1. browserlocalhost:8080
  2. hostport 8080
  3. container webport 80 · listens on 80

Docker forwards the host's port 8080 to the container's port 80, where web listens.

You cannot add -p to a container that already exists, so the container is created again:

Shell
docker rm -f web
docker run -d --name web -p 8080:80 nginx:1.27.3
docker ps
TEXT
web
52956cf236bc7d758a521a9b9acb20d1d81fdc72f299af4ad9038b500ab46445
CONTAINER ID   IMAGE          COMMAND                  CREATED                  STATUS                  PORTS                                     NAMES
52956cf236bc   nginx:1.27.3   "/docker-entrypoint.…"   Less than a second ago   Up Less than a second   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp   web

Now PORTS has an arrow. 0.0.0.0:8080->80/tcp means that port 8080 on every address of the host leads to port 80 of the container. [::]:8080->80/tcp is the same rule for IPv6. Let's send a request:

Shell
curl -sI localhost:8080
TEXT
HTTP/1.1 200 OK
Server: nginx/1.27.3
Date: Fri, 13 Nov 2026 21:18:08 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 26 Nov 2024 15:55:00 GMT
Connection: keep-alive
ETag: "6745ef54-267"
Accept-Ranges: bytes

A 200 OK answer with the Server: nginx/1.27.3 header: the request went from the host into the container. The same page opens in a browser at localhost:8080.

This is where the building picture stops working. In a building one entrance can lead to many flats, while a host port publishes only one container. A second container with the same -p 8080:80 will not start, and Docker answers port is already allocated. The second nginx gets another host port, such as -p 8081:80.

One more form is -p 127.0.0.1:8080:80. It publishes the port for the machine itself only: nothing on the local network can connect to it.

So with -p 8080:80 nginx answers on localhost:8080, and without it nginx runs, but no host port leads to it.

Common mistake

The ports were written in reverse order:

Shell
docker rm -f web
docker run -d --name web -p 80:8080 nginx:1.27.3
curl localhost:8080
curl localhost:80
TEXT
web
8973d05ec8565df657fc140c02d3d153e13b166896123563b7bcbcdd59cc88aa
curl: (7) Failed to connect to localhost:8080 after 0 ms: Could not connect to server
curl: (56) Recv failure: Connection reset by peer

-p 80:8080 is the rule "port 80 of the host leads to port 8080 of the container". That gives two different errors:

  1. Nothing listens on port 8080 of the host, so the connection is refused.
  2. Docker listens on port 80 of the host and forwards the request to port 8080 of the container, but nginx is not waiting there. The connection is reset.

The fix is the host port on the left and the container port on the right: -p 8080:80.

Key points

  • A container has its own ports; without -p no host port leads to them, even while the program runs.
  • -p 8080:80: the host port on the left, the container port on the right, the request goes from the host inward.
  • 80/tcp with no arrow in docker ps is a declared port, not a published one.
  • Reversed order gives "connection refused" on one port and "connection reset" on the other.