Introduction

Welcome to Chapter 2 of our comprehensive guide! Before we can delve into the powerful world of containerization, we need to lay the groundwork: installing Docker Engine 29.0.2. Docker Engine is the core component that runs and manages containers. While Docker Desktop provides a convenient all-in-one package for developers, understanding the standalone Docker Engine installation is crucial, especially for server environments and advanced configurations. This chapter will walk you through the necessary steps to get Docker Engine up and running on your system.

Main Explanation

Installing Docker Engine involves a few key steps, primarily depending on your operating system. We’ll focus on common Linux distributions, as Docker Engine is most frequently deployed in these environments.

Prerequisites

Before you begin, ensure your system meets the basic requirements:

  • Operating System:
    • Linux: A 64-bit version of CentOS, Debian, Fedora, RHEL, Ubuntu, or other compatible distributions.
    • macOS/Windows: While Docker Engine can be run within a VM on these platforms, Docker Desktop is generally recommended for development. For server-side installation, a Linux VM or cloud instance is typical.
  • Kernel Version: Linux kernel version 3.10 or higher.
  • System Resources: At least 2GB RAM is recommended, with more for production workloads. Adequate disk space for images and containers is also necessary.
  • Internet Connection: Required to download installation packages.

Installation on Linux (Ubuntu/Debian)

The recommended way to install Docker Engine on Ubuntu or Debian is to use the official Docker repository. This ensures you get the latest stable version and receive updates.

  1. Uninstall old versions: Remove any older Docker packages to prevent conflicts.
    for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt remove $pkg; done
    
  2. Set up the repository:
    • Update the apt package index and install packages required to use an HTTPS repository.
      sudo apt update
      sudo apt install ca-certificates curl gnupg
      
    • Add Docker’s official GPG key.
      sudo install -m 0755 -d /etc/apt/keyrings
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      sudo chmod a+r /etc/apt/keyrings/docker.gpg
      
    • Add the repository to apt sources. Replace $(. /etc/os-release && echo "$VERSION_CODENAME") with your Ubuntu version (e.g., jammy, focal) if it fails.
      echo \
        "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
        "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
        sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
      
  3. Install Docker Engine:
    • Update the apt package index again.
      sudo apt update
      
    • Install Docker Engine, containerd, and Docker Compose.
      sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
      

Post-installation Steps

After installing, there are a few crucial steps to ensure Docker runs smoothly.

  1. Verify installation: Check if Docker Engine is running correctly.
    sudo docker run hello-world
    
    You should see a message indicating that your installation appears to be working correctly.
  2. Manage Docker as a non-root user: By default, running Docker commands requires sudo. To run Docker commands without sudo, you need to add your user to the docker group.
    sudo usermod -aG docker $USER
    
    You will need to log out and log back in (or restart your system) for this change to take effect.
  3. Configure Docker to start on boot: Docker is usually configured to start automatically, but you can verify its status and enable it if necessary.
    sudo systemctl enable docker.service
    sudo systemctl enable containerd.service
    

Installation on Other Linux Distributions (CentOS/Fedora)

For RHEL-based systems like CentOS or Fedora, the steps are similar but use yum or dnf.

  1. Uninstall old versions:
    sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
    
  2. Set up the repository:
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
  3. Install Docker Engine:
    sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
  4. Start Docker and enable on boot:
    sudo systemctl start docker
    sudo systemctl enable docker
    
  5. Add user to docker group:
    sudo usermod -aG docker $USER
    
    Log out and log back in.

Examples

Let’s walk through the commands for a typical Ubuntu installation.

Example 1: Full Installation on Ubuntu

# 1. Uninstall old versions (if any)
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt remove $pkg; done

# 2. Update package index and install necessary utilities
sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release

# 3. Add Docker's official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# 4. Add the Docker repository to Apt sources
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 5. Install Docker Engine, containerd, and Docker Compose
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Example 2: Verifying the Installation

After running the installation commands, execute the hello-world container to verify.

sudo docker run hello-world

Expected output (or similar):

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
... (download progress) ...
Digest: sha256:f5233545e435618838e3a130b16230f650315bdfb9edb7191f07e86066370ad3
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output back to the Docker client, which
    sent it to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Example 3: Adding User to Docker Group

To avoid using sudo with every Docker command:

sudo usermod -aG docker $USER

Important: After running this, you must log out and log back in (or restart your machine) for the changes to take effect. Then, you can run docker run hello-world without sudo.

Mini Challenge

Your challenge is to install Docker Engine 29.0.2 on a Linux virtual machine or a cloud instance (e.g., a free tier AWS EC2 instance, a DigitalOcean droplet, or a local VM using VirtualBox/VMware).

  1. Choose a Linux distribution (Ubuntu 22.04 LTS or CentOS Stream 9 are good choices).
  2. Follow the official Docker installation guide for your chosen distribution, ensuring you install Docker Engine 29.0.2 (or the latest stable version if 29.0.2 is not explicitly available via the repository).
  3. Verify the installation by running docker run hello-world.
  4. Add your current user to the docker group and confirm you can run Docker commands without sudo after re-logging in.
  5. Check the installed Docker version using docker --version.

Summary

In this chapter, we successfully navigated the critical first step in our Docker journey: installing Docker Engine 29.0.2. We covered the prerequisites, detailed the installation process for common Linux distributions like Ubuntu, and outlined essential post-installation steps such as verifying the installation and configuring non-root user access. With Docker Engine now installed and ready on your system, you have the foundational component to begin creating, managing, and orchestrating containers, which will be the focus of our upcoming chapters. Get ready to build some amazing things!