# How to install Docker and Docker Compose on Ubuntu and Debian

> Install Docker and Docker Compose from the official repository, publish ports safely behind the firewall and run your first Compose application. The foundation for running your own applications on a Hukot.net VPS.

Source: https://www.hukot.net/community/en/tutorials/how-to-install-docker-and-docker-compose-on-ubuntu-and-debian
Updated: 2026-11-25

Docker runs applications in containers that carry everything they need with them. Docker Compose describes a whole application (for example a website, a database and a cache) in one file and starts it with one command. Most tutorials on running your own applications build on it, for example n8n, Nextcloud or Uptime Kuma.

For maximum stability, we recommend installing it on a [Hukot.net VPS](https://www.hukot.net/en/linux-vps/): your server is ready within 60 seconds of ordering and your data stays in a data centre in the Czech Republic.

## Prerequisites

- A VPS with 64-bit Ubuntu 22.04 / 24.04 / 26.04 LTS or Debian 12 / 13.
- SSH access as a user with `sudo` rights. You can create the user by following [Adding and removing users in Linux](tutorial:92) and set up key-based login with [SSH with public-key authentication](tutorial:26).
- Docker itself runs on the smallest tariff; memory and disk are used by the applications. When they need more, you can [switch to a higher tariff](tutorial:272) right in the administration and pay only the difference.

> [!NOTE]
> Install Docker from Docker's official repository, not the `docker.io` package from your distribution. The repository always has current versions, including the Docker Compose plugin.

## Installation

### Removing old packages

If the server already has Docker from the distribution or Podman, remove them so they don't conflict with the official packages:

::: tabs
+++ Ubuntu
```bash
sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc | cut -f1)
```
+++ Debian
```bash
sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-doc docker-buildx podman-docker containerd runc | cut -f1)
```
:::

On a clean server the command reports that none of the packages are installed. That's fine.

### Docker repository

Add Docker's signing key and repository:

::: tabs
+++ Ubuntu
```bash
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

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
```
+++ Debian
```bash
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/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

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

sudo apt update
```
:::

### Docker Engine and Docker Compose

Install Docker Engine, the Docker Compose plugin and the other tools:

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

The `docker` service starts right after the installation and on every boot.

> [!WARNING]
> The `get.docker.com` script installs Docker with one command, but Docker doesn't recommend it for production servers: a later run can unexpectedly upgrade to a new major version.

## Verification

1. Check that the service is running. The output should say `active (running)`:

   ```bash
   sudo systemctl status docker
   ```

2. Run a test container. It downloads a small image, prints `Hello from Docker!` and exits:

   ```bash
   sudo docker run hello-world
   ```

3. Check the Docker Compose version:

   ```bash
   docker compose version
   ```

Docker Compose is run as `docker compose`, with a space. The old standalone `docker-compose` with a hyphen is no longer developed and you don't need it.

## Docker without sudo

To let your user run Docker without `sudo`, add it to the `docker` group. The change applies from your next login:

```bash
sudo usermod -aG docker $USER
```

After logging in again, check that the command works without `sudo`:

```bash
docker run hello-world
```

> [!DANGER]
> Membership in the `docker` group equals root access: through a container, any file on the server can be read and changed. Add only users you would also trust with `sudo`, never the user an application or AI agent runs as.

## Docker and the firewall

A port published by a container (`ports` in Compose or `-p` with `docker run`) is opened by Docker directly in the kernel rules. Traffic to the container passes before the UFW rules apply, so **the firewall doesn't block it**. A port published as `8080:80` is reachable from the whole internet even if you never allowed it in UFW.

That's why you should publish application ports only on `127.0.0.1`:

```yaml
ports:
  - "127.0.0.1:8080:80"
```

The application is then reachable only from the server itself. Expose it through Nginx as a reverse proxy with an HTTPS certificate by following [Nginx as a reverse proxy](tutorial:291), or connect to it through an SSH tunnel. Keep only SSH, HTTP and HTTPS open on the firewall; see [Setting up a firewall with UFW](tutorial:96).

## Your first Docker Compose application

Try Compose with the Nginx web server.

1. Create a directory for the application and a `compose.yaml` file in it:

   ```bash
   mkdir -p ~/hello-compose
   cd ~/hello-compose
   nano compose.yaml
   ```

2. Paste into the file:

   ```yaml
   services:
     web:
       image: nginx:alpine
       ports:
         - "127.0.0.1:8080:80"
       restart: unless-stopped
   ```

   `restart: unless-stopped` means the container starts again after a crash or a server reboot, until you stop it yourself.

3. Start the application in the background and look at its status:

   ```bash
   docker compose up -d
   docker compose ps
   ```

4. Check that Nginx responds and the port listens only locally. The `ss` output should show `127.0.0.1:8080`:

   ```bash
   curl -I http://127.0.0.1:8080
   sudo ss -tlnp | grep 8080
   ```

5. Stop and remove the test application:

   ```bash
   docker compose down
   ```

Follow the application's logs with `docker compose logs -f`; update it to newer images with `docker compose pull` followed by `docker compose up -d`.

## Limiting log size

By default, Docker keeps container logs without any limit, and on a smaller disk they can fill it up. Set up log rotation in `/etc/docker/daemon.json`:

```bash
sudo nano /etc/docker/daemon.json
```

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

Each container's logs then take at most 30 MB. Restart Docker:

```bash
sudo systemctl restart docker
```

> [!NOTE]
> The setting applies only to newly created containers. Recreate running Compose applications with `docker compose up -d --force-recreate`.

## Updates

Docker from the official repository is updated together with the system:

```bash
sudo apt update
sudo apt upgrade
```

Docker restarts during the update. Containers with `restart: unless-stopped` start again on their own.

> [!TIP]
> Before a bigger update, create a snapshot in the Hukot.net VPS administration. If anything breaks after the update, you can roll the server back to its previous state.

To clean up containers and images you no longer need, see [How to stop all Docker containers](tutorial:206) and [How to remove all Docker containers and images](tutorial:190).

## Why choose our VPS?

Unlike public clouds, we guarantee that your data stays in the Czech Republic, with top-class connectivity. Docker on a Hukot.net VPS is the foundation for running your own applications instead of paid cloud services, with your data under your control.

::: cta
## Want to run your own applications?

Order a VPS from Hukot.net and start your first container on it today.

[Choose a VPS configuration](https://www.hukot.net/en/linux-vps/)
:::
