Docker on a VPS runs your apps in isolated containers, each with its own libraries and runtime version, so they don't step on each other on the same server. Installing Docker on Ubuntu 24.04 or Debian 12 takes a few minutes if you use the official repository instead of the older distro packages. This guide walks through the install, your first container, a docker compose file with a restart policy and volumes, logs, image updates and disk cleanup.

Three terms will come up constantly. An image is a template with files and a runtime environment, like nginx:stable. A container is a running instance of an image. A volume is storage for data that lives independently of the container and survives when the container is recreated.

What you need

  • A VPS running Ubuntu 24.04 or Debian 12 with SSH access. If you haven't logged in yet, see how to connect to a VPS over SSH.
  • A user with sudo privileges.
  • At least 2 GB of RAM and 20 GB of disk for a few containers. A single small service needs less, but images do take up space.

Step 1. Install from the official repository

The docker.io package in the stock Ubuntu or Debian repos lags behind what Docker itself ships. The official repo gives you timely security updates and the docker compose plugin. The commands below are for Ubuntu; on Debian, replace ubuntu with debian in the URLs. Docker keeps the current instructions on docs.docker.com, so double-check against that page when you install.

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

Verify the install. hello-world pulls a tiny test image, runs it and prints a greeting. If you see Hello from Docker!, the daemon is working.

sudo systemctl status docker --no-pager
sudo docker run --rm hello-world
docker compose version

Running Docker without sudo: the docker group

To skip typing sudo before every command, add your user to the docker group. Then log out of SSH and back in, or the group change won't take effect.

sudo usermod -aG docker $USER

After logging back in, run groups to check: docker should be in the list.

Step 2. Your first container

Run an Nginx web server in a container. -d sends it to the background, --name gives it a readable name, -p 8080:80 maps the container's port 80 to port 8080 on the server, and --restart unless-stopped brings it back after a reboot unless you stopped it yourself.

docker run -d --name web -p 8080:80 --restart unless-stopped nginx:stable
docker ps
curl -I http://localhost:8080

Heads up: Docker writes its own iptables rules for published ports, and those rules bypass ufw. A port published with -p can be reachable from the internet even if ufw never allowed it. Only publish ports that actually need to be public, and bind internal services to localhost, for example -p 127.0.0.1:5432:5432.

Step 3. Docker Compose: services, volumes and restart policy

One-off docker run commands are fine for testing, but a long-running service belongs in a docker-compose.yaml file. The whole config lives in one place, and you can change and recreate everything with a single command. Create a project directory and a file where an app and a database run together.

mkdir -p ~/app/site && cd ~/app
echo 'My app running in Docker' > site/index.html
nano docker-compose.yaml
services:
  web:
    image: nginx:stable
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./site:/usr/share/nginx/html:ro
    depends_on:
      - db

  db:
    image: postgres:17
    restart: unless-stopped
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: app
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Here's what the key lines do. restart: unless-stopped sets the restart policy: the container comes back after a crash and after a server reboot. ./site:/usr/share/nginx/html:ro mounts a host directory into the container read-only. The pgdata volume keeps database files separate from the container, so recreating the container doesn't wipe your data. The password isn't in the file: Compose reads POSTGRES_PASSWORD from a .env file in the same directory.

echo "POSTGRES_PASSWORD=$(openssl rand -base64 24)" > .env
chmod 600 .env
docker compose up -d
docker compose ps

Don't commit .env to Git. If the directory is a repo, add it to .gitignore.

Compose picks up .env automatically when it sits next to docker-compose.yaml and substitutes its values into ${NAME} placeholders. That keeps passwords and ports out of the config, so the same compose file works on different servers with different values. docker compose down stops and removes the project's containers but keeps volumes unless you add -v. To restart a single service, use docker compose restart web, which is faster than recreating everything.

Logs and image updates

You can read container logs without shelling into the container. docker compose logs merges output from every service in the project, and -f follows it live.

docker compose logs -f web
docker compose logs --tail 100 db
docker logs --tail 50 web

To update to newer images, pull them and recreate the containers. Data in volumes stays put.

docker compose pull
docker compose up -d
docker image prune -f

Pin image tags like stable or 17 rather than latest. One day latest will jump to a version with breaking changes, and your service will break without you touching anything.

By default, Docker writes container logs to json-file logs with no size limit. On a long-running server they can fill the disk, so set up rotation in /etc/docker/daemon.json and restart the daemon. The setting applies to containers created after the change.

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
sudo systemctl restart docker

Cleaning up disk space

Old images, stopped containers and build cache pile up over time. First see what's actually eating the disk, then remove what you don't need.

docker system df
docker image prune -f
docker builder prune -f
docker system prune -f

docker system df shows how much space images, containers, volumes and build cache use. If volumes take most of it, figure out which service owns them before deleting anything. Build cache is safe to clear, and images can always be pulled again.

Wrapping up

Docker on a VPS gives you a reproducible environment: install from the official repo, run containers with a restart policy, describe services in docker compose with volumes and a separate .env file, and keep up with image updates and disk cleanup. Watch out for published ports and the docker group: those are the two places where a mistake costs the most. If your service will serve visitors on a domain, add HTTPS with how to host a website on a VPS with Nginx and SSL, and to size your server, check the VPS plans.