DEV Community

Evelyn Davis
Evelyn Davis

Posted on

Installing Docker on Ubuntu 20.04

Image description

Installing Docker on Ubuntu 20.04 is a straightforward process that involves several steps, including setting up the repository, installing Docker, and configuring it for your needs. Docker is a popular platform for developing, shipping, and running applications inside containers. This guide will walk you through each step in detail, ensuring you have Docker up and running on your Ubuntu 20.04 system.

1. Update Your System

Before installing Docker on Ubuntu , it is essential to update your system's package index to ensure that all software is up-to-date. This can be done using the following commands:

bash
sudo apt update
sudo apt upgrade
The update command refreshes the list of available packages, while the upgrade command upgrades the installed packages to the latest versions.

2. Install Prerequisites

Docker requires some prerequisite packages to be installed on your system. These packages ensure that Docker can be installed and run correctly. Install these prerequisites by running:

bash
sudo apt install apt-transport-https ca-certificates curl software-properties-common
apt-transport-https: Allows the package manager to transfer data securely using HTTPS.
ca-certificates: Ensures that your system can verify the authenticity of SSL certificates.
curl: A command-line tool for transferring data using various protocols, including HTTP and HTTPS.
software-properties-common: Adds the add-apt-repository command, which is used to add the Docker repository.

3. Add Docker’s Official GPG Key

Before adding the Docker repository to your Ubuntu system, you must add Docker's official GPG key. This key is used to verify the authenticity of the packages provided by Docker. Run the following command:

bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
This command downloads the GPG key from Docker’s official repository and saves it to your system.

4. Set Up the Docker Repository

With the GPG key added, you can now add the Docker repository to your system. This repository contains the Docker packages you need to install. Use the following command to add the Docker repository:

bashecho "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
This command adds the Docker repository to your system’s list of software sources.

5. Install Docker

Now that the Docker repository has been added, update the package index again to include the packages from the newly added Docker repository:

bash
sudo apt update
After updating the package index, you can install Docker using the following command:

bash
sudo apt install docker-ce docker-ce-cli containerd.io
docker-ce: The Docker Community Edition, which includes the Docker engine.
docker-ce-cli: The command-line interface for Docker.
containerd.io: A runtime that manages containers in a lightweight manner.
During the installation, the necessary dependencies will be installed automatically.

6. Verify the Docker Installation

After the installation is complete, you can verify that Docker is installed and running correctly by checking the version of Docker:

bash
docker --version
This command should output the version of Docker that was installed, confirming that Docker is installed correctly.

7. Manage Docker as a Non-Root User

By default, Docker requires root privileges to run. This means you need to use sudo with every Docker command, which can be cumbersome. To avoid this, you can add your user to the docker group, allowing you to run Docker commands without sudo:

bash
sudo usermod -aG docker $USER
After adding your user to the docker group, log out and log back in to apply the changes. You can verify that you can run Docker commands without sudo by running:

bash
docker run hello-world
This command downloads a test image and runs it in a container. If everything is set up correctly, you should see a message indicating that Docker is working correctly.

8. Configure Docker to Start on Boot

To ensure that Docker starts automatically whenever your system boots, you can enable the Docker service using the following command:

bash
sudo systemctl enable docker
This command enables the Docker service, ensuring it starts automatically when your system boots.

9. Docker Compose Installation

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. If you need Docker Compose, you can install it by following these steps:

First, download the latest version of Docker Compose from the official GitHub repository:

bash
sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')" -o /usr/local/bin/docker-compose
Next, make the Docker Compose binary executable:

bash
sudo chmod +x /usr/local/bin/docker-compose
Finally, verify that Docker Compose is installed correctly by checking the version:

bash
docker-compose --version
This command should display the version of Docker Compose that was installed.

10. Uninstall Docker (If Needed)

If you ever need to uninstall Docker, you can remove the Docker packages using the following command:

bash
sudo apt purge docker-ce docker-ce-cli containerd.io
After running this command, remove any residual files by executing:

bash
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
These commands remove Docker and its associated files from your system.

11. Troubleshooting Tips

Docker Daemon Fails to Start: If the Docker daemon fails to start, check the status using sudo systemctl status docker. Reviewing the output can provide clues to resolve the issue.
Permission Denied: If you receive a “permission denied” error when running Docker commands, ensure that your user is added to the docker group, and you’ve logged out and back in to apply the changes.
Outdated Docker Version: If you encounter issues with outdated Docker packages, ensure you’re using the official Docker repository, as described in Step 4.

Conclusion

By following the steps outlined in this guide, you should have Docker installed and configured on your Ubuntu 20.04 system. Docker is a powerful tool that allows you to package and run applications in isolated containers, making it easier to develop, deploy, and scale your applications. With Docker up and running, you can now explore containerization and start building, shipping, and running your applications in a more efficient and reliable manner.

Top comments (0)