Docker packages an application with everything it needs into a container that runs the same way on any Linux server. Instead of installing PHP, Node.js, databases and their dependencies directly on the host, you run them as containers that you can start, stop, upgrade and remove cleanly. Many self-hosted applications — n8n, Nextcloud, Uptime Kuma, Mailcow, Gitea — are distributed primarily as Docker images.
This guide installs Docker Engine and the Compose plugin from Docker’s official repositories and covers the security and housekeeping details that tutorials often skip.
Requirements
- A 64-bit Linux server: Ubuntu 22.04/24.04, Debian 11/12, AlmaLinux or Rocky 8/9.
- At least 1 GB of RAM for light containers; 2 GB+ for databases and heavier apps.
- KVM virtualisation or a dedicated server. (Server.HK cloud VPS use KVM, so Docker runs normally.)
Remove old or unofficial packages first:
# Ubuntu / Debian
for p in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt remove -y $p; done
Install on Ubuntu
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
Install on Debian
The same steps, replacing ubuntu with debian in both URLs.
Install on AlmaLinux and Rocky Linux
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start and test
sudo systemctl enable --now docker
sudo docker run --rm hello-world
docker compose version
hello-world downloads a tiny image and prints a confirmation message.
Run Docker without sudo (and what that means)
sudo usermod -aG docker $USER
# log out and back in, then:
docker ps
Be aware: membership of the docker group is equivalent to root access — anyone in it can start a container that mounts the host’s filesystem. Only add trusted administrator accounts. Never expose the Docker API socket (/var/run/docker.sock) or TCP port 2375 to the internet; an open Docker API is one of the most common ways servers get taken over for crypto mining.
Core concepts in five minutes
| Concept | What it is | Example |
|---|---|---|
| Image | A read-only template | nginx:1.27, postgres:16 |
| Container | A running instance of an image | docker run nginx |
| Volume | Persistent storage that survives container removal | Database files |
| Bind mount | A host directory mapped into a container | ./config:/etc/app |
| Network | Lets containers talk to each other by name | db, redis |
| Port publishing | Maps a host port to a container port | -p 8080:80 |
Example:
docker run -d --name web -p 127.0.0.1:8080:80 --restart unless-stopped nginx:1.27
docker ps
docker logs -f web
docker exec -it web sh
docker stop web && docker rm web
Containers are disposable; data is not. Anything written inside a container’s filesystem is lost when you remove it. Databases, uploads and configuration must live in volumes or bind mounts.
Pin versions. nginx:1.27 or postgres:16 is predictable; latest can jump to a new major version on the next pull.
Published ports bypass UFW and firewalld
This surprises many people. Docker writes its own firewall rules, and a port published with -p 8080:80 is reachable from the internet even if UFW denies port 8080.
The simple, reliable fix: publish ports only on localhost and put a reverse proxy (Nginx, Caddy, Traefik) in front for public access:
-p 127.0.0.1:8080:80
In Compose files:
ports:
- "127.0.0.1:8080:80"
Services that only other containers need (databases, Redis) should not publish ports at all — containers on the same Compose network reach them by service name.
Check what is exposed:
sudo ss -tlnp | grep docker-proxy
Limit log size
By default container logs grow without limit and can fill the disk. Set global limits in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "3"
}
}
sudo systemctl restart docker
The setting applies to containers created afterwards; recreate existing ones to apply it.
Keep disk usage under control
Old images, stopped containers and build cache accumulate:
docker system df # what uses space
docker image prune -a # remove unused images
docker container prune
docker builder prune
docker system prune -a --volumes # everything unused — careful: removes unused volumes too
Never run --volumes unless you are sure no stopped container’s data is in an unused volume. See also fixing a full disk.
Changing the data directory
Docker stores everything in /var/lib/docker. If you have a larger second disk, move it:
sudo systemctl stop docker
sudo rsync -aP /var/lib/docker/ /data/docker/
echo '{ "data-root": "/data/docker" }' | sudo tee /etc/docker/daemon.json # merge with existing settings
sudo systemctl start docker
Registry mirrors and network access
Docker Hub can be slow or unreliable from some networks, including mainland China. A server in Hong Kong, Japan or the US pulls images from Docker Hub directly and quickly, which is one practical reason developers serving China often build and run containers outside the mainland. If pulls are rate-limited, log in with a free Docker Hub account (docker login).
Updates
Docker Engine updates come through your normal package updates. To update an application container, pull the new image and recreate the container — with Compose this is two commands; see the Docker Compose guide.
Troubleshooting
| Problem | Check |
|---|---|
permission denied while trying to connect to the Docker daemon socket |
Use sudo, or add the user to the docker group and log in again |
| Container exits immediately | docker logs NAME |
| Port already in use | `sudo ss -tlnp |
| Container cannot reach the internet | DNS in /etc/docker/daemon.json ("dns": ["1.1.1.1","8.8.8.8"]), firewall forwarding rules |
| Disk full | docker system df, log limits |