Skip to content

Docker Port Mapping Explained

Understand Docker port mapping: how -p host:container works, why 8080:80 and 443:443 differ, binding to 127.0.0.1, UDP ports, and Compose port syntax.

SDSysadmin Desk November 21, 2026 8 min read
Photographic illustration for Docker Port Mapping Explained: server racks with glowing container blocks

-p 8080:80 is the line everyone copies and few fully understand. Which number is the host? What happens if you swap them? Why does 443:443 look different from 8080:80 when it’s the same mechanism? And why does a container that’s clearly running still refuse connections?

Port mapping is just forwarding: a port on the host hands traffic to a port inside the container. Once the direction is clear, the rest — localhost binding, UDP, ranges, the difference between EXPOSE and publishing — falls into place. Getting it wrong, on the other hand, either makes your app unreachable or quietly exposes a database to the whole internet.

This guide nails down the syntax, the security-critical 127.0.0.1 trick, and the handful of mistakes that account for most “it’s running but I can’t reach it” problems.

The mapping is host:container

The publish flag is -p, and the format is host port first, container port second:

docker run -d -p 8080:80 nginx

That reads: take traffic arriving on the host’s port 8080 and forward it to port 80 inside the container. Nginx inside the container listens on 80, as it always does; you reach it from your browser at http://localhost:8080. The container never knows or cares that the outside world used 8080.

The two numbers are independent. You can map any free host port to whatever port the app uses internally:

Reading port mappings

-p 8080:80 Host 8080 → container 80. Reach it at localhost:8080.
-p 80:80 Host 80 → container 80. Standard HTTP, same number both sides.
-p 443:443 Host 443 → container 443. Standard HTTPS, same number both sides.
-p 3000:3000 Host 3000 → container 3000. Common for Node dev servers.
-p 5433:5432 Host 5433 → container 5432. Run Postgres on a non-default host port.

So 443:443 isn’t special — it’s the same host:container rule with matching numbers, because HTTPS conventionally uses 443 on both sides. There’s no hidden meaning; people just map the standard port to itself.

Why a running container can still refuse connections

This is the most common support question, and it almost always comes down to one of three things.

You didn’t publish the port. A container with no -p is reachable by other containers on its network, but not from your host. Add the mapping.

You published the wrong container port. The right-hand number must match what the app actually listens on inside the container. If a Node app listens on 3000 but you wrote -p 8080:80, you’re forwarding to port 80 where nothing’s running. Check the app’s real port and the image’s EXPOSE line.

The app binds to 127.0.0.1 inside the container. This one’s sneaky. If the process inside the container listens on 127.0.0.1 (localhost) instead of 0.0.0.0 (all interfaces), it only accepts connections from inside the container. Docker’s forwarded traffic arrives on the container’s external interface, gets refused, and you see “connection refused” despite a perfect -p flag.

# Inside the container, the app must listen on 0.0.0.0, not 127.0.0.1
# e.g. a dev server flag like --host 0.0.0.0, or app config binding to all interfaces

Binding to 127.0.0.1: the security-critical part

By default, -p 5432:5432 binds the host port to all host interfaces — 0.0.0.0. On a laptop behind a router that’s mostly fine. On a VPS with a public IP, it means your database is now reachable from the internet. This is one of the most common ways people accidentally expose a database.

Prefix the host port with 127.0.0.1 to bind it to the loopback interface only:

# Reachable only from the host itself — not the LAN, not the internet
docker run -d -p 127.0.0.1:5432:5432 postgres:16

Now the port answers only to connections originating on the host (local tooling, an SSH tunnel), never from outside. Use this for anything that doesn’t need to be public: databases, admin panels, metrics endpoints, message brokers.

Where a published port is reachable from

-p 5432:5432 All interfaces (0.0.0.0). LAN and public IP included — risky on a server.
-p 127.0.0.1:5432:5432 Host only. Safe default for databases and internal tools.
-p 10.0.0.5:5432:5432 A specific host interface only, e.g. a private network NIC.
No -p at all Not reachable from the host; only from containers on the same network.

The best option for an internal service is often no published port at all. If only other containers talk to it, leave it on a shared Docker network and skip -p entirely — the Docker networking guide explains why containers reach each other by name without any publishing.

UDP, ranges, and ephemeral ports

TCP is the default. For UDP services, append /udp:

# DNS over UDP
docker run -d -p 53:53/udp mydnsimage

Some services need both protocols on the same port — publish it twice:

docker run -d -p 53:53/tcp -p 53:53/udp mydnsimage

You can publish a contiguous range in one go:

docker run -d -p 7000-7010:7000-7010 myapp

And if you don’t care which host port you get, give only the container port and let Docker pick a random high one:

docker run -d -p 80 nginx
docker port <container>     # shows which host port Docker assigned

Ephemeral mapping is handy for throwaway tests and for running many copies of the same image without planning ports. For anything you’ll connect to deliberately, pin the host port.

EXPOSE is documentation, not publishing

A Dockerfile EXPOSE 80 line trips people up because it sounds like it opens a port. It doesn’t. EXPOSE is metadata — it documents which port the app uses, and it’s what -P (capital P) reads to auto-publish. By itself it makes nothing reachable from the host.

EXPOSE 80          # documents the port; does NOT publish it
docker run -d -P myimage     # -P publishes every EXPOSEd port to random host ports

So EXPOSE plus -P auto-publishes to random ports; EXPOSE alone does nothing for host access. To actually control where a port lands, use explicit -p host:container. If you hit a clash between two containers wanting the same host port, the port already allocated fix walks through finding and freeing it.

Port mapping in Docker Compose

Compose uses a ports: list, and each entry follows the same host:container rule. The long and short forms both work:

services:
  web:
    image: nginx
    ports:
      - "8080:80"                  # host 8080 → container 80
  db:
    image: postgres:16
    ports:
      - "127.0.0.1:5432:5432"      # localhost-only, safe on a server
    environment:
      POSTGRES_PASSWORD: change-me

The same security point applies in Compose: that 127.0.0.1 prefix on the database is what keeps it off the network. Better still, if web is the only thing that talks to db, drop the ports: block from db completely and let them communicate over the project network. The long syntax spells out each field if you prefer it explicit:

    ports:
      - target: 80
        published: 8080
        protocol: tcp
        mode: host

Quick reference

Port mapping sanity checks

  • Order is host:container — host port first, always
  • The container port must match what the app listens on inside
  • The app inside must bind 0.0.0.0, not 127.0.0.1
  • Bind public-server databases to 127.0.0.1 on the host
  • Internal-only services need no -p at all
  • Add /udp for UDP services; publish twice for TCP+UDP
  • EXPOSE documents a port; -p (or -P) actually publishes it

Wrapping up

Port mapping is forwarding with a fixed grammar: -p host:container, host first. Match the container side to whatever the app listens on, make sure that app binds 0.0.0.0 inside, and you’ve solved most connectivity problems. 443:443 and 8080:80 are the same rule with different numbers — nothing more.

The part worth burning into memory is the security default: a bare -p 5432:5432 on a public server exposes that service to the internet, while -p 127.0.0.1:5432:5432 keeps it local, and no -p at all keeps it strictly between containers. Choose deliberately for every port you publish. For the bigger picture on how containers talk without publishing, read Docker networking for beginners; to resolve clashes, see fixing port already allocated; and browse more in the Docker & Containers guides.

Frequently asked questions

What does 8080:80 mean in Docker?

It's host:container. The host listens on port 8080 and forwards traffic to port 80 inside the container. You'd reach the app at http://localhost:8080 in your browser, while the container's own process still listens on 80 internally. The two numbers are independent and don't have to match.

Which port comes first, host or container?

Host first, container second: -p HOST:CONTAINER. So -p 8080:80 publishes container port 80 on host port 8080. Getting the order backwards is the most common port-mapping mistake and usually shows up as 'connection refused' because nothing is listening where you expect.

How do I bind a port to localhost only?

Prefix the host port with 127.0.0.1, for example -p 127.0.0.1:5432:5432. The port is then reachable only from the host itself, not from the LAN or internet. This is the right default for databases and admin interfaces you don't want exposed.

Why is my container running but not reachable?

Usually one of three things: you didn't publish the port (-p missing), you published the wrong container port (the app listens on a different port inside), or the app inside binds to 127.0.0.1 instead of 0.0.0.0 so it never accepts forwarded traffic. Check all three.

How do I map a UDP port?

Append /udp to the mapping, for example -p 53:53/udp. TCP is the default if you don't specify. Services like DNS or WireGuard need UDP, and some need both — then you publish the port twice, once as /tcp and once as /udp.

What does publishing a port without a host port do?

Writing -p 80 (just the container port) publishes it to a random high host port chosen by Docker. Run docker port to see which one it picked. This is useful for tests where you don't care about the exact host port, but for real services you usually pin a specific one.

Sources & further reading

Official vendor documentation referenced while writing this guide.

SD

Sysadmin Desk

Infrastructure & Cloud

Hands-on guidance for infrastructure, virtualization, and containers — Hyper-V, VMware, Docker, and the day-to-day operations work that keeps environments running.

MCSA Guru provides independent, educational IT guidance. Microsoft, Windows, Windows Server, Microsoft 365, Exchange, and Microsoft Teams are trademarks of Microsoft Corporation; Docker is a trademark of Docker, Inc. MCSA Guru is not affiliated with or endorsed by Microsoft or Docker. Always test changes in a safe environment before applying them in production.

Related guides

Fixing something right now?

Jump straight into the guide library or search for the exact error or task you are dealing with.