Linux Tutorials

How to Install Docker and Docker Compose on a VPS (Official Method)

By NileVPS Team · Published · 6 min read

اقرأ المقال بالعربي

Docker has become the most popular way to run applications on servers: you package an application with everything it needs into a single container, and it behaves the same on your laptop as it does on the server. On a VPS where you have full root access, you can run databases, web apps and monitoring tools side by side without version conflicts. In this guide we install Docker Engine and the Compose plugin from the official repository on Ubuntu 24.04 or Debian 12, run a hands-on example, cover logs and updates, and walk through the security pitfalls many people miss.

Why the Official Repository Instead of the docker.io Package?

Distribution repositories include a package called docker.io, but it is usually older than the current release and may not ship the modern docker compose plugin at a matching version. Docker's official repository provides the latest stable release along with Buildx and Compose v2, and it receives regular updates through apt like any other package.

Requirements

  • A 64-bit VPS running Ubuntu 24.04 or Debian 12. 1 GB of RAM is enough for experimenting; for real workloads, 2 GB or more is recommended. Browse our VPS plans to pick the right size.
  • A user with sudo privileges and an SSH connection. If you need help, see connecting to a Linux VPS over SSH.

Installation Steps

1. Remove Conflicting Packages

If the server has an old or unofficial version installed, remove it first. It is fine if you see messages saying a package is not installed:

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

2. Add Docker's Key and Repository

The following commands work on both Ubuntu and Debian, because they read the distribution name and release from /etc/os-release:

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 > /dev/null

On Ubuntu 24.04 the values will be ubuntu and noble; on Debian 12 they will be debian and bookworm.

3. Install Docker Engine and Compose

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

Make sure the service is running and starts automatically at boot:

sudo systemctl enable --now docker
docker --version
docker compose version

Note that the modern command is docker compose with a space, a built-in plugin, not the old hyphenated docker-compose.

4. Run Docker Without sudo (Optional)

sudo usermod -aG docker $USER
newgrp docker

After this you can run Docker commands directly. But be careful: membership in the docker group is effectively equivalent to root access, because anyone who controls Docker can start a container that reads any file on the system. Only add users you trust completely.

5. Test the Installation

docker run --rm hello-world

If you see the welcome message, everything works: Docker pulled the image from Docker Hub, ran the container, and then removed it thanks to the --rm flag.

Hands-On Example: an nginx Site with Docker Compose

Compose lets you describe your services in a single file instead of typing long commands. Create a project folder:

mkdir -p ~/mysite/html
cd ~/mysite
echo '<h1>Hello from Docker</h1>' > html/index.html

Then create the compose.yaml file:

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

Start the service in the background:

docker compose up -d
docker compose ps

Open the server's IP address in your browser and the page will appear. A quick breakdown of the file:

  • restart: unless-stopped: the container comes back automatically after a server reboot, unless you stopped it manually.
  • ports: maps port 80 on the server to port 80 inside the container.
  • volumes with ro: mounts the content folder read-only, so the container cannot modify your files.

To stop the service and remove its containers:

docker compose down

Logs

To view a service's logs and follow them live:

docker compose logs -f web
docker logs --tail 100 <container-name>

A common problem: container logs are stored with no size limit by default and can fill the disk after a few weeks. Cap their size in /etc/docker/daemon.json:

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

Then restart Docker:

sudo systemctl restart docker

This setting applies only to containers created after the change, so recreate existing ones with docker compose up -d --force-recreate.

Updates

There are two kinds of updates to keep in mind:

What you updateCommandNote
Docker Engine itselfsudo apt update && sudo apt upgradeRestarts containers that have a restart policy
Application imagesdocker compose pull && docker compose up -dRecreates only containers whose image changed
Cleaning up old imagesdocker image pruneFrees disk space

Before updating important applications, take a snapshot of the server from the NileVPS control panel; if something goes wrong, you can roll back within minutes. In production, it is also better to pin a specific image version such as nginx:1.26-alpine instead of latest, so a major change never catches you by surprise.

Important Security Notes

Never Expose the Docker Socket

The /var/run/docker.sock file is the gateway to full control over Docker. Mounting it inside a container (-v /var/run/docker.sock:/var/run/docker.sock) gives that container root-level access to the server. Only do this for trusted tools that genuinely need it, and never enable the Docker API over unencrypted TCP on port 2375; it is one of the most common ways servers get compromised.

Docker Bypasses ufw

This one surprises a lot of people: when you publish a port with ports: "8080:80", Docker adds its own iptables rules that are processed before ufw's rules, so the port becomes open to the world even if ufw blocks it. Practical solutions:

  • Bind internal ports to localhost only if they will be reached through a reverse proxy on the same server:
    ports:
      - "127.0.0.1:8080:80"
  • Don't publish database ports at all. Containers in the same Compose file talk to each other over the internal network using the service name, such as db:3306, with no published port.
  • Use the cloud firewall in the NileVPS panel: because it runs outside the server, the iptables rules Docker adds don't affect it, so it remains a reliable line of defense.
  • For advanced users: add your own rules to the DOCKER-USER chain, which Docker respects.

Additional Tips

  • Use only official or trusted images.
  • Run applications inside containers as a non-root user whenever the image supports it.
  • Don't put passwords in a compose.yaml that you push to Git; keep them in a .env file excluded from the repository.

FAQ

What is the difference between docker-compose and docker compose?

docker-compose is the original version written in Python, and it is no longer developed. docker compose is Compose v2, an official plugin for the Docker CLI installed through the docker-compose-plugin package. Your existing files are usually compatible; just switch to the new command.

Can I run Docker on a VPS with 1 GB of RAM?

Yes, for lightweight workloads like nginx or a small app. Running a database, an application and a monitoring tool together, however, calls for 2 to 4 GB. Keep an eye on usage with docker stats to know when it is time to upgrade.

Why is my container's port reachable even though I blocked it in ufw?

Because Docker manages iptables rules directly and bypasses ufw for published ports. Bind the port to 127.0.0.1, don't publish it at all, or rely on the cloud firewall outside the server.

Do I need a Windows VPS to run Docker?

No. Docker runs best on Linux, and most available images are built for Linux containers. A Linux server running Ubuntu or Debian is the natural choice, and you can deploy a server in just a few minutes.

Related guides