Skip to content

Guide · 8 min read

Install Docker on your VPS

Install Docker and Docker Compose on Ubuntu or Debian, run your first app, cap log sizes and avoid the Docker firewall trap.

Docker runs each app in its own container, so you can start a database, a web app or a bot with one command and remove it again without leaving a mess on your server. Our VPS use KVM with their own kernel, so Docker works without special settings. The commands are for Ubuntu and Debian and use sudo; if you haven't created a sudo user yet, start with First steps on a new Linux VPS.

On a brand-new VPS the system installs updates during the first few minutes. If apt says Could not get lock, wait 2–3 minutes and try again.

1. Remove old Docker packages (only if you had them)

On a new VPS, skip this step. If Docker was installed earlier from Ubuntu's or Debian's own packages, remove those first so they don't conflict:

for pkg in docker.io docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt remove -y $pkg; done

Messages that a package is not installed or can't be found are fine.

2. Add Docker's official repository

The Docker packages in Ubuntu and Debian are often old. Docker's own repository has the current version and Docker Compose. These lines work on both systems, because $ID is ubuntu or debian:

sudo apt update
sudo apt install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
. /etc/os-release
sudo curl -fsSL https://download.docker.com/linux/$ID/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/$ID $VERSION_CODENAME stable" | sudo tee /etc/apt/sources.list.d/docker.list

3. Install Docker and Docker Compose

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Docker starts automatically, now and after every reboot. Test it:

sudo docker run --rm hello-world
docker compose version

You should see Hello from Docker! and a Compose version number.

4. Run Docker without sudo (optional)

sudo usermod -aG docker $USER

Log out and back in for this to take effect. Members of the docker group can control the whole server through Docker, so treat it like root access and only add users you trust.

5. Limit the size of container logs

By default container logs grow without any size limit, and a chatty app can fill a small disk within weeks. The local log driver rotates logs automatically and keeps at most about 100 MB per container:

sudo tee /etc/docker/daemon.json <<'EOF'
{
  "log-driver": "local"
}
EOF
sudo systemctl restart docker

Do this right after installing. The setting applies to containers created afterwards; existing ones keep their old log settings until you recreate them.

6. Start your first app with Docker Compose

Compose describes your containers in a file, so you can start, update and stop them with one command. This example runs the Nginx web server on port 8080. (If you skipped step 4, put sudo in front of the docker commands.)

mkdir ~/hello && cd ~/hello
cat > compose.yaml <<'EOF'
services:
  web:
    image: nginx:stable
    ports:
      - "8080:80"
    restart: unless-stopped
EOF
docker compose up -d

Open http://your-server-ip:8080 in your browser and you'll see the Nginx welcome page. restart: unless-stopped starts the container again after a crash or reboot. Useful commands, run in the same folder:

docker compose ps        # what is running
docker compose logs -f   # follow the logs
docker compose pull && docker compose up -d   # update to newer images
docker compose down      # stop and remove the containers

7. Important: published ports bypass UFW

Notice that port 8080 opened in your browser, although UFW never allowed it. Docker writes its own firewall rules, so every port you publish with ports: is reachable from the whole internet, whatever UFW says.

For databases, admin panels and anything else only your own apps or you should reach, publish the port on localhost only:

    ports:
      - "127.0.0.1:5432:5432"

Containers in the same Compose file can reach each other by service name (for example db:5432) without any published port at all. To reach a localhost-only port from your own computer, use an SSH tunnel: ssh -L 5432:127.0.0.1:5432 you@your-server-ip.

8. Keep an eye on disk space

Old images, stopped containers and build cache pile up over time:

docker system df      # what uses space
docker system prune   # remove stopped containers, unused networks and dangling images

Data you want to keep belongs in a volume or a mounted folder, not inside the container. Servitro does not back up your server, so copy important volumes somewhere else regularly.

Next steps

Last updated 2026-11-08. Need help? Open a ticket – our team answers 24/7.