Application deployment can sometimes present some unforeseen challenges to developers. While code can run perfectly well in one environment, the same code can fail to run as expected or fail altogether in a different computing environment.

This difference in computing environments is always a bottleneck to software development and testing. Of course, you could use a virtual machine as a test environment.

However, virtual machines often take up a lot of space, and use up CPU & memory resources on the host system. Thankfully, thanks to Docker, developers can seamlessly develop and deploy their applications on containers. In this guide, we touch base on how to install Docker on CentOS 8.

So, What is Docker?

Docker is an open-source containerization platform that allows developers to package their code or applications inside containers. Containers are stand-alone instances that ship with their own libraries and dependencies.

This takes care of any missing libraries or dependency issues during installation when the application is run across different platforms. In comparison to running virtual machines, Docker is lightweight, takes up less hard drive space, is resource-friendly, and has better performance and efficiency.

Installing Docker on CentOS 8

To get Docker on your system, ensure that you have the following before proceeding:

  1. An instance of CentOS 8 ready.
  2. SSH access to the server with a sudo user configured.
  3. Stable and reliable internet connectivity.

 Once all is clear, follow the steps outlined to install Docker on CentOS.

Step 1: Update the System

 To start off, log in to the server and update the system as follows:

$ sudo dnf update

When prompted whether to proceed with the installation, just press ‘Y’ and hit the ENTER button on the keyboard. The system will update all the packages with newer versions including the kernel, systemd, and other application packages. 

This might take a few minutes to complete depending on when your system was last updated. So, just be patient as the system updates the packages.

Step 2: Add Docker Repository

Once the upgrade is complete, you need to add the Docker repository to the system. By so doing, you will be able to install Docker using the DFN package manager.

To add the repository, invoke the following command in the terminal:

[user@centos8 ~]$ sudo dnf config-manager –add-repo=https://download.docker.com/linux/centos/docker-ce.repo

You should get some simple output confirming that the repo has been added.

Once the Docker repository is in place, it’s worthwhile to have a peek at the available Docker versions. You can accomplish this by issuing the command:

[user@centos8 ~]$ sudo dnf list docker-ce

The output displays the name of the Docker package, architecture, and version, just to mention a few as you can see in the output.

Step 3: Install Docker-CE

Next, we are going to install Docker-CE which is short for Docker Community Edition. A disclaimer: The latest Docker version is not usually available for all repositories. As such, we will append the –nobest flag to the command as follows:

[user@centos8 ~]$ sudo dnf install docker-ce –nobest

The –nobest flag allows us to install the most preferred option for our server instance. As before, press ‘Y’ and hit next to continue.

Upon completion of the installation, confirm the Docker version on command-line as shown.

$ docker –version

To run docker commands as a sudo user , add the user to the docker groups as shown.

$ sudo usermod -aG docker $USER

For this to take effect, initiate a system reboot. 

Step 4: Manage Docker Service

Docker runs as a daemon, just like services such as SSH. This implies you can perform tasks such as checking its status, starting and stopping it, etc. to mention a few. 

At the moment, Docker is currently inactive and not running. To start the Docker service, run:

$ sudo systemctl start docker

Check whether Docker is running by invoking:

$ sudo systemctl status docker

The output confirms that Docker is active and running.

Additionally, you can enable docker to start every time your boot or reboot as follows.

$ sudo systemctl enable docker

In the same vein, you can disable Docker as follows:

$ sudo systemctl disable docker

Step 5: Using Docker to Perform Docker Tasks

As we explained earlier on in the introduction, Docker is a containerization platform that manages container images, which are isolated units complete with their own libraries, dependencies and configuration files. You can create your own container image or grab one from Docker hub which is a huge repository replete with thousands of images from major vendors.

First, we will run a simulation to test if Docker is functioning as expected. To do this, we are going to pull a ’Hello world’ image from Docker hub as shown.

$ docker run hello-world

 You should get the output below as the container prints out the message ‘Hello from Docker!’

As outlined from the output, Docker simply pulled the hello-world image from Docker hub, spawned a container image and ran the container which then streamed the message ‘Hello from Docker!’ on the terminal.

To pull a docker image only without running a container, use the syntax:

$ docker pull image-name

For example, let’s try and pull an Apache web server image.

$ docker pull httpd

To confirm the docker images residing on the system invoke the command:

$ docker images

From the output, we have 2 images: the hello-world image and the httpd Apache web server image. The output gives you the  repository name, Image tag, Image ID, creation time and the size of the image.

With the image on the system, you can spin a container by running:

$ docker run image-name

This runs the container in the foreground. However, this is not always desired as you might be unable to run any other commands. A workaround would be to run the image in the background so that you can continue running other commands.

To run the container image in the background, use the -d option.

$ docker run -d image-name

For example, to run the Apache container image, run:

$ docker run -d httpd

To check running containers use the command:

$ docker ps

Once again, you get detailed output about the container including Container ID, the image the container was created from, creation time and status as well as the container name.

Once you are done running the container, you can stop it using the syntax:

$ docker stop CONTAINER-ID

For instance, to stop the httpd container, run:

$ docker stop c07ffb3fc13f

You can verify that the container is stopped using the docker ps command as earlier explained.

$ docker ps

Additionally, you can append the -a flag to view all containers – whether running or stopped.

$ docker ps -a

To remove an image, you must first stop the container as seen earlier. Once stopped, use the syntax below to remove the image.

$ docker rmi -f REPOSITORY NAME or IMAGE ID

For instance, to remove the hello-world image stop the image as follows.

$ docker stop 043df63a1b29

Then remove the image as follows.

$ docker rmi -f  hello-world

There are plenty of docker commands but we have decided to give you a bird’s eye view of what you can achieve with docker and docker images.

Conclusion

Although Docker has been deprecated in favor of podman and buildah, which are containerization tools, it is still widely used by developers and beginners looking to get started in containerization. Hopeful, you can now install Docker and run docker commands with ease.