October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Docker Ports Explained: EXPOSE, -p, -P, and Container Access

Docker EXPOSE documents a container port but does not publish it. Learn when to use -p, -P, runtime --expose, and localhost binding.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

EXPOSE documents the port an application is expected to listen on inside a Docker container; it does not publish that port on the host. To make a container port reachable through a host port, use docker run -p HOST_PORT:CONTAINER_PORT IMAGE. For example, -p 8080:80 maps host port 8080 to container port 80.

What Docker’s EXPOSE instruction does

In a Dockerfile, EXPOSE records a port and protocol as image metadata: it tells image users which container port the application is expected to use. Docker describes it as documentation between the image builder and runner. It does not create a host mapping, add a firewall rule, or make the application listen. The application itself must bind to the port inside the container. Dockerfile reference

EXPOSE 80

TCP is the default protocol, so EXPOSE 80 means TCP port 80. To document UDP, write EXPOSE 80/udp. If an application uses both TCP and UDP on port 80, declare both separately:

EXPOSE 80/tcp
EXPOSE 80/udp

These declarations describe the container port; publication is a separate runtime choice.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

EXPOSE vs. –expose vs. -p vs. -P

Option What it does Publishes to the host?
EXPOSE 80 in a Dockerfile Records the intended container port and protocol in image metadata. No.
--expose 80 on docker run Adds exposed-port metadata for that container at runtime. No. It can mark a port for publication by -P.
-p 8080:80 on docker run Maps a specified host port to a specified container port. Yes.
-P on docker run Publishes declared exposed ports using randomly selected host ports. Yes.

The Docker CLI reference documents the runtime --expose, -p, and -P options. Docker run reference

Publish a container port with -p

Use -p (or its long form, --publish) when you want a known host port. The order is HOST_PORT:CONTAINER_PORT:

docker run -p 8080:80 nginx

Here, connections arriving at host port 8080 are forwarded to port 80 in the container. The host and container port numbers do not have to match. Docker’s publishing guide describes this as mapping a host port to a container port. Port publishing and mapping

TCP is the default. To publish UDP port 80 in the container through UDP host port 8080, specify the protocol:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
docker run -p 8080:80/udp nginx

To publish both protocols on those port numbers, make both mappings explicit:

docker run -p 8080:80/tcp -p 8080:80/udp nginx

The Docker run reference also accepts SCTP as a protocol option. Select the protocol your application actually uses; a TCP mapping does not publish its UDP traffic. Docker run reference

Choose who can reach a published port

If you omit the host IP, Docker publishes the mapped port on all host addresses by default. That can make the service reachable beyond the host, depending on routing, firewall rules, and other network controls. Docker warns that publishing container ports is insecure by default; this is a warning about the broad default bind, not a claim that every published service is necessarily reachable from the public internet. Docker port-publishing security guidance

For a service intended only for software running on the Docker host, bind the host side to loopback:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
docker run -p 127.0.0.1:8080:80 nginx

This documented configuration restricts access to the host’s loopback address. Docker notes a specific historical exception: on hosts running Docker releases older than 28.0.0, other machines on the same layer-2 network could reach ports published to localhost. Keep that caveat scoped to those older releases. Docker port-publishing guidance

Docker manages networking rules itself, so do not assume a host firewall tool’s default policy necessarily blocks a published port. Actual exposure also depends on the Docker version, network mode, daemon settings, IP version, routing, and firewall configuration. Check the port-publishing guide when diagnosing a setup that differs from the standard bridge-network case. Docker networking guide

Use -P for automatic host-port selection

-P publishes ports declared with EXPOSE (or runtime --expose) to randomly selected host ports. It is useful when you do not need a fixed host port, such as when running several instances that would otherwise compete for the same one.

docker run -P nginx
docker port CONTAINER

Use docker port CONTAINER to see the mappings Docker selected. The run reference says these random host ports are chosen within the ephemeral port range defined by /proc/sys/net/ipv4/ip_local_port_range. Unlike -P, -p lets you choose the host port yourself. Docker run reference

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Docker Container Linux Devops Programming Coding T-Shirt
  • Docker, Docker Swarm, Docker Compose, Programmer, Developer, Coding, Programming, Software Engineer, Code, DevOps, Deploy, Deployment, Kubernetes, Salt, Puppet, Chef, Terraform, Container, AWS, Azure, Cloud, Geek, Funny, Computer, Software, Tech, IT
  • Integration, Scrum, Compile, Compilation, Science, Bug, Debug, Python, Linux, Java, Javascript, Scala, Dotnet, Kotlin
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Container-to-container access does not require -p

Publishing is for access through the Docker host; it is not a prerequisite for communication between containers on a shared Docker network. On a bridge network, containers connected to that network can reach one another using their container ports, while the Docker host can also access those ports. A container on another network, or a machine outside the host, does not ordinarily gain access merely because the application listens inside the container. Use a host publication or a separately configured route when host or external access is needed. Docker port-publishing guide

Keep the distinction clear: a process may be listening on a container port; another container on its network may be able to reach it; and a host port may or may not be published. Those are separate conditions.

Docker Desktop adds a forwarding layer

On Docker Desktop, published traffic passes through the Desktop backend, which listens on the selected host port and forwards the connection into the Linux VM before it reaches the container. The backend process is named com.docker.backend on Mac and com.docker.backend.exe on Windows; the networking guide identifies qemu for Linux. This Desktop-specific path can matter when troubleshooting firewall, VPN, or endpoint-security behavior. Docker Desktop networking

Quick troubleshooting checks

  • The host cannot connect: confirm the application is listening on the container port, then check whether you used -p or -P. EXPOSE alone does not publish a port.
  • The connection uses the wrong port: read -p as host first, container second; inspect automatic mappings with docker port CONTAINER.
  • A service should be local-only: bind the host side to 127.0.0.1 rather than omitting the host address.
  • Another container needs access: connect both containers to the same Docker network and use the application’s container port; host publication is not needed for that path.
  • Desktop networking behaves differently: account for the backend-to-VM forwarding layer and any host VPN or security software.

For unusual routing, bridge gateway modes, direct routing, firewall behavior, or Swarm services, consult Docker’s networking documentation for the relevant configuration. Swarm service publishing has separate ingress and host modes; it is not interchangeable with the single-container docker run -p example. Docker port-publishing guide

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.