World map

 

BM: Baca dalam Bahasa Melayu

World D3: Lockers & Phone Lines

Call containers by name

You will learn: to reach another container by its name, not by localhost.

Checked with Docker Engine 29 on 31 Oct 2026

The problem

A new box on shopnet asks localhost for the menu. wget says: can't connect to remote host (127.0.0.1): Connection refused. Docker transcript: $ docker run --rm --network shopnet alpine:3.22 wget -qO- http://localhost wget: can't connect to remote host (127.0.0.1): Connection refused

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • menurunning
    from nginx:1.30-alpine shopnet
  • cacherunning
    from alpine:3.22

Frozen packs (images)

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

Phone lines (networks)

  • shopnetmenu

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

A new box wants the menu, so it asks localhost. Connection refused. Plain words: nobody serves pages inside this box itself.

Watch

Tap each point on the timeline.

On your own line, Docker keeps a phone book. Say a box’s name, get its address. This is container DNS: Docker's phone book on your own network. Say a container's name, get its address..

Try it

From a new box on shopnet, fetch the page at http://menu.

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • menurunning
    from nginx:1.30-alpine shopnet
  • cacherunning
    from alpine:3.22

Frozen packs (images)

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

Phone lines (networks)

  • shopnetmenu

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

Predict

Inside a container, who is localhost?

Pick one answer.

Check

wget to menu on port 8080 fails: can't connect to remote host, Connection refused. Docker transcript: $ docker run --rm --network shopnet alpine:3.22 wget -qO- http://menu:8080 wget: can't connect to remote host (172.18.0.2): Connection refused

Your laptop~

  • No open hatches

Lunchboxes (containers)

  • menurunning
    from nginx:1.30-alpine shopnet
  • cacherunning
    from alpine:3.22

Frozen packs (images)

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

Phone lines (networks)

  • shopnetmenu

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

menu:8080 is refused too. nginx listens on port 80 inside the box. 8080 is only a hatch on your laptop.

Check

An app box talks to db and menu. Good setting, or not?

Swipe right for good, left for needs work. Or use the buttons.

1 / 5DB_HOST=db
DB_HOST=db

You did it!

You can now connect containers by name.

Go deeper (optional)

Docker’s phone book lives at 127.0.0.11 inside each box on your own network. --network-alias adds a second name. Compose puts every service on one network and names them for you (World D4).

Real rep (optional):

Real mission

  1. docker network create practice-net && docker run -d --name menu --network practice-net nginx:1.30-alpine
  2. docker run --rm --network practice-net alpine:3.22 wget -qO- http://menu
  3. docker run --rm --network practice-net alpine:3.22 wget -qO- http://localhost
  4. docker rm -f menu && docker network rm practice-net

0 of 4 steps done · Your ticks are saved.