How to Install Docker on Linux

Docker is a tool designed to build, deploy, and run applications using containers.

It is an open source lightweight virtualization tool that uses OS-level virtualization.

It allows you to run an application with all its dependencies within software containers by sharing the host operating system kernel.

It uses resource isolation features from the Linux kernel such as cgroups, namespaces and aufs (advanced multi layered unification filesystem).

This allows the host operating system to run multiple containers and does not require guest operating systems unlike a virtual machine. (Docker is completely different from the virtual machine).

Docker containers run in any system, any infrastructure and any cloud because they are not bonded to any specific infrastructure.

Containers run on the host operating system and share the same kernel, so they start immediately and use RAM more efficiently.

Images are constructed from layered file systems, so they can share common files, disk usage and image downloads are very efficient.

Docker is designed to run on 64-bit architecture with Linux kernel 3.10 and later.

How Does the Docker Engine Work?

Docker Engine is a client-server application that builds and runs containers using Docker’s components and services.

For instance, when you run “docker run”, the client sends these commands to “dockerd”. The Docker daemon listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.

The major Docker components are listed below

The Docker engine consists of below components:

  • Docker daemon dockerd: It is a service that runs on your host operating system. It listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.
  • Docker client docker: It is a command line tool that allows you to talk to the Docker daemon via the Docker API.
  • Docker registries: It stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default.
  • Docker images: It is a read-only template with instructions for creating a Docker container. Once you have created the image you can’t change it, but you can delete it. If you need a newer version, you will need to create a completely new image.
  • Docker container: It is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI.

1) How to Install Docker on Ubuntu

Firstly, you need to uninstall the older versions of Docker that were called docker, docker.io, or docker-engine if they are installed on the system:

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

Make a note: The contents of /var/lib/docker/, including images, containers, volumes, and networks, are preserved.

Update the apt package index and install the following prerequisites for docker:

$ sudo apt-get update

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

Add Docker’s official GPG key:

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

Use the following command to set up a stable repository:

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

Again update the apt package index, and install the latest version of Docker Engine and containerd:

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

Run the below command to uninstall the Docker Engine, CLI, and Containerd packages:

$ sudo apt-get purge docker-ce docker-ce-cli containerd.io

As part of the docker uninstallation process, images, containers, volumes, or customized configuration files on your host will not be automatically removed. You need to remove them manually as follows:

$ sudo rm -rf /var/lib/docker

2) How to Install Docker on Debian

Firstly, you need to uninstall older versions of Docker that were called docker, docker.io, or docker-engine, if they are installed.

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

Make a note: The contents of /var/lib/docker/, including images, containers, volumes, and networks, are preserved.

Update the apt package index and install the following prerequisites for docker:

$ sudo apt-get update

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

Add Docker’s official GPG key:

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

Use the following command to set up the stable repository:

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

Again update the apt package index, and install the latest version of Docker Engine and containerd:

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

Run the below command to uninstall the Docker Engine, CLI, and Containerd packages:

$ sudo apt-get purge docker-ce docker-ce-cli containerd.io

As part of the docker uninstallation process, images, containers, volumes, or customized configuration files on your host will not be automatically removed. You need to remove them manually as follows:

$ sudo rm -rf /var/lib/docker

3) How to Install Docker on CentOS 7/8

Firstly, you need to uninstall the older versions of Docker that were called as docker or docker-engine, if these are installed on the system.

$ sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

Make a note: The contents of /var/lib/docker/, including images, containers, volumes, and networks, are preserved.

Use the following command to add the stable repository:

$ sudo yum install -y yum-utils

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Install the latest version of Docker Engine and containerd:

$ sudo yum install docker-ce docker-ce-cli containerd.io

Use systemctl command to start Docker:

$ sudo systemctl start docker

Run the below command to uninstall the Docker Engine, CLI, and Containerd packages:

$ sudo yum remove docker-ce docker-ce-cli containerd.io

As part of the docker uninstallation process, images, containers, volumes, or customized configuration files on your host will not be automatically removed. You need to remove them manually as follows:

$ sudo rm -rf /var/lib/docker

4) How to Install Docker on Fedora

You need to uninstall older versions of Docker were called docker or docker-engine, if these are installed.

$ sudo dnf remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine docker-selinux docker-engine-selinux

Make a note: The contents of /var/lib/docker/, including images, containers, volumes, and networks, are preserved.

Use the following command to add the stable repository.

$ sudo dnf -y install dnf-plugins-core

$ sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

Install the latest version of Docker Engine and containerd.

$ sudo dnf install docker-ce docker-ce-cli containerd.io

Docker is installed but not started, use systemctl command to start Docker.

$ sudo systemctl start docker

Run the below command to uninstall the Docker Engine, CLI, and Containerd packages:

$ sudo dnf remove docker-ce docker-ce-cli containerd.io

As part of the docker uninstallation process, images, containers, volumes, or customized configuration files on your host will not be automatically removed. You need to remove them manually as follows:

$ sudo rm -rf /var/lib/docker

Optional: How to Allow non-root Users to Run Docker Commands

By default docker is run as root user, but if you want to run docker as a non-root user, you need to add your user to the “docker” group.

$ sudo usermod -aG docker $USER

How to Check if the Docker Engine is installed Properly

You can check if the docker engine is installed properly by running the “hello-world” image. This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Make a note:

  • If you receive the following error while executing the “docker run” command. Please point to google DNS and re-run the command. It is suitable for a home-based network.
  • If the problem persists after changing the DNS, you may be behind an HTTP or HTTPS proxy server, so you need to configure the Docker System service file.
$ sudo systemd-resolve --set-dns=8.8.8.8 --interface=[interface name]

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

Useful Basic Docker Commands

I will show you few basic docker commands.

This command displays system-wide information about the Docker installation, including the kernel version, the number of containers and the images:

$ docker info

Show the Docker version information:

$ docker version

Check the available Docker image on local:

$ docker images

Search Docker image from docker hub:

$ docker search [image_name]

download Docker image:

$ docker pull [image_name]

Run Docker container:

$ docker run [image_name]

Remove Docker image:

$ docker rmi [image_name]

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to Install Docker on Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *