Best Steps To Install Docker CE on Debian 12 Bookworm

Posted on

Best Steps To Install Docker CE on Debian 12 Bookworm

Best Steps To Install Docker CE on Debian 12 Bookworm

In this guide, we aim to instruct you on how to Install and Use Docker CE on Debian 12 Bookworm. Docker is a powerful open-source platform that streamlines the creation, deployment, and execution of applications using containers. Let’s first define what Docker CE means before diving into the installation process on Debian 12, following the steps provided by Orcacore.

What is the difference between Docker and Docker CE?

Docker CE is a specific distribution of Docker provided by Docker itself. It is made available through third-party package repositories tailored for major Linux distributions. Similar to the docker.io and docker packages, docker-ce is free and open-source. This guide will walk you through how to Install Docker CE on Debian 12 and get you started.

Before you begin the process to Install Docker CE on Debian 12, ensure you have access to your server as a non-root user with sudo privileges. If you haven’t already, you can follow the Orcacore guide on Initial Server Setup with Debian 12 Bookworm.

Once your server is properly configured, proceed with the following steps to Install Docker CE on Debian 12.

Step 1 – Run System Update and Install Required Packages for Docker

Begin by updating and upgrading your APT packages. This ensures you have the latest package information and dependencies:

sudo apt update && sudo apt upgrade -y

Next, install the necessary packages required for Docker to function correctly on Debian 12:

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y

Step 2 – Add Docker’s official GPG key on Debian 12

To ensure the authenticity of the Docker packages, add the official Docker repository’s GPG key to your system. This verifies that the packages you download are genuinely from Docker:

# 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

Step 3 – Set up Docker Repository on Debian 12

Now, add the Docker repository to your APT sources. This tells your system where to find the Docker CE packages:

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

Step 4 – How To Install Docker on Debian 12?

With the repository configured, update your system’s package list and proceed with the installation of Docker CE:

# sudo apt update
# sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin -y

How To Check Docker Status?

After the installation, Docker CE service should be enabled and running. Verify its status using the following command:

sudo systemctl status docker

The output should indicate that the Docker service is active and running, as shown below:

[Example Output]
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since ...

At this point, the docker command-line utility is installed and ready to use. The rest of this guide will demonstrate how to use it on Debian 12.

Step 5 – Run Docker Commands without sudo

By default, the docker command can only be executed by the root user or a user belonging to the docker group. This group is automatically created during the installation.

To avoid using sudo before every docker command, add your user to the docker group:

sudo usermod -aG docker ${USER}

Apply the changes by logging out and back in, or by running:

su - ${USER}

You will be prompted for your user’s password to continue.

Verify that your user has been added to the docker group:

id -nG

For the remainder of this guide, it is assumed that you are running docker commands as a user within the docker group. If you prefer not to, remember to use sudo before each command.

Now that you have finished the steps to Install Docker CE on Debian 12, let’s explore how to use the docker command-line utility.

Step 6 – How To Use Docker Command Line Utility

The basic syntax for the docker command is:

docker [option] [command] [arguments]

To view all available subcommands for docker, simply run:

docker

This will display a list of available commands and their descriptions.

How To Work with Docker Images on Debian 12?

A Docker Image is a read-only template containing instructions for creating a Docker container. It specifies the software components the container should run and how to run them.

By default, Docker pulls images from Docker Hub. Docker Hub is a public registry where anyone can host their Docker images, offering a vast library of applications and Linux distributions.

Verify your access to Docker Hub and your ability to download images using the following command:

docker run hello-world

The output should confirm a successful download and execution of the hello-world image.

Search for available Docker Images

You can search for available images on Docker Hub using the search subcommand.

For example, search for the Debian image:

docker search debian

This will display a list of images matching the search term "debian".

Download a Docker Image

Download the official Debian image using the following command:

docker pull debian

This will download the latest version of the Debian image to your system.

List Downloaded Docker Images

To see a list of all images downloaded to your system, run:

docker images

This will display a table showing the repository, tag, image ID, creation date, and size of each image.

How to Run a Docker Container on Debian 12?

Containers provide an isolated environment for running applications, sharing the host operating system’s kernel.

To run a container using the latest image of Debian, use the following command:

docker run -it debian

Note: The -it switches provide interactive shell access to the container.

This will start a new container based on the Debian image and drop you into a shell prompt within the container.

Important note: Take note of the container ID.

You can now execute commands within the container without using sudo, as you are running as the root user inside the container.

Update the packages inside the container:

root@<container_id>:/# apt update

Install any application you desire. For example, let’s install Apache:

root@<container_id>:/# apt install apache2

After the installation is complete, verify that Apache is installed:

root@<container_id>:/# apache2 -v

Note: Any changes made inside the container only apply to that specific container.

To exit the container, type exit:

root@<container_id>:/# exit
List Active Docker Containers

To view currently running containers, use the following command:

docker ps

To see all containers, both active and inactive, run:

docker ps -a

You can also view the latest container created using:

docker ps -l
Manage Docker Containers

You can start and stop containers using their container ID or name.

Start the Debian-based container using its ID:

docker start <container_id>

Check the status to ensure the container is active:

docker ps

Stop the container using its name:

docker stop <container_name>

Remove a container using its name:

docker rm <container_name>

Containers can be transformed into images for creating new containers.

Step 7 – How To Make Docker Containers to Docker Images?

After installing Apache inside the Debian container, you now have a modified container. To preserve these changes, you can commit them to a new docker image.

Commit the changes to a new docker image using the following command:

docker commit -m "<description>" -a "<author>" <container_id> <repository>/<new_image_name>

For example:

docker commit -m "install Apache" -a "olivia" 70c3b825b259 olivia/debian-apache

This will save the new image on your system.

List the docker images to verify the creation of the new image:

docker images

Note: The size difference between the original image and the new image reflects the changes made.

Now you can share the new image with others.

After creating a new image from an existing image on Debian 12, you can share it with others on Docker Hub or other Docker repositories.

First, create an account on Docker Hub.

Then log in to your Docker Hub account to push your image:

docker login -u <docker-registry-username>

You will be prompted for your Docker Hub password.

Note: If your Docker registry username differs from the local username used to create the image, tag your image with your registry username.

For example:

docker tag olivia/debian-apache <docker-registry-username>/debian-apache

Then push your own image:

docker push <docker-registry-username>/debian-apache

The process may take some time.

After pushing the image to a registry, it should be listed on your account’s dashboard.

Note: If a push attempt results in an error, log in again and repeat the push attempt. Verify that it exists on your Docker Hub repository page.

Conclusion

At this point, you have successfully learned how to Install Docker CE on Debian 12 Bookworm. You’ve also learned to work with Docker images and containers, manage containers, create images from containers, and share them on a Docker repository. You now have the knowledge to Install Docker CE on Debian 12 and begin containerizing your applications!

Alternative Solutions for Installing Docker on Debian 12

While the steps outlined above provide a reliable method for installing Docker CE on Debian 12, alternative approaches can be considered, each with its own advantages and disadvantages.

1. Using Docker’s Convenience Script:

Docker provides a convenience script that automates the installation process. This method simplifies the installation but may offer less control over the specific installation parameters. This is a great way to Install Docker CE on Debian 12 if you want an easy install.

Explanation:

The convenience script handles the steps of adding the Docker repository, installing the GPG key, and installing the Docker packages. It’s suitable for quick setups and testing but may not be ideal for production environments where specific configurations are required.

Code Example:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

This script downloads and executes the Docker installation script. It prompts for confirmation before installing and adds the current user to the docker group.

2. Using Ansible for Automated Installation:

Ansible is an automation tool that can be used to provision and manage infrastructure. Using Ansible, you can create a playbook to automate the Docker installation process, ensuring consistent and repeatable installations across multiple servers. This is an excellent way to Install Docker CE on Debian 12 in an automated and repeatable manner.

Explanation:

Ansible playbooks define the desired state of the system, including package installations, file configurations, and service management. This approach provides greater control and scalability compared to manual installation or the convenience script.

Code Example:

First, install Ansible on your control machine:

sudo apt update
sudo apt install ansible

Then, create an Ansible playbook (e.g., docker_install.yml):

---
- hosts: all
  become: true
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install required packages
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - gnupg2
          - software-properties-common
        state: present

    - name: Add Docker GPG key
      apt_key:
        url: https://download.docker.com/linux/debian/gpg
        state: present

    - name: Add Docker repository
      apt_repository:
        repo: deb [arch=amd64] https://download.docker.com/linux/debian bookworm stable
        state: present
        filename: docker

    - name: Install Docker CE
      apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
        state: present
        update_cache: yes

    - name: Add user to docker group
      user:
        name: "{{ ansible_user }}"
        groups: docker
        append: yes

    - name: Restart Docker service
      systemd:
        name: docker
        state: restarted
        daemon_reload: yes

Replace "{{ ansible_user }}" with the username you are using to connect to the server.

Finally, run the playbook:

ansible-playbook docker_install.yml

This playbook automates the Docker installation process, ensuring consistency across multiple Debian 12 servers.