Docker turned "it works on my machine" into "it works on every machine." On a VPS, that means you can run a database, a web app, a background worker, and a monitoring tool side by side, each in its own container with its own dependencies, and rebuild or move any of them in minutes.
This guide covers running Docker on a VPS the practical way: installing Docker Engine on Ubuntu, deploying with Docker Compose, keeping data safe, and avoiding a couple of mistakes that catch almost everyone the first time.
Why use Docker on a VPS?
- Isolation without extra servers. Each app gets its own runtime version and libraries. Your Python 3.12 app and your legacy Node 18 service can coexist.
- Reproducible deployments. A
Dockerfileand acompose.yamldescribe the entire setup. Rebuilding a server becomes a copy-and-run job. - Easy rollbacks. Tag your images, and going back to the previous version is a single command.
- Huge ecosystem. Official images exist for databases, caches, web servers, and countless self-hosted tools.
The trade-offs: containers add a layer you need to understand, they use some extra disk for images, and Docker's networking has quirks (more on that below).
Choosing a VPS for Docker
Docker itself is light, so the right size depends on what you run. A couple of small containers fit on a modest plan, while databases and build steps want more RAM. A few practical points:
- Use KVM-based virtualization. Docker needs a real kernel it can control. Full virtualization like KVM works out of the box; some older container-based VPS types (such as OpenVZ) may not support Docker properly.
- Leave disk headroom. Images and build caches add up. You will prune them regularly, but plan for them.
- Build elsewhere if RAM is tight. Building images can use far more memory than running them. Building in CI and pulling the finished image keeps a small VPS happy.
Step 1: Install Docker Engine on Ubuntu
Ubuntu's own docker.io package works, but Docker's official repository gives you the current Docker Engine and the Compose plugin. Follow Docker's official install documentation for the exact current commands; the process looks like this:
sudo apt update
sudo apt install ca-certificates curl -y
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 docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
Test it:
sudo docker run hello-world
Running Docker without sudo (and what that means)
You can add your user to the docker group:
sudo usermod -aG docker $USER
Log out and back in for it to take effect. Be aware that membership in the docker group is effectively root access, because anyone who can control the Docker daemon can mount the host filesystem into a container. Only add users you would trust with sudo.
Step 2: Deploy with Docker Compose
Typing long docker run commands gets old fast. Compose lets you describe services in one file. Here is an example of a web app with a PostgreSQL database:
services:
app:
image: ghcr.io/yourname/yourapp:1.4.2
restart: unless-stopped
env_file: .env
depends_on:
- db
ports:
- "127.0.0.1:3000:3000"
db:
image: postgres:16
restart: unless-stopped
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Save it as compose.yaml in a project folder, put secrets in a .env file next to it (and keep that file out of Git), then run:
docker compose up -d
docker compose ps
docker compose logs -f app
Things worth noticing in that file:
- Pinned image tags.
yourapp:1.4.2instead oflatestmeans you always know what is running and can roll back. restart: unless-stoppedbrings containers back after a reboot or crash.- The database has no
portsentry. Containers on the same Compose network reach each other by service name (db), so the database never needs to be exposed to the internet.
Step 3: Persist your data
Containers are disposable; your data is not. Anything written inside a container's filesystem disappears when the container is removed. Use:
- Named volumes (like
db-dataabove) for databases and app data managed by Docker. - Bind mounts (
./uploads:/app/uploads) when you want files in a specific host directory.
And back them up. A volume is just data on the VPS disk; if the disk or server fails, the volume goes with it. For databases, take proper dumps (for example docker compose exec db pg_dump ...) on a schedule and copy them off the server.
The firewall trap: Docker and UFW
This catches many people. When you publish a port like "3000:3000", Docker writes its own iptables rules, and those rules can bypass UFW. You might think UFW is blocking port 3000 while the world can reach it anyway.
The simplest defenses:
- Bind published ports to localhost (
"127.0.0.1:3000:3000") when a reverse proxy on the host will handle public traffic. - Don't publish ports at all for internal services like databases and caches.
- Check from an outside machine with a port scanner or
curlto confirm what is actually reachable.
Step 4: Put a reverse proxy in front
Run a reverse proxy to handle domains and HTTPS. You can install Nginx on the host and proxy to 127.0.0.1:3000, or run a containerized proxy such as Caddy or Traefik that can obtain Let's Encrypt certificates automatically. A minimal Nginx location block looks like:
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Day-to-day maintenance
- Update an app: change the tag in
compose.yaml, thendocker compose pull && docker compose up -d. - Clean up:
docker system pruneremoves stopped containers, unused networks, and dangling images. Add-acarefully; it removes all unused images. Do not add--volumesunless you are sure. - Watch disk and logs:
docker system dfshows space use. Configure log rotation (thejson-filedriver supportsmax-sizeandmax-fileoptions) so container logs cannot fill the disk. - Keep the host patched: containers share the host kernel, so
apt upgradeon the host still matters.
Final thoughts
Running Docker on a VPS gives you a tidy, repeatable way to host multiple apps on one machine. Install Docker Engine from the official repository, define everything in Compose, pin your image versions, keep databases off the public internet, and back up your volumes. Get those habits right early and adding the next app becomes a ten-minute job.