Skip to content
HOSTIGAN
Linux

How to install Docker on an Ubuntu VPS

Install Docker Engine and Compose on Ubuntu 24.04 from Docker's official repository, run your first containers, keep data in volumes and stop Docker from bypassing UFW.

Intermediate 5 min read Updated November 24, 2026

Docker packages an application together with everything it needs into a container that runs the same way on any Linux server. Instead of installing PHP, Node.js or a database directly on the host, you start a container and throw it away again when you are done. On a VPS that keeps the operating system clean and makes services easy to move, update and roll back.

This guide installs Docker Engine and the Compose plugin on Ubuntu 24.04 from Docker's official repository, runs a first container, sets up a small Compose stack and deals with the one pitfall that catches almost everybody: Docker and UFW.

Before you start

  • Ubuntu 24.04 or 22.04 LTS, 64-bit, with a sudo user. Check with lsb_release -a.
  • An up-to-date system (sudo apt update && sudo apt upgrade).
  • UFW enabled with SSH allowed. See how to configure a firewall on your Linux VPS.
  • A backup or snapshot if the server already runs something important.

Why not apt install docker.io?

Ubuntu's own docker.io package works, but it follows Ubuntu's release cycle and lags behind Docker's releases. Docker's repository gives you the current Docker Engine plus the Buildx and Compose v2 plugins, and they update with the rest of the system.

Install Docker Engine from the official repository

1. Remove conflicting packages. It is fine if apt says none of them are installed.

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

2. Install the prerequisites and Docker's signing key.

sudo apt-get update
sudo apt-get 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

3. Add the repository, trusted only for that key. The Signed-By line means the key can only vouch for Docker's repository, not for any other package source.

sudo tee /etc/apt/sources.list.d/docker.sources > /dev/null <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

4. Install Docker.

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

5. Check that it runs and starts at boot.

sudo systemctl is-enabled docker
sudo docker run --rm hello-world
docker --version
docker compose version

Note that the command is docker compose with a space. The old docker-compose binary is no longer needed.

Run Docker without sudo, carefully

To use docker without sudo, add your user to the docker group and log out and in again:

sudo usermod -aG docker $USER

Warning: Membership of the docker group is effectively root on the host, because a container can mount the whole file system. Only add trusted administrators. If you need stronger separation, look at Docker's rootless mode in the official documentation.

Your first container

Start Nginx on port 8080, but only on localhost:

docker run -d --name web -p 127.0.0.1:8080:80 --restart unless-stopped nginx:stable-alpine
curl -I http://127.0.0.1:8080
docker ps
docker logs web
docker stop web && docker rm web
FlagMeaning
-dRun in the background
--nameA name you can refer to instead of an ID
-p host:containerPublish a port; prefix with 127.0.0.1: to keep it local
-v source:/pathMount a volume or host directory
-e KEY=valueSet an environment variable
--restartWhat to do when the container stops or the server reboots

Tip: Pin image tags such as nginx:1.27-alpine or mariadb:11.4 instead of latest. You decide when to move to a new version, and a rebuild never surprises you.

Docker and UFW: published ports bypass the firewall

This is the most common Docker security mistake. When you publish a port with -p 8080:80, Docker writes its own NAT rules. The traffic is forwarded to the container before UFW's input rules ever see it, so the port is open to the internet even though ufw status does not list it.

Three ways to handle it, in order of preference:

  1. Bind to localhost (-p 127.0.0.1:8080:80) for anything that should not be public, such as databases and admin panels.
  2. Put a reverse proxy in front. Let Nginx on the host listen on 80 and 443 and forward to containers on 127.0.0.1. See how to install Nginx on Ubuntu.
  3. Filter in the DOCKER-USER chain, which Docker reserves for your own rules and evaluates before its own. Docker's packet filtering documentation explains the details.

Warning: Never expose the Docker API on a TCP port. To manage Docker remotely, use an SSH context instead: docker context create vps --docker "host=ssh://deploy@YOUR_SERVER_IP".

Docker Compose: describe the stack in one file

Compose defines several containers, their networks and volumes in a compose.yaml. Here is a small site with a database:

services:
  web:
    image: nginx:stable-alpine
    ports:
      - "127.0.0.1:8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    restart: unless-stopped

  db:
    image: mariadb:11.4
    environment:
      MARIADB_DATABASE: app
      MARIADB_USER: app
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
    restart: unless-stopped

volumes:
  db_data:

Keep passwords in a .env file next to it, readable only by you (chmod 600 .env). Then:

docker compose up -d
docker compose ps
docker compose logs -f db
docker compose down

docker compose down keeps named volumes. docker compose down -v deletes them, including the database.

Volumes, restarts and logs

Volumes. Named volumes (db_data above) are managed by Docker and suit databases. Bind mounts (./html) map a host folder and suit files you edit yourself. Back up databases with a dump such as docker compose exec db mariadb-dump -u root -p app > app.sql rather than copying raw files from a running container.

Restart policies. unless-stopped is a sensible default: containers come back after a reboot, but stay down if you stopped them on purpose. on-failure suits jobs that should only be retried when they crash.

Log rotation. By default container logs grow without limit. Set a cap in /etc/docker/daemon.json:

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

Restart Docker with sudo systemctl restart docker and recreate containers (docker compose up -d --force-recreate), because the setting only applies to new containers.

Updates and clean-up

Update the images in a Compose stack:

docker compose pull
docker compose up -d
docker image prune

Docker Engine itself updates with apt upgrade. Unattended-upgrades on Ubuntu only covers Ubuntu's own security repository by default, so include Docker in your regular manual updates; see how to update Ubuntu Server safely.

Check how much space Docker uses with docker system df, and clean up with docker system prune. Add -a to remove all unused images. Do not add --volumes unless you know exactly what each volume contains.

Tip: Before you prune on a server that runs important containers, list what would go with docker image ls --filter dangling=true and docker volume ls --filter dangling=true. A few seconds of reading can save a database.

Common errors

ErrorCause and fix
permission denied ... docker.sockUser not in the docker group, or you did not log in again
port is already allocatedAnother container or service uses the port; sudo ss -tulpn
Container exits immediatelyRead docker logs <name>; often a missing variable or a bad command
Disk fullOld images, build cache and unrotated logs; docker system df
Container not reachableCheck the -p binding and that the app listens on 0.0.0.0 inside the container

In short

Install Docker from the official repository with a signed-by keyring, use docker compose for anything with more than one container, keep data in volumes, pin image tags and rotate logs. Above all, remember that published ports bypass UFW: bind to 127.0.0.1 and use a reverse proxy for public traffic.

Docker on a Hostigan VPS

A Hostigan VPS runs on KVM with full root access and NVMe storage, so Docker works without restrictions. Watch your memory as you add containers, and take a snapshot before big changes on VPS Plus and up; monitoring CPU, RAM and disk helps you see when it is time for a larger plan.

Frequently asked questions

No. A VPS is a virtual machine with its own kernel. Docker containers run on top of an operating system and share its kernel, so you typically run many containers inside one VPS.

Next in “Your own web server on Linux” · 6 of 6How to install WordPress on a VPS (Ubuntu 24.04, Nginx)