Skip to content
TutorialsContainersBeginner

How to Install Docker on Ubuntu

Install Docker Engine and the Compose plugin on Ubuntu Server from Docker's official repository, verify the install, run your first container and fix the common errors.

by HomeServersGuide TeamUpdated October 27, 20264 min read

This tutorial installs the official Docker Engine and Compose plugin on Ubuntu, then verifies everything with a test container. It takes about ten minutes. We use Docker's own repository rather than the docker.io package in Ubuntu's archive, because the official repository gets current releases and the Compose plugin.

If you are new to containers, read Docker basics for self-hosting first, or follow it afterwards.

Prerequisites

  • Ubuntu Server 22.04 LTS or newer (24.04 LTS is a good choice), 64-bit.
  • A user with sudo access.
  • An internet connection, and the system time set correctly.
  • Optional: a fresh server you reach over SSH. See the beginner's guide for setting one up.

Step 1: Remove old or conflicting versions

If a distribution-packaged version is installed, remove it. It's fine if apt says the packages are not installed.

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

Your existing images and containers in /var/lib/docker are not deleted by this.

Step 2: Add Docker's official repository

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
 
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
  https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
 
sudo apt update

The GPG key lets apt verify that packages really come from Docker. If apt update shows an error about the repository, check that you typed the URL exactly and that your Ubuntu release is supported.

Step 3: Install Docker Engine and Compose

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

Docker starts automatically and is enabled at boot. Confirm with:

sudo systemctl status docker --no-pager

Step 4: Verify the installation

sudo docker run --rm hello-world
docker compose version

If you see "Hello from Docker!", the engine works. docker compose version should print a version of Compose v2. Note the command is docker compose (with a space), not the old docker-compose.

Step 5: Run Docker without sudo (understand the risk)

sudo usermod -aG docker $USER
newgrp docker

Log out and back in for the group change to fully apply. Be aware: members of the docker group can start containers that mount the host's filesystem, which is effectively root access. That is acceptable on a personal server you control, but don't add accounts you don't fully trust. See the security checklist.

Step 6: Your first real container

mkdir -p ~/whoami && cd ~/whoami
cat > compose.yaml <<'YAML'
services:
  whoami:
    image: traefik/whoami
    ports:
      - "8088:80"
    restart: unless-stopped
YAML
docker compose up -d

Visit http://your-server-ip:8088 to see it respond with details about the request. Stop and remove it later with docker compose down.

Step 7: Configure log rotation

By default Docker keeps container logs forever, which can slowly fill your disk. Limit them by creating /etc/docker/daemon.json:

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

Then restart Docker with sudo systemctl restart docker. New containers use the limit; recreate existing containers to apply it.

Important: Docker and the firewall

Docker modifies the firewall rules itself. Ports you publish with -p or ports: can be reachable from outside even if ufw says they are blocked, because Docker inserts its own rules ahead of ufw's. Protect services by binding them to a specific address, for example 127.0.0.1:8088:80 for local-only access, or by using a reverse proxy and a firewall at your router. Our networking guide covers this.

Troubleshooting

  • permission denied when talking to the Docker socket: you are not in the docker group yet, or have not started a new session. Log out and back in.
  • Cannot connect to the Docker daemon: check sudo systemctl status docker and start it with sudo systemctl start docker.
  • Port already in use: another program uses that host port. Change the left side of the mapping (for example 8089:80).
  • Repository or key errors: re-run Step 2 and make sure /etc/apt/keyrings/docker.asc exists and is readable.
  • Containers can't resolve DNS: check your host's DNS configuration; you can set "dns" in daemon.json.

Keeping Docker up to date

Docker is updated through apt like any other package. Run sudo apt update && sudo apt upgrade regularly, or enable automatic security updates. Updating the engine restarts containers unless you enable live-restore, so do it at a quiet time.

Next steps

You now have a container platform ready for dozens of self-hosted apps.

Related articles

ArticlesContainersBeginner

Docker Basics for Self-Hosting

Learn images, containers, volumes, networks and Docker Compose, plus the commands, update workflow and mistakes to avoid, so you can deploy self-hosted apps with confidence.

5 min read
TutorialsContainersBeginner

How to Install Portainer

Install Portainer CE on Docker for a web UI to manage containers, stacks, volumes and networks: setup, first stack, security hardening, remote agents and troubleshooting.

5 min read
GuidesContainersIntermediate

Docker Compose Best Practices for Home Servers

Structure Compose stacks, volumes, networks, secrets, healthchecks, logging and updates so your home server stays clean, secure and easy to rebuild.

5 min read