# Docker Engine (Part 2)

## Prerequisites & Installation Methods

Before starting, ensure you have a running Linux server (on AWS EC2, Azure, GCP, VMware, or VirtualBox) with `sudo` privileges.

Docker provides three ways to install the engine on Linux:

1. **Using Docker's Official Repositories (Recommended):** Best for production; makes installation and future updates seamless via `yum` or `apt`.
2. **Manual Package Installation:** Downloading `.rpm` or `.deb` packages manually—useful for air-gapped servers without internet access.
3. **Convenience Script (**[`get.docker.com`](http://get.docker.com)**):** A quick shell script for testing/development environments (not recommended for production).

We will use **Method 1 (Official Repositories)** for both distributions below.

## Part 1: Installing Docker Engine on CentOS

### Prerequisites

- A maintained version of CentOS 7, 8, or 9 (e.g., CentOS 7.7+).
- The `centos-extras` repository enabled (enabled by default).

### Step 1: Remove Older Versions

Older versions of Docker were distributed under names like `docker` or `docker-engine`. Remove any legacy packages to avoid conflicts:

```plaintext
sudo yum remove docker \
                docker-client \
                docker-client-latest \
                docker-common \
                docker-latest \
                docker-latest-logrotate \
                docker-logrotate \
                docker-engine
```

### Step 2: Set Up the Stable Repository

Install `yum-utils` (which provides the `yum-config-manager` utility) and add Docker's official stable repository:

```plaintext
sudo yum install -y yum-utils

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
```

### Step 3: Install Docker Engine

Install the latest version of Docker Engine (`docker-ce`), the CLI (`docker-ce-cli`), and [`containerd.io`](http://containerd.io):

```plaintext
sudo yum install -y docker-ce docker-ce-cli containerd.io
```

### Step 4: Verify Package Installation & Start the Service

Check that the `docker-ce` package is installed using native RPM commands:

```plaintext
rpm -qa | grep docker
```

Unlike Ubuntu, **CentOS does not start the Docker daemon automatically** after installation. Check its status, start it, and enable it to launch automatically on system boot:

```plaintext
sudo systemctl status docker
sudo systemctl start docker
sudo systemctl enable docker
```

## Part 2: Installing Docker Engine on Ubuntu

### Prerequisites

- A 64-bit (`x86_64` or `arm64`) maintained version of Ubuntu (Ubuntu 16.04, 18.04, 20.04, 22.04, or 24.04).

### Step 1: Remove Older Versions

Clean up any older or unofficial Docker packages before starting:

```plaintext
sudo apt-get remove docker docker-engine docker.io containerd runc
```

### Step 2: Install Prerequisite Packages

Update the `apt` package index and install dependencies required to allow `apt` to use repositories over HTTPS:

```plaintext
sudo apt-get update

sudo apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
```

### Step 3: Add Docker’s Official GPG Key & Repository

Download Docker’s official GPG key to verify package integrity, then register the stable repository:

```plaintext
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
```

### Step 4: Install Docker Engine

Update the package index again so Ubuntu recognizes the new Docker repository, then install Docker Engine:

```plaintext
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
```

### Step 5: Verify Package & Service Status

Confirm the package installation using `dpkg`:

```plaintext
dpkg -l | grep docker
```

On Ubuntu, the Docker service **starts and enables itself automatically** during installation. Verify that it is active:

```plaintext
sudo systemctl status docker
```

## Part 3: Verifying the Docker Installation (Both Distros)

Once the daemon is active on either CentOS or Ubuntu, verify the installation using native Docker CLI commands:

### 1\. Check Quick Version (`docker -v`)

Displays a single-line output of the installed Docker CLI version:

```plaintext
docker -v
```

### 2\. Inspect Client & Server Details (`docker version`)

Displays detailed information about both the **Docker Client** and the **Docker Server (Daemon)**—including the edition (`Community`), the Go version it was compiled with, and underlying runtime components like `containerd` and `runC`:

```plaintext
sudo docker version
```

### 3\. Inspect Host Configuration (`docker system info`)

Provides a complete system-wide summary of your Docker host, including:

- Total number of running, paused, and stopped containers
- Number of cached images
- Storage driver and default logging driver
- Default container registry (`[`[`https://index.docker.io/v1/](https://index.docker.io/v1/)`](https://index.docker.io/v1/]\(https://index.docker.io/v1/\)))

```plaintext
sudo docker system info
```

### 4\. Run a Test Container

Finally, confirm that your host can pull images from Docker Hub and execute a container cleanly:

```plaintext
sudo docker run hello-world
```
