Docker is a powerful platform that allows developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment. Installing Docker on Ubuntu enables you to deploy and manage containerized applications efficiently.

Prerequisites

Before installing Docker on Ubuntu, ensure that you have:

  • An Ubuntu system running version 20.04 or newer.
  • Sudo privileges to install packages.
  • Access to a terminal or command line.

Step 1: Uninstall Old Versions

It's important to remove any older installations of Docker to prevent conflicts. To uninstall old versions, run:


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

This command removes any previous Docker installations, but it does not delete images, containers, volumes, or custom configuration files.

Step 2: Update Package Index

Update your existing list of packages to ensure you get the latest version available:


sudo apt-get update
    

Step 3: Install Prerequisite Packages

Install packages that allow apt to use packages over HTTPS:


sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    

These packages ensure that package transfers are secure and the correct versions are downloaded.

Step 4: Add Docker’s Official GPG Key

Adding Docker’s GPG key verifies the authenticity of the packages. Run the following command:


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

This command downloads Docker's official GPG key and saves it for use in the next step.

Step 5: Set Up the Docker Repository

Add the Docker repository to APT sources by running:


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
    

This command adds the Docker repository to your system so that you can install Docker from it.

Step 6: Update the Package Index Again

After adding the Docker repository, update the package index:


sudo apt-get update
    

This ensures that apt knows about the Docker packages available in the newly added repository.

Step 7: Install Docker Engine

Install the latest version of Docker Engine and containerd using the following command:


sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    

This command installs Docker Engine and related components required to run Docker.

Step 8: Verify the Docker Installation

To verify that Docker Engine is installed correctly, run the hello-world image:


sudo docker run hello-world
    

This command downloads a test image and runs it in a container. If the installation is successful, you will see a message indicating that Docker is working correctly.

Optional: Manage Docker as a Non-Root User

By default, the docker command can only be run by the root user or by a user in the docker group. To avoid using sudo with every Docker command, add your user to the docker group:


sudo usermod -aG docker ${USER}
    

To apply the new group membership, log out and log back in. Verify that your user is now added to the docker group:


groups
    

You should see docker in the list of groups.

Basic Usage of Docker

Now that Docker is installed, you can start using it to run containers. For example, to run an Ubuntu container in interactive mode:


docker run -it ubuntu /bin/bash
    

This command pulls the Ubuntu image (if not already downloaded) and starts a bash shell inside a new container.

Inside the container, you can run commands as you would on any Ubuntu system. To exit the container, type exit.

Managing Docker Containers

You can list running containers using:


docker ps
    

To list all containers (running and exited):


docker ps -a
    

To stop a running container:


docker stop [container_id]
    

Replace [container_id] with the actual container ID obtained from docker ps.

To remove an inactive container:


docker rm [container_id]
    

By following these steps, you have installed Docker on your Ubuntu system and learned the basics of managing Docker containers. You can now leverage Docker to containerize your applications and simplify your development workflow.