You do not have to use Docker. On a server running more than one thing, it saves enough pain to be worth the hour of learning.
What it buys you
- No dependency conflicts. Two apps needing different PHP or Python versions coexist without argument.
- The same thing everywhere. What runs on your laptop runs on the server, which removes an entire category of deployment mystery.
- Clean removal. Deleting a container leaves nothing behind. Uninstalling software installed with apt rarely does.
- Trivial rollback. Run the previous image tag.
Install from Docker's repository, not apt's
Ubuntu's packaged docker.io is usually old and omits Compose.
Use the official repository:
sudo apt update
sudo apt install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Run Docker without sudo
sudo usermod -aG docker $USER
Log out and back in for it to apply. Understand the trade: membership of
the docker group is effectively root, because a container can
mount the host filesystem. Acceptable on a server you administer, not
something to hand out.
Verify
docker run --rm hello-world
docker compose version
Note docker compose as two words. The old
docker-compose binary is superseded, and mixing the two is a
frequent source of confusing errors in older guides.
A first real container
docker run -d --name web -p 127.0.0.1:8080:80 nginx
curl http://localhost:8080
Note the 127.0.0.1: prefix — that is the firewall lesson from
the last chapter applied. This container is reachable from the server and not
from the internet, which is exactly right: the reverse proxy in chapter six
will be the only thing facing outward.
Clean up:
docker rm -f web
Housekeeping that matters
Docker accumulates images and will fill a 25 GB disk faster than you expect. Monthly:
docker system df # what is using space
docker system prune -a # remove unused images and containers
Run df -h before prune -a, and note that it
removes images not currently in use by a container — including ones you may
want to roll back to.