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

How to Install and Use Docker on Ubuntu 20.04 shell 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

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Install Docker from the Local Repository

Next, install Docker by running the following apt command.

sudo apt install docker.io

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Check the Docker Version

Check the Docker version with the following command.

docker --version

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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 -

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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"

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

And update your system again.

sudo apt update

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Install Docker

Finally, install Docker using the following command.

sudo apt-get install docker-ce

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Check the Docker Version

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

docker --version

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Verify that the Docker service has started by its status.

sudo systemctl status docker

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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

sudo systemctl stop docker

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Uninstall Docker On Ubuntu

You can remove Docker from your system with the following commands

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

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

sudo apt-get remove docker.ce

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Use Docker in Ubuntu

Run a Container in Docker

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

sudo docker run 

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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 

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Run the following command for changes to take effect.

su - 

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

And verify the changes.

id -nG

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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

docker run hello-world

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

Search for Images in Docker

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

docker search 

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

List All Docker Images in Docker

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

docker images

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

List All Container in Docker

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

docker container ps -a

How to Install and Use Docker on Ubuntu 20.04 shell ubuntu

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.