# How to Install Docker on Ubuntu 20.04

## What is Docker

[Docker](https://docs.docker.com/get-docker/?_gl=1*a27hw4*_ga*MTkyODg5NDg1Mi4xNzAxNTI0MDA1*_ga_XJWPQMJYHQ*MTcwMjEyNzMzOC4xMy4xLjE3MDIxMjczMzkuNTkuMC4w) is a powerful tool that allows us to build, run, and distribute applications by using containerization technology. [Containers](https://docs.docker.com/get-started/#:~:text=using%20Docker%20Compose.-,What%20is%20a%20container%3F,-A%20container%20is) are lightweight, standalone, executable packages that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. They are isolated from each other and the host system.

# **Install Docker Engine on Ubuntu**

Before [installing Docker](https://docs.docker.com/engine/install/ubuntu/) on Ubuntu 20.04, make sure you meet the prerequisites.

### Prerequisites

[OS requirements](https://docs.docker.com/engine/install/ubuntu/#os-requirements:~:text=and%20ufw.-,OS%20requirements,-To%20install%20Docker)

To install Docker Engine, you need the 64-bit version of one of these Ubuntu versions:

* Ubuntu Mantic 23.10
    
* Ubuntu Lunar 23.04
    
* Ubuntu Jammy 22.04 (LTS)
    
* Ubuntu Focal 20.04 (LTS)
    

Docker Engine for Ubuntu is compatible with x86\_64 (or amd64), armhf, arm64, s390x, and ppc64le (ppc64el) architectures.

[**Uninstall old versions**](https://docs.docker.com/engine/install/ubuntu/#uninstall-old-versions)

Before you can install Docker Engine, you need to uninstall any conflicting packages.

Distro maintainers provide unofficial distributions of Docker packages in APT. You must uninstall these packages before you can install the official version of Docker Engine.

The unofficial packages to uninstall are:

* [`docker.io`](https://docs.docker.com/engine/install/ubuntu/#uninstall-old-versions)
    
* `docker-compose`
    
* `docker-compose-v2`
    
* `docker-doc`
    
* `podman-docker`
    

Moreover, Docker Engine depends on `containerd` and `runc`. Docker Engine bundles these dependencies as one bundle: [`containerd.io`](https://docs.docker.com/engine/install/ubuntu/#uninstall-old-versions). If you have installed the `containerd` or `runc` previously, uninstall them to avoid conflicts with the versions bundled with Docker Engine.

### [**Install using the apt repository**](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository)

1. Ensure that Ubuntu has the right version by running the following command:
    
    ```bash
    lsb_release -a
    ```
    
    ![](https://i.imgur.com/jesVYNx.png align="left")
    
2. Ensure that you do not have any conflicting packages by uninstalling old versions. Run the following command:
    
    ```bash
     for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman- 
    docker containerd runc; do sudo apt-get remove $pkg; done
    ```
    
    ![](https://i.imgur.com/ywyIiSR.png align="center")
    
    Images, containers, volumes, and networks stored in `/var/lib/docker/` aren't automatically removed when you uninstall Docker. If you want to start with a clean installation, and prefer to clean up any existing data, read the [uninstall Docker Engine](https://docs.docker.com/engine/install/ubuntu/#uninstall-docker-engine) section.
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Before you install Docker Engine for the first time on a new host machine, you need to <mark>set up</mark> the Docker repository by adding <em>Docker's official GPG key</em>. Afterward, you can install and update Docker from the repository.</div>
</div>

1. Update the package database to get the latest versions of packages and their dependencies:
    
    ```bash
    sudo apt-get update
    ```
    
2. Install packages that allow `apt` to use a repository over HTTPS:
    
    ```bash
    sudo apt-get install ca-certificates curl gnupg
    ```
    
    * [`ca-certificates`](https://askubuntu.com/questions/857476/what-is-the-use-purpose-of-the-ca-certificates-package) package contains a set of Certificate Authority (CA) certificates. These are essential for verifying the authenticity of SSL/TLS certificates - in other words, they are used to establish the trustworthiness of secure websites and downloaded packages.
        
    * [`curl`](https://phoenixnap.com/kb/curl-command#:~:text=What%20Is%20the%20curl%20Command%3F): Curl is a command-line tool and library for transferring data with URLs. It's used for downloading files from the internet and can handle a variety of protocols, including HTTP, HTTPS, FTP, and more.
        
    * [`gnupg`](https://www.gnupg.org/): Stands for GNU Privacy Guard; it's a package for encryption and signing data. It's used here primarily for handling the GPG key related to Docker.
        
        ![](https://i.imgur.com/wGJ6LOF.png align="center")
        
3. Create a new directory named `/etc/apt/keyrings` with read, write, and execute permissions for the owner, and read and execute permissions for group members and other users.
    
    ```bash
    sudo install -m 0755 -d /etc/apt/keyrings
    ```
    
4. Download the GPG key for the Docker repository from its website and save it as a binary keyring file in `/etc/apt/keyrings`, which APT can then use to verify the authenticity of Docker packages downloaded from the repository:
    
    ```bash
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    ```
    
    * [`gpg --dearmor`](https://github.com/grafana/grafana/issues/72687#:~:text=gpg%20%2D%2Ddearmor%20creates%20the%20binary%20key.) is used to convert the armored GPG key (ASCII text format) into a binary format.
        
    * `-o /etc/apt/keyrings/docker.gpg` specifies the output file where the converted key should be saved.
        
    
    This is a security step: when APT is downloading packages from the Docker repository, it can use this GPG key to verify that the packages are authentic and haven't been tampered with. The GPG key mechanism explanation is provided in the next section.
    
5. Change the file permissions of the Docker GPG key to be readable by all users:
    
    ```bash
    sudo chmod a+r /etc/apt/keyrings/docker.gpg
    ```
    
6. Verify that you now have the key with the fingerprint. Run:
    
    ```bash
    sudo apt-key fingerprint
    ```
    
    ![](https://i.imgur.com/lCs53qh.png align="center")
    
7. Add the Docker Repository to APT sources:
    
    ```bash
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    ```
    
    The command above is used in Ubuntu-based systems to add Docker's official APT repository to the list of sources from which packages can be installed.
    
8. Update Package Database with Docker Packages.
    
    ```bash
    sudo apt update
    ```
    
9. Now, install the latest version of Docker CE (Community Edition) using:
    
    ```bash
    sudo apt install docker-ce
    ```
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">We can install Docker either directly from Docker's official repository (docker-ce) and get the latest version of Docker or from Ubuntu's repository (docker.io) maintained by the Ubuntu community which might not always be the latest version of Docker. In this hands-on, we download docker-ce.</div>
    </div>
    
10. Check if Docker is running:
    
    ```bash
    sudo systemctl status docker
    ```
    
    If Docker is running, you'll see an active (running) status in the output.
    
11. Add Your User to the Docker Group (Optional). By default, running the `docker` command requires administrator privileges. To run Docker commands as your non-root user, add your user to the `docker` group:
    
    ```bash
    sudo usermod -aG docker ${USER}
    ```
    
    You'll need to log out and back in for this to take effect.
    
12. Test Docker. Run the `hello-world` image to test if Docker is correctly installed:
    
    ```bash
    docker run hello-world
    ```
    
    ![](https://i.imgur.com/c1rjEfp.png align="center")
    
13. Configure Docker to Start on Boot (Optional). Enable Docker to start on boot with:
    
    ```bash
    sudo systemctl enable docker
    ```
    

### **How GPG Works in Package Verification**

1. **Key Pair**: GPG operates using a pair of keys:
    
    * **Private Key**: Known only to the key's owner, used for signing data.
        
    * **Public Key**: Distributed openly, used for verifying signatures.
        
2. **Signing the Package**:
    
    * The package maintainer (e.g., Docker) signs the package with their private GPG key before distributing it. This signature is a form of a digital fingerprint uniquely generated from the package content via a hashing algorithm.
        
    * The signing process doesn't encrypt the whole package; it just generates a signature based on the package's content.
        
3. **Distributing the Public Key**:
    
    * The maintainer also makes their public GPG key available, often on a website or in a public key server. This public key is what users will download and add to their keyring.
        
    * This public key can verify signatures made by the corresponding private key but can't be used to generate those signatures.
        
4. **User Downloads the Package**:
    
    * When a user downloads the package, they also receive the digital signature.
        
5. **Verification Process**:
    
    * On the user's side, the GPG tool uses the maintainer's public key to verify the signature attached to the package.
        
    * If the signature is valid, it ensures that the package hasn't been altered since it was signed - i.e., it's authentic and unmodified.
        

## References

1. [How to Install Docker on Ubuntu: A Step-By-Step Guide](https://www.youtube.com/watch?v=cqbh-RneBlk)
    
2. [Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository)
    
3. [What is PGP/GPG Encryption? In 3 Minutes - PGP/GPG Tutorial for Beginners](https://www.youtube.com/watch?v=1-MPcUHhXoc)
    
4. [Public and Private Keys - Signatures & Key Exchanges - Cryptography - Practical TLS](https://www.youtube.com/watch?v=_zyKvPvh808)
    
5. [Installing Docker on Ubuntu 20.04](https://www.youtube.com/watch?v=K03beiGemKQ)
