HomeBlog
How to Install Docker on an Ubuntu VPS

How to Install Docker on an Ubuntu VPS

Your application runs on your laptop, but the server lacks the libraries it needs. Docker helps you describe a container environment and repeat the launch without configuring every component by hand.

We will show how to install Docker on Ubuntu 24.04 LTS, add Docker Compose and check that data is preserved. This guide is for a new VPS with sudo and SSH; the examples need testing on your own configuration.

What to prepare for Docker on a VPS

Check the OS with cat /etc/os-release and the architecture with dpkg --print-architecture. The official Docker documentation lists the supported releases and architectures. Do not use another distribution's repository just because the name looks similar.

The application determines the resources: Docker itself sets no universal minimum for a website, database or queue. For a learning container, leave room for images, logs and volumes. Check df -h before installing.

If the system already has docker.io, the old docker-compose or a separate containerd, check for possible conflicts first. Do not remove packages from a production server without a plan for moving containers and a copy of the data.

“A repeatable result starts with a clear description.”

The example below uses docker-ce from the official APT repository. The commands run on the server; you do not need to copy the $ sign from documentation examples.

Add the official Docker repository

Update the package list and prepare the keyring directory:

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

This file lets APT verify the signature of the repository's packages. It is downloaded from Docker's official domain, not from a random mirror. Next, create the source file.

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

Review the apt update messages. If the repository does not support your release or the key cannot be read, fix this before installing. Do not disable signature checking to hide an error.

Install the Engine and the Compose plugin

Install the packages and check the service:

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl status docker --no-pager
sudo docker version
sudo docker compose version
sudo docker run --rm hello-world

hello-world should download a test image, print a confirmation and exit. This checks the engine, not whether your future application is ready. If the daemon is not running, review journalctl -u docker.

Docker Compose runs as docker compose, with a space. This guide does not need the separate legacy docker-compose command. Write down the versions of the client, daemon and plugin so you can reproduce the result.

We will work through sudo. In the standard configuration, access to the Docker daemon grants broad rights over the host, so adding a person to the docker group is an access decision, not just a convenience.

Run a simple application with Compose

Create a working directory with mkdir -p ~/fotbo-demo and go to it with cd ~/fotbo-demo. Save the configuration in the compose.yaml file:

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

volumes:
  site_data:

The image tag in this learning example may change over time. For a repeatable production deployment, record the tested version or the image digest. Do not assume a new tag is automatically compatible with your application.

The port is bound to 127.0.0.1, so the page is available on the VPS itself but not publicly on all interfaces. This is convenient for an initial check before setting up a domain and HTTPS.

sudo docker compose config
sudo docker compose up -d
sudo docker compose ps
curl http://127.0.0.1:8080/

The first command checks the configuration structure, and the last one should return HTML. To open the page in your own browser, create an SSH tunnel from your computer: ssh -L 8080:127.0.0.1:8080 admin@YOUR_IP, then open localhost:8080.

Check that data persists in the volume

The named volume site_data stores content separately from the container's file system. If the image contains files at the mount point, Docker by default copies them into a new empty volume.

Change the test page and recreate the container:

sudo docker compose exec web sh -c 'printf "Fotbo demo\n" > /usr/share/nginx/html/index.html'
sudo docker compose up -d --force-recreate
curl http://127.0.0.1:8080/

The expected text is Fotbo demo. If it remains after the container is recreated, the data separation check has passed. This is not a backup: losing the VPS disk can destroy the volume along with other files.

Important data needs a separate backup strategy. This is especially true for databases: simply copying the directory of a running database does not always produce a consistent state. Check the method against your DBMS documentation.

Control access and open ports

Publishing a Docker port can bypass the usual UFW rules. Do not assume that ufw deny will automatically close any container port: Docker manages its own network rules.

In our example, keep the binding to loopback. For a public website, set up a reverse proxy, a domain, TLS and an external check. Do not publish a database port just because it appears in a Compose example.

“Convenience should leave room for control.”

Do not mount docker.sock into random containers or run them as privileged without a justified need. Check the image source, permissions and secrets. Do not store passwords in a public repository.

Useful diagnostic commands are docker compose logs --tail=50 and docker system df. The first shows logs, the second shows disk usage. Do not wipe all volumes as a standard way to free up space.

Update the application while keeping your data

Before updating, save the configuration and a copy of the data, and read the image changes. For the chosen compatible version, run sudo docker compose pull, then sudo docker compose up -d and repeat the page check.

The docker compose down command removes the project's containers and network but keeps named volumes by default. The down -v option deletes the project's volumes: do not use it for data you want to keep.

After a successful test, write down the launch commands and the recovery procedure. For your next project, choose a VPS for Docker and start with a tested configuration.

Now you know how to install Docker on Ubuntu, run a container and check your data after replacing it. To make Docker useful in your project, apply this sequence of checks to your own application.

How to Install Docker on an Ubuntu VPS
vds

You might be interested in Fotbo's VPS server

View