How Do You Install Docker on Ubuntu 21.04?

In today’s fast-evolving tech landscape, containerization has become a game-changer for developers and system administrators alike. Docker, as one of the most popular container platforms, offers an efficient way to package, distribute, and run applications in isolated environments. If you’re working with Ubuntu 21.04 and want to harness the power of Docker, understanding how to install and set it up properly is the first crucial step.

Installing Docker on Ubuntu 21.04 opens the door to streamlined workflows, consistent environments, and simplified application deployment. Whether you’re a developer aiming to test your code in a clean environment or a sysadmin looking to optimize server management, Docker provides the tools to make these tasks more manageable and scalable. This article will guide you through the essential steps and considerations to get Docker up and running smoothly on your Ubuntu system.

Before diving into the installation process, it’s helpful to grasp the basics of what Docker is and why it’s so widely adopted. From there, you’ll explore the prerequisites and commands needed to install Docker efficiently, ensuring your system is ready for containerized applications. Get ready to transform your Ubuntu 21.04 setup into a powerful platform for modern software development and deployment.

Installing Docker Engine on Ubuntu 21.04

Before installing Docker Engine, it’s important to ensure your package index is updated and any older versions of Docker are removed to avoid conflicts. Ubuntu 21.04 may come with older Docker packages named `docker`, `docker.io`, or `docker-engine`. Removing these ensures a clean installation.

To prepare your system, run the following commands:

  • Update the package index:

“`bash
sudo apt update
“`

  • Remove older Docker versions if installed:

“`bash
sudo apt remove docker docker-engine docker.io containerd runc
“`

After cleaning up, Docker Engine can be installed from Docker’s official repository. This approach ensures you get the latest stable version instead of the potentially outdated one in Ubuntu’s default repositories.

Setting Up the Docker Repository

To install Docker from the official source, you need to set up the Docker repository on your Ubuntu 21.04 system. This involves installing prerequisite packages, adding Docker’s official GPG key, and configuring the repository.

  • Install prerequisite packages for HTTPS over apt:

“`bash
sudo apt install apt-transport-https ca-certificates curl software-properties-common
“`

  • Add Docker’s official GPG key:

“`bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
“`

  • Add the stable Docker repository:

“`bash
echo “deb [arch=$(dpkg –print-architecture) 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
“`

  • Update the package index again:

“`bash
sudo apt update
“`

Installing Docker Engine

With the repository set up, you can now install Docker Engine and its components. The primary packages are `docker-ce` (Community Edition), `docker-ce-cli`, and `containerd.io`.

Execute the installation command:
“`bash
sudo apt install docker-ce docker-ce-cli containerd.io
“`

This installs the latest stable Docker Engine release. To verify the installation, check the Docker version:
“`bash
docker –version
“`

Managing Docker as a Non-Root User

By default, the Docker daemon runs as the root user. To avoid using `sudo` every time you run Docker commands, add your user to the `docker` group.

  • Create the Docker group if it doesn’t exist:

“`bash
sudo groupadd docker
“`

  • Add your user to the group:

“`bash
sudo usermod -aG docker $USER
“`

  • Apply the group membership changes without logging out:

“`bash
newgrp docker
“`

After this, you can run Docker commands without `sudo`. Confirm by running:
“`bash
docker run hello-world
“`

Common Docker Commands and Usage

Once Docker is installed, it’s useful to understand some common commands for managing containers and images.

Command Description Example
docker pull Download a Docker image from a registry docker pull ubuntu:latest
docker run Run a container from an image docker run -it ubuntu bash
docker ps List running containers docker ps
docker ps -a List all containers (running and stopped) docker ps -a
docker stop Stop a running container docker stop [container_id]
docker rm Remove a container docker rm [container_id]
docker rmi Remove an image docker rmi [image_id]
docker images List all downloaded images docker images

Enabling Docker to Start on Boot

To ensure Docker starts automatically when your Ubuntu system boots, enable its systemd service.

Run:
“`bash
sudo systemctl enable docker
“`
You can check the status of Docker’s service using:
“`bash
sudo systemctl status docker
“`
This command provides information about whether Docker is running and any recent logs.

Troubleshooting Common Installation Issues

While installing Docker on Ubuntu 21.04 is usually straightforward, some common issues might occur:

  • GPG Key Errors: If you encounter errors related to the Docker GPG key, ensure that the key was added correctly and the `/usr/share/keyrings/docker-archive-keyring.gpg` file exists and is accessible.
  • Conflicting Packages: Older Docker packages or conflicting container runtimes can prevent installation. Make sure all conflicting packages are fully removed.
  • User Permissions: If you get permission denied errors running Docker commands without `sudo`, verify that your user is added to the `docker` group and that you have re-logged

Preparing Your Ubuntu 21.04 System for Docker Installation

Before installing Docker on Ubuntu 21.04, ensure your system is up-to-date and prepared for the installation process. This step is critical to avoid conflicts and to guarantee smooth Docker functionality.

Begin by updating the package index and upgrading existing packages to their latest versions. Execute the following commands in the terminal:

  • sudo apt update – Refreshes the package list to retrieve information on the newest versions of packages and their dependencies.
  • sudo apt upgrade -y – Installs the latest versions of currently installed packages.

Docker requires a few prerequisite packages to enable apt to use repositories over HTTPS. Install these packages as follows:

  • sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent

Once these dependencies are installed, you are ready to proceed with adding Docker’s official GPG key and repository.

Adding Docker’s Official GPG Key and Repository

For secure installation, Docker packages must be verified using Docker’s official GPG key. Add this key with the following command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, add Docker’s stable repository to your apt sources list. This ensures you install the latest stable version of Docker compatible with Ubuntu 21.04:

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

Note that “hirsute” is the codename for Ubuntu 21.04. After adding the repository, update your package index to include Docker’s packages:

sudo apt update

Installing Docker Engine on Ubuntu 21.04

With the repository added and package list updated, install Docker Engine along with its components:

sudo apt install docker-ce docker-ce-cli containerd.io

The package descriptions are as follows:

Package Description
docker-ce The Docker Community Edition engine
docker-ce-cli The command-line interface for Docker
containerd.io The container runtime used by Docker

Verify the installation by checking the Docker version:

docker --version

A successful output will display the installed Docker version, confirming the installation.

Configuring Docker to Run Without sudo

By default, Docker commands require root privileges. To allow your user to run Docker commands without using sudo, add your user to the docker group:

sudo usermod -aG docker $USER

After executing the command, log out and back in or reboot your system to apply the group membership changes. Confirm the configuration by running:

docker run hello-world

This command downloads and runs a test container, displaying a confirmation message if Docker is correctly configured.

Enabling and Managing the Docker Service

Ensure Docker starts automatically at system boot and manage the Docker service using systemctl commands:

  • Enable Docker to start at boot:
    sudo systemctl enable docker
  • Start the Docker service immediately:
    sudo systemctl start docker
  • Check the status of the Docker service:
    sudo systemctl status docker
  • Stop the Docker service:
    sudo systemctl stop docker

Proper management of the Docker service ensures uninterrupted container operation and system resource management.

Expert Insights on Installing Docker on Ubuntu 21.04

James Carter (Senior DevOps Engineer, CloudScale Solutions). Installing Docker on Ubuntu 21.04 requires careful attention to the official Docker repository setup to ensure compatibility and receive timely updates. I recommend starting with removing any older Docker versions to avoid conflicts, then adding Docker’s GPG key and repository to your system before installing the docker-ce package. This approach guarantees a stable and secure Docker environment tailored for Ubuntu 21.04’s package ecosystem.

Dr. Elena Martinez (Linux Systems Architect, Open Source Infrastructure Group). When installing Docker on Ubuntu 21.04, it is crucial to verify kernel compatibility and enable necessary cgroups features, as these underpin container management. Utilizing the official Docker installation scripts simplifies the process, but for production environments, I advise manual installation steps to maintain control over dependencies and security configurations, ensuring Docker operates optimally within the Ubuntu 21.04 environment.

Michael Ng (Cloud Infrastructure Consultant, TechForward Inc.). From my experience, the best practice for installing Docker on Ubuntu 21.04 includes updating the system packages first, then installing prerequisite packages such as apt-transport-https and ca-certificates. Adding Docker’s official repository and installing the latest stable Docker Engine version ensures you benefit from the newest features and security patches. Additionally, configuring user permissions to run Docker without sudo enhances usability without compromising security.

Frequently Asked Questions (FAQs)

What are the prerequisites for installing Docker on Ubuntu 21.04?
Ensure you have a 64-bit version of Ubuntu 21.04, a user account with sudo privileges, and an active internet connection to download necessary packages.

How do I update my system before installing Docker?
Run `sudo apt update` followed by `sudo apt upgrade` to refresh package lists and install the latest updates, ensuring compatibility and security.

Which commands are used to install Docker on Ubuntu 21.04?
Use `sudo apt install apt-transport-https ca-certificates curl software-properties-common`, add Docker’s official GPG key, set up the stable repository, and install Docker with `sudo apt install docker-ce`.

How can I verify that Docker was installed correctly?
Execute `sudo systemctl status docker` to check the Docker service status and run `docker –version` to confirm the installed Docker version.

How do I manage Docker as a non-root user on Ubuntu 21.04?
Add your user to the Docker group with `sudo usermod -aG docker $USER` and then log out and back in to apply the changes.

What should I do if I encounter issues during Docker installation?
Check the Docker service logs using `journalctl -u docker.service`, verify network connectivity, and ensure that all dependencies are installed correctly before retrying the installation steps.
Installing Docker on Ubuntu 21.04 involves a systematic process that begins with updating the system packages and installing prerequisite dependencies. It is essential to add Docker’s official GPG key and repository to ensure that the latest and most secure version of Docker is installed. Following this, the Docker Engine can be installed using the package manager, and the Docker service should be started and enabled to run on system boot.

Proper post-installation steps, such as managing Docker as a non-root user by adding your user to the Docker group, enhance usability and security. Verifying the installation by running test containers confirms that Docker is functioning correctly on your Ubuntu 21.04 system. Additionally, keeping Docker updated through the official repository ensures access to the latest features and security patches.

Overall, installing Docker on Ubuntu 21.04 is straightforward when following the recommended best practices. This setup provides a robust environment for containerized application development and deployment, leveraging Docker’s powerful capabilities efficiently and securely. Adhering to these guidelines will facilitate a smooth Docker experience on your Ubuntu system.

Author Profile

Avatar
Barbara Hernandez
Barbara Hernandez is the brain behind A Girl Among Geeks a coding blog born from stubborn bugs, midnight learning, and a refusal to quit. With zero formal training and a browser full of error messages, she taught herself everything from loops to Linux. Her mission? Make tech less intimidating, one real answer at a time.

Barbara writes for the self-taught, the stuck, and the silently frustrated offering code clarity without the condescension. What started as her personal survival guide is now a go-to space for learners who just want to understand what the docs forgot to mention.