Previously, if you were to run a web server, you had to either set up a Virtual Machine on your own or find a physical server elsewhere. This is often referred to as “bare-metal” because the code is actually running on the processor with no abstraction. This is ideal if you need high efficiency and have a large and expert team to manage these servers. This article is a guide for those who want to know about How to Install Docker on Linux?
The trouble with operating your servers on bare metal is that you become incredibly inflexible. What if you need to fire up another server? Call Dell or IBM and request that another one be sent to you, then have your technician update the physical server, set it up, and put it into the server cluster. This creates a need for somewhat simpler technology to develop and host applications.
Need of Docker
Docker is an open-source project that allows you to create, ship, and run applications isolated from the underlying host. It’s an open-source Linux tech, which means anybody can use it and it’s built from a wide range of perspectives, thus offering multiple features.
A Docker image is a multi-layered file used to create the environment of a Docker container. An Image is basically a set of instructions for a completely functioning and executable version of an application that runs on the host operating system kernel. When a Docker user runs an image, one or more instances of that image called containers are generated.
How to Install Docker on Linux?
Now that you have a good understanding of Docker and objects such as containers and images, et’s look into different ways to install Docker on Linux.
In this article, we are going to use the ubuntu 20.04 machine. You can use the docker.io package if you use any of the ubuntu machines.
Difference between docker.io and docker-ce
Docker.com offers docker-ce, while Debian provides Docker.io. Overall, this means you can install docker.io straight away, while docker-ce requires you to first connect to a docker.com external repository. More specifically, although both bundles have properly released versions of Docker, their internal mechanisms are somewhat different.
Docker-ce follows the Golang approach. Before the installation, all dependencies are taken into the source tree, and everything is combined into a single box. As a result, you always upgrade Docker with all of its dependencies at the same time.
We are going to install docker-ce in our ubuntu machine, and we will start with updating the packages.
$ sudo apt-get update
Next, we remove the older versions of Docker present in our machine (if any).
$ sudo apt-get remove docker docker-engine docker.io containerd runc
There are a couple of ways using which we can install docker. The first is using repositories. For ease of installation and update activities, most users set up Docker’s repositories and install using them. The other method is through the deb package and manual up-gradation.
Installation Through Repositories
- First, we need to install the pre-requisite packages such as curl, ca-certificates, etc.
$ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release
- Next, we need to add the official GPG key. We can use the curl command to do so.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Next, we need to set up a stable repository.
$ echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Next, we can install the docker engine. Here, we need to update the packages from the repositories that we just added.
$ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io
- We can install different versions of docker. To list all the versions available in your added repository, type the following command.
$ apt-cache madison docker-ce
- To install a specific version, we can use the below command.
$ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
We need to replace the VERSION_STRING with the specific version listed in the above command, for example, 5:18.09.1~3-0~ubuntu-xenial.
- To verify the installation, we can use the docker run command to run a sample hello-world image.
$ sudo docker run hello-world
This command downloads and runs a test image in the host machine. When the container is started, it prints a message and then exits. We can now verify that the Docker Engine has been built and is ready to use. While the docker group is formed, no users are attached to it. To execute Docker commands, you’ll need sudo access.
Installation through package
If you are not able to install Docker Engine from Docker’s registry, you can manually import the .deb file for your own release version and then install it. In such a case, when we want to update Docker, we must download the updated package.
- First, we need to go to the official Docker Website to check out the available versions. If you are using a Debian Ubuntu Linux, you can visit this link. For any other version, you can select your Linux version/flavor and download the corresponding file. Then, you can install it through the terminal or like any other executable file.
$ sudo dpkg -i /path/to/package.deb
For Debian packages, we can use the above command to install. Next, to verify the installation, we can use the docker run command to run a hello-world image.
$ sudo docker run hello-world
As mentioned above, we can run docker commands without sudo by adding users to the docker group.
Instead of using a TCP port, the Docker daemon uses a Unix socket. By default, the user root controls the Unix socket, and other users can only use it through sudo. However, the root user is still used for the Docker daemon.
To avoid using sudo, we can build a Unix group named docker and connect users to it. The Docker daemon generates a Unix socket which the members of the docker group can use.
To do so, we need to run the below commands which will create a docker group and add a user to that group.
$ sudo groupadd docker $ sudo usermod -aG docker $USER
Now, we need to log out and log back in for the changes to come into effect. After that, we can run the docker command without the sudo keyword.
In this article, we have discussed the entire process of installing Docker in a Linux machine in great detail [How to install Docker on Linux]. We discussed the differences between the docker.io and docker.ce and saw two different ways to install Docker on a Linux machine. The first one is through the repositories and the second one is directly by downloading the Debian package. Moving ahead, we discussed how to run docker commands without using sudo by adding the user to the Docker group.
We certainly hope that you will now be able to begin your journey with Docker.
People are also reading:
- What is Docker Networking?
- What is Docker Swarm?
- What are Docker Storage Drivers?
- What is Docker Container Linking?
- What is Docker Logging?
- Difference between Docker Images and Container
- Docker Interview Questions
- How to Use a Dockerfile to Build Docker Images?
- How to list Docker Images?
- How to Search Docker Images using CLI?