Home / Localhost / Docker port mapping

Docker port mapping 8080:80 explained (not localhost:8080:80)

What -p 8080:80 and ports: "8080:80" mean in Docker: the first number is on your computer, the second is inside the container. Which URL to open and how to fix it.

In Docker, <code>8080:80</code> is not a web address. It is a pair of port numbers written HOST:CONTAINER. The first number is the port on your computer, and the second is the port the program uses inside the container. You open <b>http://localhost:8080</b>, never <code>localhost:8080:80</code>.

How to use a port mapping

  1. Read the mapping from left to right: HOST_PORT:CONTAINER_PORT. Only the left number is used from your computer.
  2. Open http://localhost: followed by the left number, for example http://localhost:8080.
  3. Check what is really published with docker ps. The PORTS column shows lines such as 0.0.0.0:8080->80/tcp. docker port NAME shows one container’s mappings.
  4. If you want a different address on your computer, change the left number only, then recreate the container.
  5. Other containers on the same Docker network use the container name and the right number, not your computer’s port.

Commands you can copy

Run a web server on your port 8080
docker run -d -p 8080:80 --name web nginx
Same container, only reachable from your computer
docker run -d -p 127.0.0.1:8080:80 --name web nginx
Compose: always write the mapping as a quoted string
services:
  web:
    image: nginx
    ports:
      - "8080:80"
See what is published
docker ps
docker port web

Examples: what to type in the browser or client

MappingMeaningOpen or connect to
-p 8080:80Your port 8080 goes to port 80 in the containerhttp://localhost:8080
-p 8081:80A second container, also port 80 insidehttp://localhost:8081
-p 5433:5432PostgreSQL inside uses 5432; you reach it on 5433localhost, port 5433 (a database client, not a browser)
-p 3306:3306MySQL on the same number on both sideslocalhost, port 3306
-p 127.0.0.1:8080:80Only your own computer can use ithttp://localhost:8080
-p 8080:80/udpThe UDP port, not TCPA UDP client on port 8080

Docker’s documentation describes the host IP and protocol parts of the -p flag. Database ports are not web pages, so a browser shows nothing useful there.

If it does not work

localhost:8080:80 shows an error or a search page
That text is a port mapping, not a URL. A web address has only one port: http://localhost:8080. The same goes for localhost:8081:80, localhost:5433:5432 and localhost:3306:3306.
Compose: always quote the mapping
Docker’s Compose reference warns that YAML can read numbers like 22:22 as a base-60 value, so write "8080:80" as a quoted string.
The page refuses to connect after I published the port
Publishing is not enough. The app inside the container must listen on 0.0.0.0, not only on 127.0.0.1, and the right number must match the port the app really uses. See the refused to connect checklist.
EXPOSE in the Dockerfile did not open the port
EXPOSE only documents the port. To reach it from your computer, publish it with -p or with ports: in Compose.
Port is already allocated
Another container or program holds the left number. See Docker: port is already allocated.
Published ports are visible to other computers
Without a host IP, a published port is available to the outside world as well as your computer, and Docker’s documentation says publishing is insecure by default. Use 127.0.0.1: in front, as in -p 127.0.0.1:8080:80, to keep it on your computer.
My app in another container cannot connect
Between containers on the same network, use the container or service name with the right number, for example db:5432. See Docker container cannot reach localhost.

Questions about Docker port mapping

What does 8080:80 mean in Docker?
Port 8080 on your computer is forwarded to port 80 inside the container. You open localhost:8080.
Which number is which?
The left number is the host (your computer). The right number is the container. Remember it as FROM your side TO the container.
Can I use the same left number twice?
No. Each host port can belong to only one container or program at a time. Use 8081:80 for the second container.
Why does 5433:5432 appear for PostgreSQL?
The database always uses 5432 inside the container. Using 5433 on your computer avoids a clash with a PostgreSQL that is already installed there.
What is -P (capital)?
It publishes the container’s exposed ports to random free ports on your computer. Use docker ps to see which.

Updated 2026-10-20

Free tools that help

Config generators for this

Related pages