RackUp IT - View Cloud Hosting Plans

Get started now or speak directly with our engineering team for specialized assistance.

Logo
DevelopmentWEB & APPCalculatorTOOLServicesProductBlogOur TeamCareers
View Plans
Home>Blog>Docker Port Mapping Explained: -p, EXPOSE and Compose ports
DevOps & Tutorials

Docker Port Mapping Explained: -p, EXPOSE and Compose ports

Alex Vance
Alex Vance
Published Nov 26, 2026•✓ Verified Expert Reviewed•3 min read
Docker port mapping cover

Key Takeaways

  • ✓The format is -p HOST_PORT:CONTAINER_PORT, host first.
  • ✓EXPOSE only documents a port; -p publishes it.
  • ✓Compose services reach each other by service name without published ports.
  • ✓Published ports bypass UFW, so bind to 127.0.0.1 behind a reverse proxy.

Table of Contents

  • •Docker Port Mapping Explained: -p, EXPOSE and Compose ports
  • •The -p Syntax
  • •See Which Ports Are Published
  • •EXPOSE Is Documentation, Not Publishing
  • •Ports in Docker Compose
  • •The Security Detail: Published Ports Bypass UFW
  • •Common Problems
  • •Frequently Asked Questions
  • •Run Containers Without Port Juggling

Docker Port Mapping Explained: -p, EXPOSE and Compose ports

A container has its own network, so a web server listening on port 80 inside it isn’t reachable from your browser until you publish that port to the host. That’s what port mapping does: -p 8080:80 sends traffic arriving on the host’s port 8080 to port 80 in the container. This guide explains the syntax, the difference between -p and EXPOSE, how ports work in Docker Compose, and the security detail most tutorials skip.

Quick answer: the format is -p HOST_PORT:CONTAINER_PORT. Use -p 127.0.0.1:8080:80 to publish only on localhost when a reverse proxy sits in front, because a plain -p 8080:80 is open to the internet and can bypass UFW.

The -p Syntax

OptionMeaning
-p 8080:80Host port 8080 on all interfaces → container port 80
-p 127.0.0.1:8080:80Only on the host’s localhost → container port 80
-p 80:80 -p 443:443Several ports
-p 5353:53/udpA UDP port
-p 8000-8005:8000-8005A range
-p 80Container port 80 → a random free host port
-PPublish every EXPOSEd port to random host ports
docker run -d --name web -p 8080:80 nginx
curl http://localhost:8080

The order matters: host first, container second. Getting it backwards is the most common mistake.

See Which Ports Are Published

docker ps                  # PORTS column, e.g. 0.0.0.0:8080->80/tcp
docker port web            # mappings for one container

0.0.0.0 (or [::]) means the port is open on every network interface, including your public IP.

EXPOSE Is Documentation, Not Publishing

EXPOSE 80 in a Dockerfile records which port the app listens on. It does not make the port reachable from the host. You still need -p (or -P, which publishes exposed ports to random host ports). Containers on the same Docker network can reach each other’s ports whether or not they’re exposed or published.

Ports in Docker Compose

services:
  web:
    image: nginx
    ports:
      - "127.0.0.1:8080:80"
  api:
    build: ./api
    expose:
      - "3000"
  db:
    image: postgres:17
  • ports: publishes to the host, same syntax as -p. Quote the values, because YAML can misread some numbers with colons.

  • expose: documents the port for other services; it isn’t published to the host.

  • Services reach each other by service name on the Compose network: the web service connects to http://api:3000 and the API to db:5432. The database needs no published port at all.

The Security Detail: Published Ports Bypass UFW

Docker publishes ports by adding its own iptables rules, which are evaluated before UFW’s. So -p 5432:5432 on a server can expose your database to the internet even though ufw status shows the port as closed. Safe habits:

  • Don’t publish database or internal service ports at all; let containers talk over the Docker network.

  • Bind web apps to localhost (127.0.0.1:8080:80) and put Nginx, Caddy or Traefik in front on 80/443.

  • Use your cloud provider’s firewall as an outer layer.

More on this in how to open ports on a VPS.

Common Problems

  • “port is already allocated” / “address already in use”: another container or service uses the host port. Find it with sudo ss -tlnp | grep :8080 or docker ps, then pick another host port.

  • Connection refused from the host: the app inside listens on 127.0.0.1. Inside a container it must listen on 0.0.0.0.

  • Works on localhost, not from outside: it’s bound to 127.0.0.1, or a cloud firewall blocks it.

  • Can’t change ports on a running container: port mappings are fixed when a container is created. Recreate it with the new -p, or edit Compose and run docker compose up -d.

Frequently Asked Questions

What does -p 8080:80 mean in Docker?
Traffic to port 8080 on the host goes to port 80 inside the container. The host port comes first.

What is the difference between EXPOSE and -p?
EXPOSE documents which port the app uses; -p actually publishes a port to the host so it can be reached from outside.

How do I publish a port only to localhost?
Add the IP: -p 127.0.0.1:8080:80. The port is then reachable only from the host itself, for example by a reverse proxy.

Do containers need published ports to talk to each other?
No. Containers on the same Docker network reach each other directly by name and internal port.

Can I add a port mapping to an existing container?
No. Recreate the container with the new mapping; with Compose, change ports and run docker compose up -d.

Run Containers Without Port Juggling

On RackUp Cloud Apps your container gets HTTPS on your domain automatically, with Git deploys and a private database, from $2.44 a month. Running your own Docker host? The Developer VPS gives you root access, 8 vCPU and 12 GB RAM for $35 a month. See plans.

← Previous ArticleBluehost Alternatives (2026): WordPress Hosting Without the Renewal Price JumpNext Article →Vultr Alternatives in 2026: VPS Hosting for Developers Compared
Interactive 60-Second Tool

Unsure Which Cloud Hosting Plan Fits Your Site?

Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.

Free Migration 99.9% Uptime Target
Calculate My Hosting Build
Alex Vance
Written ByAlex Vance

Cloud Infrastructure Engineer.

99.9% Uptime Target

RackUp IT - View Cloud Hosting Plans

View Hosting Plans

View Hosting Plans

CONTENTS

  • Docker Port Mapping Explained: -p, EXPOSE and Compose ports
  • The -p Syntax
  • See Which Ports Are Published
  • EXPOSE Is Documentation, Not Publishing
  • Ports in Docker Compose
  • The Security Detail: Published Ports Bypass UFW
  • Common Problems
  • Frequently Asked Questions
  • Run Containers Without Port Juggling

Categories

  • VPS Hosting
  • DevOps & Tutorials
  • WordPress Hosting
  • Cloud Hosting
  • Server Stack
  • Networking & Security
  • Web Hosting / Managed Cloud
  • E-Commerce
  • Hosting & SEO
  • Hosting Benchmarks
  • Cloud Backup & DevOps
  • Managed VPS

Recent Posts

  • Vultr Alternatives in 2026: VPS Hosting for Developers Compared
  • Bluehost Alternatives (2026): WordPress Hosting Without the Renewal Price Jump
  • Docker Engine vs Docker Desktop: Which One Do You Need?

Tags

DockerNetworkingDocker ComposeTutorial

Related Posts

Docker Engine vs Docker Desktop coverDevOps & Tutorials
Nov 24, 2026

Docker Engine vs Docker Desktop: Which One Do You Need?

Docker Engine is the open-source runtime for Linux; Docker Desktop is a desktop app with a VM, GUI and its own licence. Which to use on servers and laptops.

Read Article →
ERR_CONNECTION_REFUSED: causes and fixesDevOps & Tutorials
Nov 18, 2026

ERR_CONNECTION_REFUSED: What It Means and How to Fix It

ERR_CONNECTION_REFUSED means your browser reached the address and the connection was rejected. Fixes for visitors, for localhost and for server owners.

Read Article →
docker compose up --build coverDevOps & Tutorials
Nov 18, 2026

docker compose up --build Explained (With Examples)

docker compose up --build builds images before starting containers. When you need it, when you don't, and the related flags people confuse, with examples.

Read Article →
Logo

Expert Web & App Development, UI/UX Design, Branding, and Consulting to scale your business.

Company

  • About Us
  • Blog
  • Contact
  • Our Team
  • Careers

Services

  • Web Development
  • App Development
  • UI/UX Design
  • Consulting
  • Managed WordPress VPS
  • Website Speed Test

Subscribe to our newsletter

We accept
  • bKash
  • Nagad
  • Rocket
  • Upay
  • CellFin
  • Meghna Pay
  • OK Wallet
  • Tap
  • mCash
  • DBBL Nexus
  • Visa
  • Mastercard
  • American Express
  • Crypto
Pay with Visa, Mastercard, American Express, DBBL Nexus, CellFin, Rocket, bKash, Tap, Meghna Pay, Nagad, mCash, Upay and OK Wallet. Verified by EPS

© 2026 Rackup IT Solution. All rights reserved.

Privacy PolicyTerms of Service