How to Install and Use Docker on Ubuntu 20.04

Docker is a compact virtualization that runs on top of the operating system, allowing users to design, run, and deploy applications encased in small containers. It is a collection of platform-as-a-service (PaaS) tools for launching and managing containers. Docker containers are used by developers to develop and deploy apps because they are isolated and lightweight.

Docker has transformed the software engineering business, changing not just how we deliver and deploy applications but also how engineers build-up application development environments on their workstations.

Linux containers are robust, scalable, and secure. A Docker container’s processes are always insulated from the host system, avoiding manipulation from the outside.

In this tutorial, I will show you how to install, use, and remove Docker on an Ubuntu Linux system.

Prerequisites

  • Ubuntu or any other Debian-based distribution
  • Terminal access
  • Sudo or root privileges
  • Internet access

Note: Although the commands used in this tutorial are specifically for the Ubuntu system, all the methods are also valid for any other Linux-based system.

Install Docker From the System Repository

Docker is included and comes by default with the Ubuntu system. Install the Docker through the following steps.

Update Your System

Always update your system repositories before any installation.

sudo apt update

Update Ubuntu

Remove Any Previous Docker Installations

Remove any older version of Docker using the following command for a fresh installation.

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

Remove old Docker installations first

Install Docker from the Local Repository

Next, install Docker by running the following apt command.

sudo apt install docker.io

Install Docker

Check the Docker Version

Check the Docker version with the following command.

docker --version

Check Docker Version

You can see the version is not the latest available version, you have to install it from its official repository to get the latest available version.

Install Docker From the Docker Official Repository

Update Your System

Update the system repositories by running the following command.

sudo apt update

Update packages

Install Dependencies

Install the dependency packages to access the Docker repository over HTTPS.

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

Install Docker dependencies

Add the GPG Key

To add the GPG key of the Docker repository, run the following command.

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

Add Docker GPG Key

Install the Docker Repository

Next, to install the Docker repository, run.

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

Add Docker repository

And update your system again.

sudo apt update

Update repo list

Install Docker

Finally, install Docker using the following command.

sudo apt-get install docker-ce

Install Docker Community Edition

Check the Docker Version

To verify that you have the latest available version of Docker on your system, run the following command.

docker --version

Check Docker version

Start and Enable the Docker Service

You can start and enable Docker services using the following commands.

sudo systemctl start docker
sudo systemctl enable docker

Start Docker

Verify that the Docker service has started by its status.

sudo systemctl status docker

Check Docker Status

You can see that the Docker service is running.

Stop and Disable the Docker Service

Similarly, you can run the systemctl commands to stop and disable the Docker services.

sudo systemctl disable docker

Disable Docker service

Disabling the services will make sure that Docker services will not automatically start at system boot.

sudo systemctl stop docker

Stop Docker service

Uninstall Docker On Ubuntu

You can remove Docker from your system with the following commands

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

Remove Docker

sudo apt-get remove docker.ce

Remove Docker CE

Use Docker in Ubuntu

Run a Container in Docker

To run a container in Docker, use the following command.

sudo docker run <container>

Run Docker Container

You can see that sudo or root privilege is required to run Docker. To opt-out of this, you have to add the docker group to sudo and then the user in the docker group. To do that, run the following commands.

sudo groupadd docker
sudo usermod -aG docker <user>

Add user to Docker group

Run the following command for changes to take effect.

su - <user>

Su user

And verify the changes.

id -nG

Check Linux group memberships

You can see the docker group in the output. Now you can run Docker commands without sudo.

docker run hello-world

Run Docker container

Search for Images in Docker

To search for a specific Docker image, you can search with the image name in Docker.

docker search <image-name>

Search for Docker images

List All Docker Images in Docker

Or you can list all the images with the following command.

docker images

List Docker Images

List All Container in Docker

Similarly, you can list all the containers in Docker with the following command.

docker container ps -a

get a List of all Docker Images

Conclusion

Docker is an extremely flexible technology with various applications in software development. Docker will ease the way you distribute software in diverse settings and is excellent for testing and prototyping applications, whether you are a software developer or work in DevOps.

This tutorial discussed how you can install and uninstall dockers on your Ubuntu system. It also briefly teaches some basic use of Docker.