How to install and use Docker on Ubuntu 20.04?

Overview

Docker allows users to package and run applications inside of containers. Both containers and virtual machines are isolated but, containers are more portable and resource friendly. Numerous containers can run at once on a single host due to the isolation.

In order to manage the containers, Docker uses a client-server architecture and the Docker daemon.

You will discover how to set up and utilize Docker on Ubuntu 20.04 in this article.

Prerequisites

There are certain prerequisites that need to be met before you begin:

  • Ubuntu 20.04 installed, or an Ubuntu 20.04 server set up.

  • User account with admin privileges.

  • Working knowledge of the Linux command line.

Get Started

For administering Docker or Kubernetes containers, Ubuntu is the best platform. This is because Ubuntu powers millions of machines worldwide, is swift, robust, and open-source, and runs the containers at scale.

This tutorial will cover docker installation using the official docker repository. (You may also use the default ubuntu repositories for installation but that doesn't ensure installation of the latest docker version)

Follow the steps mentioned below to install Docker on Ubuntu 20.04 :

1. Uninstall Older Versions

Uninstall all pre-existing older versions of Docker before proceeding with the installation of the latest version of Docker.

Use the following command to remove the older versions :

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

2. Updating the Package Repository

Use the following command to update the existing list of packages :

sudo apt update

Enter the root user password when prompted to do so.

3. Installing Prerequisite Packages

Install some prerequisite packages that enable apt to use packages over HTTPS.

Use the following command to install the prerequisite packages :

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

4. Adding the GPG Key

The legitimacy of a software package is confirmed by a GPG key.

Use the following command to add the docker repository GPG key :

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

The output should display OK, verifying its legitimacy.

5. Adding the Docker Repository

Use the following command to add the Docker repository to apt sources :

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

This command will update our package database with the latest docker packages.

6. Establish Installation Source

Use the following command to establish that the Docker installation source is the Docker repository and not the Ubuntu repository :

apt-cache policy docker-ce

You will see an output similar to this :

7. Install Docker

Use the following command to install Docker:

sudo apt install docker-ce -y

Wait for the installation to finish.

8. Check Docker Status

Use the following command to check Docker status:

sudo systemctl status docker

The output will display that the Docker daemon is up and running.

Using Docker on Ubuntu

Run the docker command in the terminal to access all Docker information, including syntax, options, and commands.

docker

Docker Images

The foundation for creating Docker containers are Docker images. The images can be found on Docker Hub, a repository for Docker files. All users are able to host their images on Docker Hub thanks to the repository, which provides a large selection of images, including Linux distributions and apps.

The sections below demonstrate various methods of using Docker images.

Searching Docker Images

Use the docker search command to search for images accessible on Docker Hub.

sudo docker search [image name or keyword]

Specify the Image Name or Keyword you want to search for. For example, to search for all Node JS images, run :

sudo docker search nodejs

The output of this command is a list of all images containing the nodejs keyword. If the official column has the OK parameter it indicates that the image was uploaded by the official company that created the project.

Pulling a Docker Image

Choose the image you want, then use the pull option to download it. The syntax is :

sudo docker pull [Image Name]

For instance, pull the official python image by running :

sudo docker pull python

Use the image to run a container once it has been downloaded. If you try to run a container from an image that has not been downloaded yet, docker will download the image first before running the container.

Browse Downloaded Images

Use the following command to see a list of previously downloaded images :

sudo docker images

Docker Containers

A Docker container is a virtual environment that is built from a Docker image. To automatically download an image and construct a container, use an image you have already downloaded or specify it's name in the docker run command.

sudo docker run [Image Name]

For example download a test image and start up a container using the python image.

sudo docker run python

This will create a container from the mentioned image but it will run in non-interactive mode, utilize the -i and -t switches to get shell access to the container.

sudo docker run -it python

After running the container in interactive mode you can run the commands as usual, you do not need to use sudo as the shell has root access.

Use the exit command to exit the container.

Managing Containers

Use the following command to view the list of all active containers :

sudo docker ps

To view the list of all active and inactive containers use the -a flag with the previous command.

sudo docker ps -a

Use the following command to start a stopped container :

sudo docker start [container-ID or container-name]

You can either use the container ID or the container name to specify the container you wish to start.

For example :

sudo docker start 2ad2b380ae3c
 
or
 
sudo docker start eloquent_bohr

Use the following command to stop a running container :

sudo docker stop [container-ID or container-name]

You can either use the container ID or the container name to specify the container you wish to stop.

For example :

sudo docker stop 2ad2b380ae3c
 
or
 
sudo docker stop eloquent_bohr

Use the following command to remove any irrelevant containers using the following command :

sudo docker rm [container ID or container name]

You can use either the container ID or the conatiner name to specify the container you wish to remove.

For example :

sudo docker rm 2ad2b380ae3c
 
or
 
sudo docker rm eloquent_bohr

Note: Use the --rm switch while creating a container to automatically remove it when stopped.

Docker Volumes

Volumes are the ideal method to store data generated and used by containers. Volumes are entirely managed by docker and stored on the host, they are not dependent on the container.

Use the following command to create a docker volume:

sudo docker volume create [volume name]

You can assign any unique name you want to the volume.

For example:

sudo docker volume create lbh

Use the following command to remove a docker volume:

sudo docker volume rm [volume name]

Specify the name of the volume you wish to remove.

For example:

sudo docker volume rm lbh

Use the following command to uninstall Docker Engine, CLI, containers and Docker compose packages:

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

Use the following command to remove all Docker images, containers, volumes, or custom configuration files:

sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

Conclusion

This tutorial showed how to install docker on ubuntu 20.04 and use it to download images, create containers and create volumes.

Last updated