Step-by-Step Guide: Installing Docker on Debian 11

Docker is an open-source platform that offers OS-level virtualization, enabling developers to package applications into standardized units referred to as containers. Each container operates independently, containing its own libraries and configuration files, yet communicates seamlessly across a network of defined channels.

Today, Docker is quintessential in software development and DevOps, utilized by millions of developers for building, sharing, and running applications. It traverses the entire application development lifecycle—development, testing, and production—rendering applications highly flexible and portable across various operating systems while maintaining intact libraries and configurations.

This guide will walk you through the process of installing Docker on Debian 11 Bullseye, along with some foundational Docker operations such as container execution and image management.

Prerequisites

  • A Debian 11 server with packages updated to their latest versions.
  • A root user or a user with root privileges to install new packages and modify system settings.

Install Dependencies

Initiate by installing essential package dependencies on your Debian system:

Execute the following apt command to install necessary package dependencies:

apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Type ‘y‘ and press ‘Enter‘ to confirm the installation.

Installing packages dependencies for Docker

Adding Docker Repository

Install Docker from its official repository by adding it to your Debian system:

Run the command below to add Docker’s GPG key:

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Create the Docker repository using the subsequent command:

echo \
 "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
 $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Refresh the repositories by executing:

apt update

Add Docker key and repository

Installing Docker on Debian 11

If you previously installed Docker from the Debian repository, remove it with:

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

Proceed with Docker installation:

apt install docker-ce docker-ce-cli containerd.io

Type ‘y‘ and press ‘Enter‘ to confirm the installation.

Installing Docker container

Verify the installation by checking the service status:

systemctl is-enabled docker
systemctl is-enabled containerd
systemctl status docker containerd

Checking docker service status

The Docker and Containerd services should display as active (running) and set to enabled.

Allow Non-root User to Use Docker

Docker can traditionally only be run by the ‘root‘ user. To enable non-root users to execute Docker containers, follow these steps:

Create the ‘johndoe‘ user:

useradd -m -s /bin/bash johndoe

Add ‘johndoe‘ to the ‘docker‘ group:

usermod -aG docker johndoe

Log in as ‘johndoe‘ to verify:

su - johndoe

Run the following command:

docker run hello-world

Allow non-root user to run docker - docker hello-world

      • The command attempts to find a local ‘hello-world’ image. If unavailable, Docker downloads it from Docker Hub.
      • Once downloaded, Docker runs the container, resulting in the ‘Hello Docker’ message.

Check container status using:

docker ps
docker ps -a

Checking container status

Docker Basic Usage

Here, you’ll learn basic Docker operations like managing images and containers.

Download Docker images to your local system:

docker pull nginx:alpine

Download docker image

To view all Docker images available on your system:

docker images

Checking container on local system

Run a container using the following command:

docker run -it --rm -d -p 8080:80 --name web nginx:alpine

Access the running container using:

docker ps

Running docker container

Verify your Nginx container by navigating to:

http://192.168.1.10:8080/

Verify Nginx status Docker container

Check the logs of the container using:

docker logs web

Docker check logs

Limit the log output to the last 10 lines:

docker logs --tail 10 web

Limit docker log in Nlines

Stop the Docker container:

docker stop web

Verify with:

docker ps
docker ps -a

Stop docker container and Checking docker container

Run a container with a custom volume, utilizing a directory on your host machine:

mkdir -p ~/data/
nano ~/data/index.html

Enter the following HTML content:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Welcome to Docker Nginx</title>
</head>
<body>
  <h2>Hello from Nginx container</h2>
</body>
</html>

Save by pressing ‘Ctrl+x‘, then ‘y‘, and ‘Enter‘. Run the new container:

docker run -it --rm -d -p 8080:80 --name web -v ~/data:/usr/share/nginx/html nginx:alpine

Verify the running container:

docker ps

Running docker container with custom volume

Check the custom HTML content at:

http://192.168.1.10:8080/

Nginx container with custom index.html and docker volume

    1. Access the running container’s shell:
docker exec -it web /bin/sh

Retrieve the hostname, IP, and routing details using:

hostname
ip a
route -n

Attach to docker cocntainer

Clean the environment by stopping the ‘web‘ container and removing exited ones:

docker stop web
docker rm $(docker ps --filter "status=exited" -q)

Conclusion

Congratulations! You have successfully installed Docker on Debian 11 Bullseye. By following this tutorial, you’ve become familiar with Docker basics: image pulling, container execution, status checking, log review, and basic volume usage. For a more advanced step, try creating custom Docker images for your application and push them to Docker Hub.

FAQ

  • What is Docker? Docker is an open platform providing OS-level virtualization for applications, packaged in containers for ease of transport and deployment.
  • Why should developers use Docker? It offers portability, consistency, and scalability, making it easier to develop, test, and deploy applications across multiple environments without compatibility issues.
  • Can Docker containers run on any operating system? Yes, thanks to containers encapsulating all necessary software components, they can run consistently on any OS that supports Docker.
  • Is Docker free to use? Yes, Docker is open-source and available for free, with additional enterprise features provided by Docker Inc. for a fee.
  • Why should non-root users have Docker access? It’s a security best practice to minimize root-level permissions, and granting Docker access to non-root users allows safer operational workflows.