How To Install Docker in Debian 10/11/12

Overview

This guide will walk you through the steps to install the Docker engine on Debian(10 (oldstable), 11 (stable), and 12 (testing) from the official Docker APT repository.

Docker is a free and open-source platform that enables developers to build, deploy, and run applications inside containers. Containers are lightweight, portable, and self-contained environments that bundle an application and its dependencies, making it easy to ship the application from a developer’s laptop to a testing environment or a production server.

This guide will walk you through the steps to install the Docker engine on Debian(10 (oldstable), 11 (stable), and 12 (testing) from the official Docker APT repository.

Note: This guide also works for Raspbian versions(Raspbian Bookworm 12 (testing), Raspbian Bullseye 11 (stable), and Raspbian Buster 10 (oldstable)).

Uninstall old Docker Packages

If you have the old versions of Docker packages namely: dockerdocker.io, or docker-engine, and containerd or runc, issue the following command to uninstall them before proceeding to new versions:

$ sudo apt-get remove docker docker-engine docker.io containerd runc

In case you don’t have the above packages installed, the apt-get package manager might report that you have none of them installed.

After removing the old docker packages shown above, if you intend to start with a fresh installation, and wish to remove any previously installed data, you can delete all Docker directories and data as shown here.

Set Up Docker APT Repository

Start by running the following commands to update the apt package index and install the necessary packages to enable the apt package manager to access an HTTPS repository:

$ sudo apt-get -y update
$ sudo apt-get -y install ca-certificates curl gnupg lsb-release

Then run the following commands to add Docker’s official GPG key:

$ sudo install -m 0755 -d /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
$ sudo chmod a+r /etc/apt/keyrings/docker.gpg

Next, issue this command to add and configure the repository:

$ echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian   "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" |  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker Engine

Now run the next commands to update the apt package index again and install the Docker Engine, containerd, and Docker Compose:

$ sudo apt-get -y update
$ sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Post-installation Steps for Docker Engine

With the help of these post-installation procedures, you can configure your Linux host computer so that Docker can operate more efficiently on it.

Manage Docker Without Sudo(as a non-root User)

The Docker daemon uses/listens on a Unix socket, instead of a TCP port. The Unix socket is typically owned by the root user, and other users can only access it through sudo. This requires that you always run Docker daemon with root user permissions.

Additionally, when the Docker daemon starts, the Unix socket it creates is accessible by the root user and members of the docker group. To avoid running the docker command with sudo, create a system group called docker and add users to it.

Note that if the docker group is already created at the time of installation of the docker package, ignore the error message:

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

Log out and back in again to have your group membership re-evaluated. Alternatively, you can use the following command to initiate the group changes:

$ newgrp docker

Run the groups command to ensure that the current user is now a member of the docker group:

$groups
Verify Ddocker group membership for the current user
Verify Docker Engine Installation

To verify that the docker installation is correct and that you can run docker commands without sudo, run the following command which will download a test image and runs it in a container. When the container runs, it prints a message and exits as shown in the following screenshot:

$ docker run hello-world
Test Docker engine installation by running the hello-world image
Enable Docker to Start on Boot With Systemd

Run the following commands to have Docker and containerd start automatically on boot:

$ sudo systemctl enable docker.service
$ sudo systemctl enable containerd.service
Uninstall Docker Engine

To uninstall the Docker Engine, CLI, contained, plugins, as well as Docker Compose packages, run the following command:

$ sudo apt-get purge docker-ce docker-ce-cli containerd.io  docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin
Delete Docker Files/Directories

Once you have uninstalled Docker, all images, containers, volumes, and custom configuration files on your host are not automatically deleted. To delete all them, use the following commands:

$ sudo rm -rf /var/lib/docker
$ sudo rm -rf /var/lib/containerd
Conclusion

Congratulations! You have successfully installed and configured Docker Engine on Debian Debian, from the official Docker APT repository. For any questions, use the feedback form below.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Page Contents