>_ IzzyLab

Hands-on task

This task is step 8 of the course “Docker from scratch”

A site from a container: publishing a port

easy~6 min

Run nginx in the background from the nginx:1.27-alpine image with the name site so that the site opens from the server at http://127.0.0.1:8080/. Inside the container nginx listens on port 80.

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

Topic breakdown

A container has its own network: nginx inside listens on port 80 of its own network namespace, and that port isn't open on the server by itself. To reach the service from the server and from outside, you publish the port with the -p flag at startup:

docker run -d --name api -p 9000:8080 izzylab/minihttp:1
curl http://127.0.0.1:9000/    # reaches port 8080 in the container

On the left is the server port, on the right the port in the container. You can repeat the flag to publish several ports. docker port name and the PORTS column in docker ps show what's forwarded where.

Publishing is set only at creation: you can't add or change it on a running container — you recreate the container.

What's checked

  1. site from nginx:1.27-alpine is running
  2. Container port 80 is published on server port 8080
  3. The site opens at http://127.0.0.1:8080/

What it looks like