Install Docker Engine and the Compose Plugin on Debian or Ubuntu
Goal
Docker Engine and Compose v2 installed from Docker's official apt repository on a Debian or Ubuntu server, as a script that can be re-run.
Prerequisites
- A Debian or Ubuntu server with
sudo.
Steps
- Save this as
install-docker.sh. It reads the distribution and codename from/etc/os-release, so the same script works on both:#!/usr/bin/env bash set -euo pipefail . /etc/os-release # ID=debian|ubuntu, VERSION_CODENAME 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/${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 sudo apt-get update sudo apt-get install -y docker-ce docker-ce-cli containerd.io \ docker-buildx-plugin docker-compose-plugin sudo systemctl enable --now docker sudo docker run --rm hello-world docker compose version - Copy it to the server and run it:
scp install-docker.sh user@server:~ ssh -t user@server "chmod +x ~/install-docker.sh && ~/install-docker.sh" - Optionally let your user run Docker without
sudo, then open a new session (ornewgrp docker) for the group to apply:sudo usermod -aG docker "$USER"
Verify
-
docker run --rm hello-worldprints its welcome message. -
docker compose versionprints a v2 version. - After logging in again,
docker psworks withoutsudo(if step 3 was done).
Troubleshooting
permission denied ... /var/run/docker.sock: the group change hasn't reached your session; log out and back in, or runnewgrp docker.docker-compose: command not found: expected: usedocker compose(space, not hyphen).
References
- Docker documentation: Install Docker Engine on Ubuntu - the official repository setup.
- Docker documentation: Install Docker Engine on Debian - the Debian variant.
- Docker documentation: Linux post-installation steps - the
dockergroup and its security implications. - Specify the versions of the softwares you install on your server - what the old
docker-composepackage broke. - Docker and Container Security MOC - hub for Docker notes.