How to Install Docker on Debian 12, 11, or 10

Docker has rapidly gained popularity due to its ability to simplify the deployment process by containerizing applications, ensuring consistency across multiple environments. Its lightweight nature and ability to scale efficiently have made it a favorite among developers and IT professionals.

  • Portability: Docker containers can run on any system that supports Docker, ensuring your application works seamlessly across different environments.
  • Isolation: Each container operates independently, reducing the risk of conflicts and making it easier to manage dependencies.
  • Efficiency: Containers share the host OS kernel, making them more efficient than traditional virtual machines.
  • Scalability: Docker allows for easy scaling of applications, accommodating varying loads by adding or removing containers.
  • Version Control: Track changes and roll back to previous versions effortlessly, enhancing the development and deployment workflow.

Debian is a preferred choice to pair with Docker for several reasons:

  • Stability: Known for its robust and reliable nature, Debian ensures a stable foundation for Docker containers.
  • Security: Regular updates and a strong focus on security make Debian a secure environment for deploying applications.
  • Community Support: A large and active community provides extensive documentation and support, making troubleshooting easier.
  • Performance: Lightweight and efficient, Debian ensures that Docker containers run smoothly with minimal overhead.

With the introduction out of the way, let’s explore how to install Docker on Debian, utilizing terminal commands and various methods to get your environment up and running efficiently.

Docker Pre-Installation Steps

Setting up Docker CE on your Debian system is straightforward yet meticulous. Before delving into the installation process, let’s set the stage for an error-free execution.

Step 1: Remove Previous Docker Instances

Note: If you do not have the default version of Docker installed from Debian’s repository, skip the removal step.

First, we must purge any pre-existing Docker installations to ensure a conflict-free environment. Prior Docker versions could interfere with our upcoming installation and lead to unexpected errors. Use the following command to uninstall older Docker iterations if they exist:

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

Should there be no older Docker instances to remove, the apt package manager will return a message indicating nothing to uninstall.

It’s crucial to remember that uninstalling Docker does not automatically delete Docker images, containers, volumes, or networks that are usually stored in /var/lib/docker/. If you intend to start afresh and eliminate all Docker-related data, use these commands:

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

With this, you’ve purged any potential Docker residues that could influence your installation process.

Step 2: Update Debian Packages Before Docker Installation

After removing older Docker versions, ensure your Debian system is fully updated as your next step. Updating promotes system stability and ensures your system packages are at their latest versions, minimizing potential conflicts and vulnerabilities.

To update the list of available packages and upgrade the installed ones, run the following command:

sudo apt update && sudo apt upgrade

This command will first refresh the list of available packages (apt update), followed by the upgrade of any outdated packages (apt upgrade).

Import Docker CE APT Repository

To successfully install Docker CE, it’s essential to configure your Debian system to access the Docker repository. This involves integrating the Docker repository into your system and importing the corresponding GPG key. These steps ensure the authenticity of the downloaded Docker packages and mitigate the risk of unauthorized alterations.

Step 1: Install Initial Packages For Docker CE

Initially, the system may lack the necessary packages for this process. Let’s rectify that by installing them. Execute the following command to install these critical packages:

sudo apt install ca-certificates curl gnupg lsb-release dirmngr software-properties-common apt-transport-https

Here, you’re leveraging the apt package manager to install a variety of tools, like ca-certificates for certificate verification, curl to transfer data, gnupg for key management, and others necessary for this process.

Step 2: Add Docker CE GPG Key

After installing the required packages, let’s import the Docker GPG key. This key enables your system to verify the integrity of packages downloaded from the Docker repository.

Use the following commands to download and save the 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

The curl command fetches the GPG key from the Docker repository, which is then processed by gpg --dearmor to convert it into the binary format that apt requires.

Step 3: Add Docker CE APT Repository

With the GPG key in place, you can now import the Docker repository. Here’s the command to accomplish that:

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

This complex-looking command adds the Docker repository to your system’s source list and links it to the previously downloaded GPG key. This link verifies the integrity of the packages downloaded from the Docker repository, keeping your system secure.

Finalize Docker Installation

This section outlines the necessary steps to install Docker CE on your Debian system. You’ll learn how to update your system’s repository information, install Docker, and verify the installation by executing a test Docker image. Moreover, the section includes a crucial security practice to ensure the secure handling of Docker containers and images.

Step 1: Update Debian APT Cache After Docker CE Repository Import

Before initiating the Docker installation, ensuring that your system’s repository information is up-to-date is advantageous, particularly with the recently added Docker repository. To update the repository information, execute the following command:

sudo apt update

This command refreshes your system’s package lists, including the details about the latest versions of packages and their dependencies.

Step 2: Install Docker via APT Command

With the system’s repository information updated, you can proceed with the Docker installation. Here’s the command to install Docker along with some additional plugins that enhance your Docker experience:

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

In this command, you’re installing docker-ce (Docker Community Edition), docker-ce-cli (the Docker command-line interface), containerd.io (an industry-standard runtime), and two useful Docker plugins for building images and managing multi-container applications.

Step 3: Verify Docker CE Installation on Debian

After completing the Docker installation, prudently verify its correct installation by running a test Docker image:

sudo docker run hello-world

This command pulls the hello-world image from the Docker repository, creates a new container from this image, and runs the container. When executed, it prints a welcome message, thus confirming that Docker is functioning as expected.

Step 4: Running Docker as a Non-Root User

For security reasons, configure Docker to run as a non-root user. This practice safeguards your system against accidental or malicious changes that could cause harm. A later section will discuss this aspect more thoroughly.

Docker Troubleshooting Tip

While interacting with Docker containers and images, if you encounter any issues, a system reboot might help resolve them, particularly ones related to path generation. To restart your system, use the following command:

reboot

Managing Docker via Systemd

This segment will explore how to manipulate the Docker service using systemd. Systemd is a fundamental component of many Linux distributions, including Debian, and provides functionalities to administer system processes and services. As Docker installs a systemd unit on your Debian system, this presents an efficient way to manage the Docker service.

Starting the Docker Service via systemd

To utilize Docker, the service must be actively running on your Debian system. Systemd facilitates this through the following command:

systemctl start docker.service

This command starts the Docker service and configures it to launch automatically upon system boot, ensuring its availability after each startup.

Stop Docker Service via systemd

In certain circumstances, you may want to stop the Docker service. Systemd provides a straightforward command to accomplish this:

systemctl stop docker.service

This command halts the Docker service and prevents it from starting automatically on the next system boot.

Restart Docker Service via systemd

You may sometimes want to restart the Docker service, especially when troubleshooting Docker-related issues. Systemd provides the functionality to restart the Docker service with this command:

systemctl restart docker.service

This command stops and starts the Docker service, effectively applying the most recent configurations.

Check Docker Service Status via systemd

To check the operational status of the Docker service, whether it’s running, stopped, or inactive, you can use the following systemd command:

systemctl status docker.service

The command fetches and displays the current status of the Docker service, providing insights into its operational condition.

Enable Docker Service on Debian System Boot via systemd

If you wish the Docker service to start automatically at system boot, enabling it through systemd is the preferred method:

systemctl enable docker.service

This command adjusts the Docker service settings to start automatically at each system boot, providing continuous availability of Docker functionalities.

Disable Docker Service on System Boot via systemd

If you decide not to have the Docker service automatically start during system boot, systemd can accommodate this request with the following command:

systemctl disable docker.service

This command alters the Docker service settings, preventing it from initiating automatically at system boot.

Utilizing systemd commands, you can effectively manage the Docker service on your Debian system, providing a high degree of control over Docker’s operational behavior. The following sections will dive into more specifics about utilizing and managing Docker containers and images.

Examples Of How to Configure Docker

In this guide portion, we delve into setting up Docker configurations. This includes managing Docker as a non-root user, which enhances security, and altering the default logging driver to better suit your needs.

Step 1: Run Docker as a Non-Root User

While you can run Docker as a root user, doing so is discouraged because of potential security risks and accidental modifications to your Debian host system. Instead, manage Docker operations under a non-root user account to enhance security.

To create a new user specifically for Docker management, execute the following commands:

sudo useradd -m dockeruser
sudo usermod -aG docker dockeruser

These commands create a new user named ‘dockeruser’ and append this user to the Docker group. Being a member of the Docker group grants a user the necessary permissions to run Docker commands.

If you wish to assign your current user to the Docker group, replace ‘dockeruser’ with your username. For instance, for a user named ‘joshua’, the command would be:

sudo usermod -aG docker joshua

After making these changes, log out and back in to apply the modifications. Sometimes, you might need to restart the system to propagate these changes.

To confirm the user has permission to run Docker commands, use the following command:

docker ps

The command should display a list of running Docker containers, signifying a successful installation.

Step 2: Modify Default Docker Logging Driver

Docker, by default, logs events in JSON file format. Nonetheless, Docker’s flexibility allows you to change the default logging driver to a different format or even configure it to forward logs to a remote log management system.

To modify the default logging driver, you need to create a new file called daemon.json in the /etc/docker/ directory. Utilizing a text editor such as nano, execute the following command:

sudo nano /etc/docker/daemon.json

When the file is open, paste the following content into it:

{
  "log-driver": "syslog",
  "log-opts": {
    "syslog-address": "tcp://logs.example.com:514",
    "syslog-facility": "daemon",
    "tag": "{{.Name}}"
  }
}

Docker uses the syslog driver in this configuration, and relays logs to a remote syslog server. Replace logs.example.com with your syslog server’s address.

Once the configuration is in place, restart the Docker daemon using the following command to apply the new logging driver settings:

sudo systemctl restart docker.service

Please remember any modifications made to the daemon.json file necessitate a Docker daemon restart for the changes to be applied.

Docker Command Examples

In this segment, we delve into the usage of Docker commands that play a vital role in effectively managing Docker containers, images, networks, and volumes. The docker command provides a robust and versatile toolset designed to simplify and automate tasks in your Docker environment.

Fundamentals of Docker Commands

Familiarizing oneself with the Docker command line interface (CLI) is essential to mastering Docker. Below is an assortment of commands you’ll frequently encounter in your Docker journey:

  • docker run: Launches a new container from an image.
  • docker ps: Displays all currently running containers.
  • docker images: Lists all locally available images.
  • docker build: Constructs a new image from a Dockerfile.
  • docker stop: Halts a currently running container.
  • docker rm: Eliminates a container.
  • docker rmi: Deletes an image.
  • docker network: Administers Docker networks.
  • docker volume: Manages Docker volumes.

Each command has a unique set of options that allow you to modify its behavior to meet your needs. Let’s explore each command and its respective options.

The docker run Command

The docker run command creates a new container from a specified image. For example, to initiate a container from the Debian image and open an interactive shell within it, use the following command:

docker run -it debian:latest /bin/bash

The docker ps Command

The docker ps command is deployed to enlist all currently active containers. It reveals valuable information about each container, including container ID, associated image, and running status. To get a list of all running containers, simply type:

docker ps

The docker images Command

The docker images command is tasked with listing all locally available Docker images. It returns information about each image, including its ID, associated repository, and tag:

docker images

The docker build Command

The docker build command is used to create a new Docker image from a Dockerfile. A Dockerfile is essentially a script housing instructions for constructing a Docker image. For example, to build a new image named ‘myimage’ using the Dockerfile in the current directory, you’d use the following command:

docker build -t myimage:latest .

The docker stop Command

The docker stop command gracefully terminates a running Docker container. You can target the desired container using its ID. For example, to stop a container with ID ‘abcdefg,’ you’d use:

docker stop abcdefg

The docker rm Command

Use the docker rm command to delete a Docker container. Like the stop command, specify the container ID of the container you wish to delete. For example:

docker rm abcdefg

The docker rmi Command

Use the docker rmi command to remove Docker images. Identify the image you want to delete by its ID. For example:

docker rmi 1234567

The docker network Command

The docker network command is a versatile tool for creating, listing, and removing Docker networks. For instance, to create a new network named ‘mynetwork,’ you’d use:

docker network create mynetwork

The docker volume Command

Finally, the docker volume command offers functionalities to manage Docker volumes. For instance, to create a new volume named ‘myvolume,’ you would use:

docker volume create myvolume

Navigating Docker Container Management

Effectively managing Docker containers significantly influences the functionality and longevity of a Docker environment. The docker command provides a variety of crucial sub-commands for manipulating Docker containers. These sub-commands facilitate container creation, operation, modification, and deletion. Whether you are a seasoned developer or a beginner in containerization, understanding these commands can enhance your Docker interaction substantially.

Core Commands for Docker Container Management

The command docker ps is an essential tool in your Docker toolkit for enumerating all running containers. By executing this command, you can see all active containers, their corresponding image, status, and unique container ID.

docker ps

To terminate a currently active Docker container, the docker stop command is deployed. You append the unique ID or name of the container you want to stop.

docker stop abcdefg

The docker rm command is used to eradicate a Docker container. This command, like docker stop, accepts either the container’s unique ID or name as an argument.

docker rm abcdefg

It’s crucial to note that deleting a container discards any modifications made. You must create a new image from the modified container using the ‘docker commit’ command to retain changes.

Retaining Container Changes using docker commit

While working with Docker containers, you might need to customize a container and preserve these alterations as a new image. This can be accomplished using the docker commit command.

Initiate a new container using a base image and make the necessary alterations within this container. For instance, consider starting a new container from the Debian image and opening a shell inside the container:

docker run -it --name mycontainer debian:latest /bin/bash

You can perform various tasks in this new container, such as modifying configuration files or installing new software. After making the desired changes, use the docker commit command to produce a new image encapsulating these alterations. To create a new image named ‘myimage’ with the changes made in the ‘mycontainer’ container, execute the following command:

docker commit mycontainer myimage:latest

You now possess an image named ‘myimage’, incorporating changes made in the ‘mycontainer’ container. Use this new image to generate and operate new containers with updated configurations or software.

Remember, the docker commit command only saves modifications made to the container’s filesystem, not preserving changes to networking and storage. If retaining changes in networking and storage is necessary, consider using other Docker commands like docker network and docker volume.

Wrapping Up

We delved into installing and managing Docker Community Edition (CE) on a Debian Linux distribution throughout our discussion. Docker CE brings all the benefits of containerization, allowing developers to build, ship, and run distributed applications in different environments. The Docker commands and use cases discussed here offer foundational knowledge for managing Docker containers. While the article touched on fundamental Docker commands like docker run, docker ps, docker rm, and others.

Remember that the Docker ecosystem is much broader, with numerous commands and options that you can explore to fine-tune your Docker experience.

Leave a Comment