Linux & software2 min read

Install Docker and Docker Compose and start your first containers

Install Docker from the official repository, use it without sudo, start applications with Docker Compose and learn the most important everyday commands.

Docker packs applications together with everything they need into containers. Instead of setting up software by hand, you start it with a short file and remove it without leftovers. This guide installs Docker from Docker's official repository, because the distribution packages are often outdated.

Remove old packages

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

You can ignore messages that a package is not installed.

Set up the repository

For Debian 12:

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/debian/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/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

For Ubuntu, replace debian with ubuntu in both addresses.

Install Docker

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo docker run hello-world

If "Hello from Docker!" appears, everything works.

Docker without sudo

sudo usermod -aG docker $USER

Log out and back in once afterwards.

Members of the docker group effectively have root rights on the server. Only add users you fully trust.

Your first application with Docker Compose

With Compose you describe containers in a file. As an example, a simple web server:

mkdir ~/hello && cd ~/hello
nano compose.yaml
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    restart: unless-stopped
mkdir html && echo '<h1>Hello from Docker</h1>' > html/index.html
docker compose up -d

The page appears at http://YOUR-IP:8080. restart: unless-stopped makes sure the container runs again after a server reboot.

Important commands

Command Effect
docker compose up -d Start or apply changes
docker compose down Stop and remove containers
docker compose logs -f Follow the log live
docker compose pull && docker compose up -d Update to new versions
docker ps running containers
docker exec -it NAME sh Open a shell in the container
docker system prune Clean up unused images and containers

Docker and the firewall

Docker adds its own firewall rules and bypasses UFW in the process. A port published with ports: "8080:80" is reachable from outside even if UFW blocks it. If a service should only be reachable through a reverse proxy, bind it to localhost:

ports:
  - "127.0.0.1:8080:80"

How to publish it with a domain and HTTPS is shown in Nginx as a reverse proxy and Traefik with Docker.

Back up your data

Data inside containers is lost when they are deleted. Anything important belongs in volumes or folders like ./html above. Back up these folders regularly, for example with restic.

Written by TryNxt team

We build and run TryNxt ourselves and write down here what we learn along the way.