Latest

Docker in practice: images, containers, volumes, networks, ports and the compose.yaml file

Third guide in the Docker series: what images, containers, volumes and networks really are, how published ports work and why they bypass ufw, and how to describe a service in a compose.yaml file with a complete example to try, change and remove without leaving traces.

By 6 min read
Images, volumes, networks and compose.yaml

In the previous guide we started nginx with a long docker run (installing Docker Engine and Compose). It works, but a few weeks later nobody remembers which options a container was created with. The answer is the compose.yaml file: it describes the service in a readable way, can be kept, copied and recreated identically. First, though, you need four concepts, because everything else (Portainer, AdGuard Home, Pi-hole) builds on them.

The four concepts in one table

Concept What it is Analogy
Image The read-only package with program, libraries and starting configuration, downloaded from a registry such as Docker Hub The installation DVD
Container A running instance of an image, isolated from the rest of the system; it can be stopped, deleted and recreated The installed, running program
Volume Where the container keeps data that must survive when the container is recreated The Documents folder
Network An internal virtual network between containers, with private addresses and name resolution A dedicated switch

The golden rule: anything that is not in a volume is lost when the container is recreated, for example on every update. That is why every serious service declares where it keeps its data.

Image tags: latest or a fixed version?

An image is written as name:tag, for example nginx:alpine or adguard/adguardhome:latest. The latest tag does not mean “always up to date”: it means “the latest version at the time of download”. It stays that way until you pull the image again.

  • Moving tag (latest, lts): convenient for a lab, you update when you want with a pull.
  • Fixed tag (for example a version number): recommended when an unplanned update could break something. You update by changing the number in the file.

Volumes and bind mounts: two ways to keep data

Type How it is written Where the data lives When to use it
Named volume data:/path/in/container In /var/lib/docker/volumes/, managed by Docker Data you should not touch by hand (databases, Portainer)
Bind mount /opt/stacks/app/conf:/path/in/container In a server folder of your choice Configuration you want to read, copy and back up easily

In this series we use bind mounts under /opt/stacks/NAME for services (one folder per service, easy to back up) and a named volume only for Portainer, as its documentation does.

Published ports: who can reach the container

A container cannot be reached from outside until you publish a port: "8080:80" means “the server’s port 8080 forwards to the container’s port 80”. Two things to know:

  • Without an address, Docker publishes the port on all the server’s interfaces (0.0.0.0): it is reachable from the whole local network.
  • To make it reachable only from the server itself, write "127.0.0.1:8080:80".

As mentioned in the first guide, ports published by Docker bypass ufw rules: Docker creates its own rules in the kernel firewall. To restrict access, the simplest way is to bind the port to 127.0.0.1 or a specific address; for finer rules Docker says to use the iptables DOCKER-USER chain.

Networks: containers find each other by name

Compose automatically creates a network for each project (called PROJECTNAME_default). Inside it every service can reach the others by its own name: if a service is called db, the application connects to db:5432 without knowing any IP address. So when two containers need to talk, you do not publish ports: you publish only those you need to open from the browser or the network.

Complete example: a test site with compose.yaml

Create a project folder and an HTML page to serve:

mkdir -p ~/stack/site/html
cd ~/stack/site
echo '<h1>It works: page served by a container</h1>' > html/index.html
nano compose.yaml

File content:

services:
  web:
    image: nginx:alpine
    container_name: site-test
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    restart: unless-stopped
Key Meaning
services The project’s services (here just one, web)
image The image to use
container_name A fixed container name; without it Compose generates one
ports Published ports, always in quotes
volumes The project’s html folder mounted read-only (:ro) where nginx looks for pages
restart What to do after an error or a server reboot

You no longer need the version: line found in many old guides: Compose v2 and v5 ignore it. Start the project:

docker compose up -d
docker compose ps
docker compose up -d creates the site_default network and the site-test container, docker compose ps shows it on port 8080
Compose creates the project network and the container; the project name is the folder name. Reconstructed screenshot.

Open http://SERVER-IP:8080: you see your page. Now edit html/index.html and reload the browser: the page changes without restarting anything, because the container reads the server folder. Also try changing the port in the file (for example "8081:80") and run docker compose up -d again: Compose notices the configuration has changed and recreates only that container.

Restart policies

Value Behaviour
no Never restarted automatically (default)
on-failure Restarted only if it exits with an error
unless-stopped Always restarted, including after a server reboot, unless you stopped it
always Always restarted, even if you stopped it by hand, at the next Docker start

For the services in this series we use unless-stopped.

Compose commands

Command (from the project folder) What it does
docker compose up -d Creates or updates and starts the services
docker compose ps Status of the project’s services
docker compose logs -f Follows the logs of all services
docker compose pull Downloads new versions of the images
docker compose down Stops and removes containers and network (bind mounts stay)

Variables and passwords: the .env file

Passwords and settings that differ from server to server do not belong in the compose file: put them in a .env file in the same folder and reference them with ${NAME}.

# .env
TZ=Europe/London
ADMIN_PASSWORD=change-this-password
    environment:
      TZ: ${TZ}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}

Protect the file with chmod 600 .env and never copy it to a public repository. In Portainer you do the same in the stack’s Environment variables section, as we will see.

Cleaning up

docker compose down
docker system df
docker image prune
docker system df shows space used by images, containers and volumes, docker image prune asks for confirmation before deleting
docker system df summarises the space used; image prune removes only “dangling” images with no name. Reconstructed screenshot, example values.

docker image prune deletes only the unnamed images left behind by updates; with -a it also deletes images not used by any container, which will have to be downloaded again. Avoid docker system prune --volumes unless you know exactly what the volumes contain: it would also delete the data of stopped services.

Another compose example, with a real service, is in the guide to Open WebUI with Ollama in Docker. In the next guide we install Portainer, which lets you do all of this from the browser.

Sources