>_ IzzyLab

Hands-on task

This task is step 3 of the course “Docker networking”

Publishing ports: 127.0.0.1 vs 0.0.0.0

easy~10 min

The server runs two containers:

  • admin (izzylab/minihttp:1, NAME=admin) is an internal admin panel. It must open only from the server itself: http://127.0.0.1:8080/, and not on the server's external address;
  • site (nginx:1.27-alpine) is the public website: port 8081 on all of the server's addresses.

Right now it's the other way round: the admin panel is exposed to the outside and the site opens only locally. Fix it. You can't change the port binding of a running container — recreate both with the same names, images and NAME. The server's external address is on the eth0 interface.

The lab has its own Docker with no internet access; the images are already loaded: izzylab/minihttp:1, alpine:3.21, busybox:1.36, nginx:1.27-alpine. izzylab/minihttp is a tiny HTTP service on port 8080: GET / answers <NAME> ok, GET /call itself goes to http://<UPSTREAM>/ and answers <NAME> -> <the neighbor's reply> (or 502), GET /env shows its settings. It is configured with the environment variables NAME, PORT, BIND (the address it listens on) and UPSTREAM.

What you'll practise:
  • docker run -p
  • port publishing

Topic breakdown

-p 8080:80 publishes a container's port on all of the host's addresses: Docker adds a DNAT rule to iptables and (by default) starts docker-proxy listening on 0.0.0.0:8080. On a server with a public address that means "open to the whole internet" — and Docker's rules come before many firewalls, so ufw won't close such a port. To make a service reachable only from the server itself (behind a reverse proxy or through an SSH tunnel), publish it on loopback: -p 127.0.0.1:8080:80. Any of the host's addresses will do — for example, only the internal interface. You can't change the publishing of a running container: it's set at creation, so the container is recreated.

What's checked

  1. admin is published only on 127.0.0.1:8080
  2. From the server, the admin panel opens on 127.0.0.1:8080
  3. The admin panel doesn't open on the server's external address
  4. site is published on port 8081 on all addresses
  5. The site opens on the server's external address

What it looks like