Sub topic 6
Installing Docker Engine on Ubuntu with apt
This chapter walks through installing Docker Engine on Ubuntu from Docker's own apt repository. It assumes you can use a terminal, have sudo rights, and know what an image and a container are. You should also have checked the system requirements from the earlier chapter. Docker supports current Ubuntu LTS releases on 64-bit hardware.
Why use Docker's repository
Ubuntu ships its own docker.io package. It is maintained by the distribution, so it often lags behind Docker's releases and may not include the Compose and Buildx plugins. Docker's repository gives you the official docker-ce packages and updates through the normal apt workflow.
If an older Docker is already installed, remove it first. Check for docker.io, docker-compose, podman-docker, and containerd. Two installs of the same components conflict with each other.
Step 1: Add the repository
The first command block refreshes the package index and installs the tools needed to download the key. It then stores Docker's GPG key and writes the repository definition.
sudo apt-get update
sudo apt-get install 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-get updateHere is what the pieces do:
- The GPG key lets
aptverify that packages were signed by Docker. signed-by=ties that key to this repository only, instead of trusting it system-wide.dpkg --print-architecturefills in your CPU architecture, such asamd64orarm64.UBUNTU_CODENAMEorVERSION_CODENAMEfills in your release name, such asnoble. Derivatives like Linux Mint setUBUNTU_CODENAMEto the Ubuntu base.
The final apt-get update should list download.docker.com among the sources it reads. If it does not, the repository file is wrong.
Step 2: Install the packages
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginEach package has a distinct role:
| Package | Contents |
|---|---|
| `docker-ce` | The Docker Engine daemon, `dockerd` |
| `docker-ce-cli` | The `docker` command |
| `containerd.io` | The container runtime that the daemon calls |
| `docker-buildx-plugin` | The `docker buildx` command |
| `docker-compose-plugin` | The `docker compose` command |
On Ubuntu, the installer normally starts the service for you.
Step 3: Run without sudo and start at boot
Right now only root can reach the daemon's socket. These commands add you to the docker group, activate that group in the current shell, and enable the service.
sudo usermod -aG docker $USER
newgrp docker
sudo systemctl enable --now docker.serviceUse -aG, with the a. Without it, usermod replaces your supplementary groups instead of adding one. newgrp only affects the current shell, so log out and back in for other sessions to pick up the change. The enable --now option both starts the service immediately and registers it to start at boot.
Step 4: Verify
docker version
docker compose version
docker run hello-worlddocker version prints both a Client and a Server section. If only the Client appears, the CLI is installed but cannot reach the daemon. docker run hello-world pulls a small image and prints a confirmation message, which proves the whole chain works: CLI, daemon, registry access, and runtime.
Common errors at this stage
permission denied while trying to connect to the Docker daemon socket: your shell does not have thedockergroup yet. Runnewgrp dockeror log in again, and check withid.Cannot connect to the Docker daemon: the service is not running. Checksystemctl status dockerandjournalctl -u docker.Unable to locate package docker-ce: the repository was not added, or the codename is not supported. Re-read/etc/apt/sources.list.d/docker.list.- Key or signature errors: confirm
/etc/apt/keyrings/docker.ascexists and is readable by all, as thechmod a+rstep sets.
Exercise
After installing, docker version shows a Client section but the Server section reports a permission error on the socket. The service is running. What is the most likely cause and the fix?
Show answer
Your user is not yet in an active docker group session. Run sudo usermod -aG docker $USER, then newgrp docker or log out and back in, and confirm the group with id.
Exercise
Why does the repository line use signed-by=/etc/apt/keyrings/docker.asc instead of adding the key to the system-wide trusted keys?
Show answer
It limits trust in that key to Docker's repository. Packages from other sources cannot be validated with it.