How to install Docker on Ubuntu 24

To install Docker on Ubuntu the way Docker recommends, add its own apt repository: it carries the current Docker Engine and the Compose plugin. Ubuntu’s docker.io package works too, but it lags behind. This guide is for Ubuntu 24.04 and 22.04 and also covers running docker without sudo and the errors people hit most.

Remove old Docker packages

If Docker was installed before from Ubuntu’s packages (docker.io, docker-compose, podman-docker), remove them first, or they clash with Docker’s own. This command removes only those packages. Images, containers and volumes in /var/lib/docker/ stay where they are.

sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc | cut -f1)

On a fresh system apt says none of them are installed. That is fine.

Install Docker on Ubuntu from Docker’s repository

These are the commands from Docker’s installation page. They add Docker’s signing key and a repository file for your Ubuntu release, then refresh the package list.

# Add Docker's official GPG key: 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/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: 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

Now install Docker Engine, its command-line tool, containerd and the Buildx and Compose plugins:

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

The service starts by itself and comes back after a reboot. Check it:

sudo systemctl status docker

Look for Active: active (running). If it isn’t running, start it:

sudo systemctl start docker

Check Docker with hello-world

Run the test image. Docker downloads it, starts a container that prints a message and exits.

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

Look for Hello from Docker! in the output. The other two commands print the versions of Docker Engine and of the Compose plugin.

Run docker without sudo

The Docker package creates a docker group. Its members can use the Docker socket without sudo. Add yourself to it and apply the change to the current shell, or log out and back in.

sudo usermod -aG docker $USER newgrp docker docker run hello-world

The docker group gives root-level rights on the machine: a member can start a container that mounts any folder of the system. Add only your own account and people you would trust with sudo.

Start a project with Docker Compose

Compose is now a plugin, so it runs as a subcommand of docker, written with a space. The old docker-compose with a hyphen is not installed. As an example, here is a project with one Nginx container. Create a folder and a compose.yaml file in it:

mkdir -p ~/hello cd ~/hello nano compose.yaml
~/hello/compose.yaml
services: web: image: nginx:alpine ports: - "127.0.0.1:8080:80"

Start it in the background, see what runs, and stop it again:

docker compose up -d docker compose ps docker compose down

Ports that Docker publishes don’t go through UFW rules. With "8080:80" the container would be open to the whole network even if UFW blocks 8080. 127.0.0.1: in front keeps it on this machine only. Open a port to the network only when you mean to.

If docker doesn’t work

  • permission denied while trying to connect to the docker API at unix:///var/run/docker.sock (older versions: …to the Docker daemon socket at…): your user isn’t in the docker group yet, or the shell was opened before you joined it. Run the usermod command above, then log out and back in. groups must list docker.
  • Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?: the service is stopped. Start it: sudo systemctl start docker If it stops again, read its log: sudo journalctl -u docker -n 50
  • WARNING: Error loading config file: /home/user/.docker/config.json: ~/.docker was created by an earlier run of docker with sudo and belongs to root. Give it back to yourself: sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
  • docker-compose: command not found: write compose after docker with a space, as in the section above. The Compose plugin replaces the old program.
  • apt reports a conflict with containerd: an old containerd or runc package is still installed. Run the removal command from the first section and install again.

If you also work on a Mac

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned

System Junk in MacWashing finds the caches of every app it knows, including those hidden inside app data, plus logs, Xcode junk and leftovers of deleted apps. It shows how much each takes and clears them in one go. Anything debatable stays unchecked until you decide.

Free trial · macOS 14 or later

Questions and answers

How do I install Docker on Ubuntu?

Add Docker’s apt repository with its signing key, then install the packages: sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin Check the install: sudo docker run hello-world

Can I use the docker.io package from Ubuntu instead?

Yes, it is Docker built by Ubuntu and gets security fixes. It is usually older than Docker’s own packages. Pick one source and don’t mix them: the packages conflict.

Where does Docker keep images and containers?

In /var/lib/docker/. This shows how much space they take: docker system df This one deletes stopped containers, unused networks, dangling images and the build cache, and asks before it starts: docker system prune