World map

 

BM: Baca dalam Bahasa Melayu

World D1: First Containers

The order hatch: port mapping

You will learn: to open a door from your laptop into a container with -p.

Checked with Docker Engine 29 on 31 Oct 2026

The problem

docker ps shows web is Up, with 80/tcp and no laptop port. curl localhost:8080 fails: Couldn't connect to server. Docker transcript: $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3bd7b3ef220a nginx:1.30-alpine "/docker-entrypoint.…" 3 seconds ago Up 3 seconds 80/tcp web $ curl localhost:8080 curl: (7) Failed to connect to localhost port 8080 after 0 ms: Couldn't connect to server

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • webrunning
    from nginx:1.30-alpine

Frozen packs (images)

  • nginx:1.30-alpine8 layers · 93.5MB · in use

1 containers (1 running), 1 images, 0 lockers.

web is Up, but the real error says Couldn't connect to server. Plain words: nobody answers at port 8080 on your laptop. The box is sealed.

Try it

Cut an order hatch: plug laptop port 8080 into web’s port 80. This is port mapping: The order hatch: a door from a port on your computer to a port in the container..

Open the order hatch: laptop port 8080 to web port 80.

Tap a laptop port, then a container port.

Your laptop

web

$ docker run -d --name web nginx:1.30-alpine

Watch

The old web is removed. A new web starts with -p 8080:80. docker port web shows 80/tcp -> 0.0.0.0:8080. curl gets the Welcome to nginx! page. Docker transcript: $ docker rm -f web web $ docker run -d --name web -p 8080:80 nginx:1.30-alpine 11f3205b1373ecd5c6a55438fb1cb5fb2c7c3356dba3c8a091ba21e7bd8c86e4 $ docker port web 80/tcp -> 0.0.0.0:8080 $ curl -s localhost:8080 | grep title <title>Welcome to nginx!</title>

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • webrunning
    from nginx:1.30-alpine

Frozen packs (images)

  • nginx:1.30-alpine8 layers · 93.5MB · in use

1 containers (1 running), 1 images, 0 lockers.

A hatch is cut when a box is made. So web is made again. Read -p 8080:80 left to right: laptop first, box second.

Try it

Your turn. Run web with a hatch on 8080. Then curl localhost:8080.

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • No containers yet

Frozen packs (images)

  • alpine:3.221 layers · 12.9MB
  • nginx:1.30-alpine8 layers · 93.5MB

0 containers (0 running), 2 images, 0 lockers.

Predict

web holds hatch 8080. A new box, api, also asks for laptop port 8080. What happens?

Pick one answer.

Check

web already holds laptop port 8080. docker run for api fails: Bind for 0.0.0.0:8080 failed: port is already allocated. api is left Created. Docker transcript: $ docker run -d --name api -p 8080:80 nginx:1.30-alpine b14de4e0bea1426da714c529fab09a838d1915d89754b8a64597c4a6fb07dfa8 docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint api (9c7e32522c4bd5d03e154ce32bed280368ce0eb0de550be443e03db638c7c328): Bind for 0.0.0.0:8080 failed: port is already allocated Run 'docker run --help' for more information

Your laptop~

  • Laptop port 8080 → web port 80

Lunchboxes (containers)

  • webrunning
    from nginx:1.30-alpine 8080→80

Frozen packs (images)

  • alpine:3.221 layers · 12.9MB
  • nginx:1.30-alpine8 layers · 93.5MB · in use

1 containers (1 running), 2 images, 0 lockers.

The real error ends with port is already allocated. Plain words: another box already uses laptop port 8080.

Try it

Give api its own hatch. Any free laptop port works.

api needs a hatch too. 8080 is busy.

Tap a laptop port, then a container port.

Your laptop

web

api

  • -p 8080:80laptop 8080 → web:80
$ docker run -d --name web -p 8080:80 nginx:1.30-alpine$ docker run -d --name api nginx:1.30-alpine

Check

docker run -d -p 3000:80 nginx:1.30-alpine. Which address opens the page?

Pick one answer.

You did it!

You can now open an order hatch into a container.

Go deeper (optional)

Without an address, -p opens the hatch on every network you are on. Others on your Wi-Fi may reach it! -p 127.0.0.1:8080:80 keeps it for your laptop only. More hatch tricks come in World D3.

Real rep (optional):

Real mission

  1. docker run -d --name web -p 127.0.0.1:8080:80 nginx:1.30-alpine
  2. docker port web
  3. docker rm -f web

0 of 4 steps done · Your ticks are saved.