Introduction

Docker is a set of platform-as-a-service products used to launch and manage containers. Developers use Docker containers for developing and deploying applications because they provide isolated, lightweight, virtual environments.

In this tutorial, learn how to install Docker on Ubuntu 20.04. We also cover basic commands to get you started.

docker install tutorial for ubuntu 20.04

Prerequisites

  • Ubuntu 20.04 Installed on 64-bit operating system
  • A user account with sudo privileges
  • Command-line/terminal (Ctrl+Alt+T or Applications menu > Accessories > Terminal)
  • Docker software repositories (optional)

Install Docker on Ubuntu 20.04

There are two options when for installing Docker on your Ubuntu system:

  1. Installing using the official Docker repository
  2. Installing using the default repositories

When you download a package from the default Ubuntu repository, it may not be the latest version. If installing the latest (or a specific) version of Docker is important, use the official repository.

Option 1: Installing Docker from Official Repository

Step 1: Updating the Software Repository

Start by opening a terminal window and updating the local repository:

sudo apt update

Wait for the process to complete.

Step 2: Downloading Dependencies

Allow your Ubuntu 20.04 system to access the Docker repositories over HTTPS by running:

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

The above-mentioned command:

      • Gives the package manager permission to transfer files and data over https.
      • Allows the system to check security certificates.
      • Installs curl, a a tool for transferring data.
      • Adds scripts for managing software.

Step 3: Adding Docker’s GPG Key

Next, add the GPG key to ensure the authenticity of the software package:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –

Add the GPG key for Docker.

Step 4: Installing the Docker Repository

Now install the Docker repository using the command:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"

The command installs the latest repository for your specific Ubuntu release (in this case, 20.04 Focal Fossa).

Step 5: Installing the Latest Docker

Start by updating the repository again:

sudo apt update

Now you can install the latest Docker version with:

sudo apt-get install docker-ce

Step 6: Verifying Docker Installation

To confirm the installation check the version of Docker:

docker --version

docker version 19.03.8 installed sucessfully

It should show the Docker version, as in the image above.

Step 7: Enable Docker Service 

To start the Docker service run the following commands:

sudo systemctl start docker

Enable Docker to run at startup with:

sudo systemctl enable docker

To check the status of the service, use the command:

sudo systemctl status docker

The output should show Docker is active (running).

example of docker active and running on Ubuntu 20.04

Option 2: Installing Docker from Default Repositories

Step 1: Updating the Local Repository

Open a terminal window and update the local repository with:

sudo apt update

Step 2: Uninstalling Old Docker Versions

Before installing the software, make sure you remove any old Docker packages on your Ubuntu 20.04 by running the command:

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

Step 3: Installing Docker

Now let’s install Docker on Ubuntu 20.04. Run the following command in the terminal window:

sudo apt install docker.io

Type y and hit Enter to confirm the installation. Once the install is completed, the output notifies you Docker has been installed.

Step 4: Checking Docker Installation

To verify the installation check the Docker version:

docker --version

The output displays the Docker version, as in the image below.

example output of docker version installed on ubuntu 20.04

Step 5: Starting Docker Service 

Start the Docker service by running:

sudo systemctl start docker

Then, enable it to run at startup:

sudo systemctl enable docker

To check the status of the service, run:

sudo systemctl status docker

The output should verify Docker is active (running).

Checking Docker Service Status in ubuntu 20.04

Use Docker on Ubuntu 20.04

The basic syntax for docker commands is:

sudo docker [option] [command] [argument]

Run Docker Commands Without Sudo

By default, you can only use the docker commands with root privileges. Ubuntu requires the use of the sudo prefix. For example, if you try to run a hello-world container, the output displays permission was denied.

Running a Docker command without sudo in ubuntu 20.04

It is advisable to keep the settings as is. However, you can bypass typing sudo every time. Adding the user to the docker group grants privileges equivalent to root.

1. First, create the docker group with the command:

sudo groupadd docker

2. Then, type the following command (making sure to replace [user] with your username):

sudo usermod -aG docker [user]

3. Enable the new settings with:

su - [user]

4. Lastly, check to confirm the user is now a part of the docker group by running:

id -nG

Add user to docker group to run the docker commands without the sudo prefix

5. Now you can run the docker run hello-world command without the sudo prefix.

Run hello-world container in Docker without sudo

Working With Docker Images

Docker images are files that contain the source code, libraries, dependencies, tools, and other files a container needs. You can create Docker images with Dockerfiles or use existing ones available on Docker Hub.

To download a new Docker image, use the command:

docker pull [image_name]

If you don’t know the exact name of the image, search for it in Docker’s repository with:

docker search ubuntu

After working with Docker for some time, you will collect a local registry of images. Display a list of all Docker images on the system with:

docker images

List available Docker images on Ubuntu.


Note: To learn more about managing images, refer to a detailed list of Docker image commands.


Working With Docker Containers

Docker containers are isolated virtual environments that run based on the Docker image assigned to them.

To run a container based on an existing Docker image, use the command:

docker run [image_name]

Using the command above runs a container but doesn’t move you inside of it. To run a container in interactive mode and change to the container command prompt, run:

docker run -it [image_name]

Note: Learn how to run a container in How to Use Docker Run Command With Examples.


Another useful docker command is listing all the containers on the system. To list all active containers, type:

docker container ps

To view all containers (active and inactive), run:

docker container ps -a

example list all Docker containers


Note: Check out more container practices in How to Manage Docker Containers.


Working With Docker Volumes

The best way to preserve data generated within a container is mounting Docker volumes on to them. Mounted volumes don’t depend on the container life cycle and can share data between containers.

Create a new Docker volume with:

docker volume create [volume_name]

To create a container and mount a volume to it, follow the syntax:

docker run --mount source=[volume_name],destination=[path_in_container] [docker_image]

For more information about Docker volumes and how to share between containers, refer to Docker Volumes: How To Create & Get Started.

Conclusion

This article should have helped you install and get started with Docker on Ubuntu 20.04.

Be sure to read our other Docker tutorials.


Next you should also read