Docker is famous for its revolutionized development, and in the last few years, its popularity has spiked a lot. Before docker and containers, the deployment and scaling was a heck of a headache.
There are three methods available on the Official Documentation webpage of Docker for installing Docker on the CentOS 8 Operating system.
- One is using automated convenience scripts.
- The second is by downloading the RPM package of Docker and installing it manually.
- The third method is to set up Docker’s repository and install it from there, which is a recommended and most suitable way.
This post will definitely go with the recommended and most efficient way to install Docker on CentOS 8 Operating system.
Installation of Docker on CentOS 8 using repository
To install the latest version of Docker on CentoOS 8, before even beginning with the installation, make sure you do not have an older version of Docker installed on your CentOS 8 Operating system.
Uninstall old versions
First of all, it is better to uninstall Docker and docker-engine, which were the older versions of Docker. So, execute the command typed below to remove/uninstall docker, docker-engine, and all of its dependencies:
If DNF finds no matches for these packages, then it is totally fine.
The new Docker Engine package is called docker-ce, and its installation process on CentOS 8 is straightforward.
Although Docker is available in the Official DNF package repository to download and install on CentOS 8, it is not the latest version. To install the latest version, installing Docker by adding its Official repository to the centOS 8 system is the best method.
Before adding the repository, update the system’s package repository by using the command:
After updating, if there are any packages to upgrade, then upgrade them using the command typed below:
Once all the system’s packages are up to date, set up the repository. The Docker’s repository can be set up by adding the repository to the system’s sources list.
Set up the repository
To add any repository to the system’s sources list, the syntax is really simple. You can use the command given below to add the Docker’s repository to the system:
Once Docker’s repository is added, you can install the latest available version of Docker Engine on CentOS 8.
Install Docker Engine
Since Docker’s repository is added to the system, Docker’s installation is really simple and easy, just like installing any other package in centOS 8. To install the latest version of Docker straight from the Docker’s repository, execute the command typed below:
In the above command, the “–nobest” option is used to erase the installed packages for resolving dependencies. The “–allowerasing” option is used to erase the installed packages for resolving dependencies.
The -y flag is already added to the installation command, so the installation process would not prompt anything. You do not have to type y and hit Enter to continue the installation process of Docker.
Alright, at this stage, Docker is successfully installed but not ready to use. To start using Docker, we need to start the Docker daemon. To start the Docker daemon on centOS 8, execute the command given below:
Once the Docker daemon is started, it does not show any output. You can verify either it is running or not by checking the status of the Docker daemon.
To verify the status of the Docker daemon, run the command typed below::
In the output screenshot attached below, you can witness that the service is active and running.
Lastly, instead of starting the Docker daemon every time, why don’t we enable the Docker daemon on the system boot to start at the boot time. To start the Docker daemon automatically on the boot time, execute the command:
At this stage, the Docker is installed, and the docker-daemon is started; you have the docker command-line utility from where you can use docker and perform docker-related tasks. Let’s learn the basic usage of Docker on CentOS 8 Operating system.
Usage of the Docker Command-line Utility
After completing the installation of Docker, let’s dig into its usage and learn about the Docker command-line utility.
The syntax of writing the docker command is:
To know about all the options and commands available, simply run the docker command in your terminal and hit Enter:
Work with Docker Images
Docker has a registry known as Docker Hub, where all the Docker images are stored. A Docker Image contains the application and its dependency software along with some other required libraries. From Docker Hub, we can pull any Docker image available, and by using these docker images, Docker containers can run on the Docker platform.
There is no restriction of building and hosting of Docker images on Docker Hub. On the Docker Hub, any person can host his Docker Image.
Let’s try to access, download, and run a “hello-world” Docker Image from the Docker hub and see whether it works or not. To download and run a Docker Image from the Docker Hub, execute the docker command with the subcommand run, as typed below:
You can witness that the “hello-world” Docker Image is successfully downloaded and run from the Docker Hub in the output provided below.
Similarly, if you do not know whether a docker image of your need exists in the Docker Hub or not, you can search for it. For example, to search for a “CentOS” Docker Image from the Docker Hub, execute the docker command with the subcommand search, as shown below:
The above command will search for the string “centos” from the whole Docker Hub and fetch all the Docker Images which contain this string in their names and show the result on your terminal:
In the above-attached screenshot, you can see that we have got all the Docker Images, their descriptions, and stars containing “centos” in their name.
After finding the Docker Image of your need, you can get/download that Image by executing the docker command with the help of the subcommand pull, as shown below:
Once an Image is pulled successfully, you can simply run it using the run subcommand:
To list all the docker images in your system, type the simple command given below in your terminal:
You can witness in the output that “hello-world” and “centos” images are listed out.
After understanding the whole concept of Docker Images, let’s learn a bit about Docker Containers.
Running a Docker Container
Since Docker containers are just like Virtual Machines and they are much interactive and resource-friendly. Recently, we ran a “hello-world” container, which shows the hello world message and exits.
Now, let’s try to run another container with the CentOS image that we have just downloaded. To run the container with centos image, run the command:
In the above command, we used the -it switches, which will give us access to an interactive shell into the container.
As you can see in the screenshot attached above, we are into the Docker container’s shell now, and 163e7ed6525f is the container ID.
Now, whatever the command we will execute, it will be performed inside the container. For example, if we want to download and install MySQL in the container, we just need to run the “dnf install @mysql” command without even writing sudo at the start of the command because we are logged in as a root user:
You can witness that the installation of MySQL inside a container has begun.
Type ‘y’ to import GPG keys and hit enter to continue the installation process of MySQL inside the Container.
At this stage, you can witness that MySQL is successfully installed.
After making desired changes to the container, you definitely need to commit the changes in a Docker Container to a Docker Image.
Until now, the changes that we have made, like installing MySQL, are just applied to the container. However, if we mistakenly forget to save changes, then everything will definitely be lost.
After installing MySQL inside a container, to save the state of the container as a new image, you first need to exit from the container in which you are present.
To save the changes that we have performed inside a container, exit from it using the command typed below:
After exiting from the container, execute the docker commit command to commit the changes to a new Docker image. The syntax for committing changes is as follows:
In the above syntax:
“-m” is for commit message that explains what you did and what changes did you made,
“-a” is for the Author name who is committing these changes,
“Container-id” is the ID of the container that we have just seen in the Container shell,
At last, provide the “repository name.” The repository name can any new name that you want to give; it is a new image name.
To commit the changes that we made recently, the docker commit command would go like this:
Once the changes are committed, a new image will be created on the CentOS Operating system.
You can show the images on your CentOS Operating system using the docker images command:
You can witness the output screenshot attached above; a new Docker image with centos is created 4 minutes ago and listed in the Docker images list. You can also see the increase in the image’s size compared to the older centos image. The increase in the size of the image is definitely due to the changes that we recently made in the container(installation of MySQL).
From now on, whenever you have to run a container with MySQL, you can use the latest centos image.
Listing Docker Containers
We have just listed the Docker Images; we can list the Docker Containers as well. There can be two states of Containers in Docker:
- Active Containers
- Inactive Containers
To show/list down the active Docker Containers, execute the command given below:
To show/list down all the Docker Containers, execute the docker ps command with the -a flag:
If you want to stop an active container, simply execute the docker command with the subcommand stop and container ID as shown below:
Pushing Docker Image
The last thing you need to learn is pushing a Docker Image to the Docker Hub and sharing it.
To push or upload a Docker image on the Docker Hub, you must first have an account on the Docker Hub. If you have no account, sign up and create one; otherwise, you are ready to move ahead for pushing a Docker Image if you already have an account.
To push an image, first, log in to the Docker Hub using the command typed below:
Enter the password of your Docker Hub, and if the password you entered is correct, you will be logged in and push the Docker Image of your choice.
For pushing an Image, execute the below-given docker command with the subcommand push and Docker Image Name:
Uploading of Docker Image will take time-based on the size of the Image:
The Pushed Docker Image will be listed on the dashboard page of your Docker Hub account, and you can verify it from there.
Contrary to this, if you cannot push a Docker Image to the Docker Hub, you are definitely not logged correctly. Try to log in again and push the Docker Image.
This post contains a complete step-by-step and profound guide on a Docker and how to install and set up Docker on the CentOS 8 operating system. In this post, we have learned to install Docker by adding Docker’s repository to the CentOS 8 system and install the latest version of Docker.
We have learned the basic yet detailed usage of Docker. We have learned about Docker Images, Docker Containers, how to pull a Docker Image from the Docker Hub, how to run a docker container, how to commit changes of a container to a Docker Image, and lastly, how to push a Docker Image to the Docker Hub for sharing it with the international community.