How to Install and Use Docker on Ubuntu 20.04

Docker application is a virtualization platform. It allows users to manage applications and images in containers. These containers are portable and lightweight for application deployment. As a Linux user, you can easily install and use Docker on Ubuntu. 

Docker virtualization simplifies the process of application management. The applications run in isolated containers similar to virtual machines. However, these containers are not resource-intensive compared to virtual machines and are independent of the operating system. 

Docker packs all of the application dependencies inside a Linux container. Docker is widely used by system administrators and DevOps experts to automate application deployment. It is also used to boost employee productivity and manage resources productively. 

In this article, we will cover how to install and use Docker on Ubuntu 20.04. So let’s dive in!

Prerequisites

For this tutorial, you will need a Ubuntu 20.04 machine with sudo privilege. You will additionally need an account on Dockers website to create your images and push them. Make sure to create your account from the Dockers website.

Install and Use Docker on Ubuntu

1. Install Docker on Ubuntu

To start with the Docker installation, first, update the existing list of packages in your Ubuntu machine.

sudo apt update
Install and Use Docker on Ubuntu

Secondly, add a few prerequisite packages required for the Docker installation.

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

Next, generate a GNU Privacy Guard (GNG) key for the Docker repository that you will install in the next step. For example:

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

In the next step, add the Docker repository to the APT package manager. We are performing this because existing Docker installation packages in the APT repository might not be updated to the latest version. Therefore, we will add the latest Docker repository. 

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Install and Use Docker on Ubuntu

Now, install the Docker from the Docker repository.

sudo apt-cache policy docker-ce

You’ll see an output like this.

Install and Use Docker on Ubuntu

Alternatively, you can also install Docker from APT directly.

sudo apt install docker-ce

After the installation, the daemon starts running automatically. To ensure that Docker has been installed correctly, run the following command to check the Docker version:

docker –version

To check its status, run the systemctl command

sudo systemctl status docker

You will see a similar output. 

Install and Use Docker on Ubuntu

However, if the service has not started, you can start the following command:

sudo systemctl start docker

Also, make sure to enable Docker so that it starts running at the machine startup. For example:

sudo systemctl enable docker

By following these steps, you have installed the Docker service. However, we still have to install the Docker CLI, also known as Docker Client. 

2. Use Docker on Ubuntu

The Docker command consists of options, commands, and arguments. The syntax of the command looks like this:

docker [option] [command] [arguments]

To view all the available options and subcommands, type the keyword docker in the terminal. For example: 

docker

You will get a list of commands and arguments available with this command. 

Use Docker on Ubuntu

To view the specific options for the commands, type:

docker docker-subcommand --help

You will get a similar output:

To get system-related information on Docker, use the following command:

docker info

3. Use Docker Images from Docker

Since Docker containers are built from Docker images, Docker pulls these images from Docker Hub. A Docker Hub is a registry managed by the Docker company itself. To check the access to Docker Hub and execute an image, type the following command:

docker run hello-world

If you get a similar output, the image is working correctly. 

Use Docker Images from Docker

The Docker application downloads the image from Docker Hub first. Then, it created a container from the downloaded image and executed the application within its container. To search for the images available on the Docker Hub, use the following command:

docker search image_name

For example:

docker search ubuntu

You will get a similar output. The list of available images with the name “ubuntu” is listed in the output. 

Use Docker Images from Docker

Once you have found your image, you can download it by executing the following command:

docker pull ubuntu

Secondly, confirm that the downloaded image exists by running the following command:

docker images

If you see a similar output, the image has downloaded successfully. Now, we will try to run it in the next step.

4. Run a Docker Container

The Docker Container runs similar to the hello-world that we executed before. However, some containers are interactive and may require interactive access. The same is the case with the Ubuntu image that we have downloaded. 

To access the Ubuntu image, type the following command:

docker run -it ubuntu

Your command line will look something like this now:

Run a Docker Container

Now, you can run any Ubuntu command inside this Docker image. For example, run the following command to update Ubuntu packages and install Node.js:

apt update
apt install nodejs
Run a Docker Container

This will install Node.js from the official Ubuntu repository. You can check its version using the following command:

node -v

To exit from the interactive container, type exit. 

To check the currently running Docker images, you can type the following command: 

sudo docker ps

This command will only show the active containers. To view both the active and inactive containers, execute the following command:

sudo docker ps -a

To view the recent container, type:

docker ps -l

5. Uninstall Docker from Ubuntu

Before uninstalling Docker, it is crucial to remove all the existing containers and running images. To perform this step, run the following commands: 

docker container stop $(docker container ls -aq)

You will have to stop all the existing containers in your machine individually. 

docker system prune -a --volumes

Now that you have stopped all the containers, you can uninstall using the apt command. For example: 

sudo apt purge docker-ce
sudo apt autoremove

In this article, you have learned how to install and use Docker on Ubuntu machines. The article covers basic Docker installation, but there is more. You can also manage containers in Docker, create your own Docker images, and push those images to Docker Hub. We hope this article has helped you.

If this guide helped you, please share it.

Leave a Reply
Related Posts