Installing and Setting up Docker on CentOS 7

Introduction

Docker is a platform that makes running and managing application processes in containers easier. It provides a way to separate your applications from your infrastructure. Containers are very similar to virtual machines, but they are more portable, efficient, and easy to use. There are various ways to install Docker on Linux distributions. The most popular and easy way to install Docker on the existing operating system is by using yum commands.

This tutorial will demonstrate how you can set up and use Docker on CentOS 7 in a few simple steps.

Prerequisites:

All the commands you execute throughout this tutorial are run as a non-root user and if necessary root access will be provided using the sudo keyword.

Step 1: Setting up Docker on CentOS 7 Instance

The official CentOS 7 repository may not have the latest installation package for Docker. In this section, you’ll be installing the latest version of Docker from the official Docker repository. First, you need to update the package database using:

After the update step is complete, run the below command to download and install the latest version of Docker:

Docker is now installed, hence you can start the Docker daemon by running the command below:

To verify if the Docker daemon is running type the following:

The output of the systemctl status command should be similar to the below-shared output, which shows it is running:

Finally to make sure Docker starts up as you reboot your machine use the command:

The Docker installation provides you with Docker service as well as the client utility (Docker command-line client). In the next sections of the tutorial, you’ll be able to get more hands-on using the Docker commands.

You can also take a look at our in-depth tutorial on how to install & operate Docker on Ubuntu in the public cloud.

Step 2: Using Docker Commands Without Sudo Prefix

Docker commands need root privileges to run. Thus, if you want to run the commands you will have to prefix them with sudo. During installation, a Docker group is created by default. If you add a user to the group you can run the Docker commands without sudo. Trying to run Docker commands without sudo or adding the user to the group will result in an output similar as below:

Adding your username to the Docker group will ensure you don’t have to use sudo to run the Docker commands:

To add another user to the Docker group you can simply replace the username in the command:

For the rest of this guide, we will assume that all the commands are executed by a user in the Docker user group. If this is not the case, you can use the sudo prefix with the commands.

Step 3: Executing the Docker Commands

Now that you have Docker installed and running, let’s look at some commands to get familiar with the Docker command-line utility. Docker commands usually take the form of:

To find all the available subcommands you should use:

As of Docker 20.10.3, the complete list of available subcommands includes:

You can use –help flag with a specific command to get more information about it:

To get detailed information about the system, use:

Step 4: Working with Docker Images

Docker images can be called the blueprint for Docker containers. These images are usually pulled from the Docker Hub, which is a registry managed by the Docker project. Anyone can create and push their images on the Docker Hub. As a result, you can easily find a wide variety of applications and os distributions in the registry. Let’s try out a simple program that will confirm access to the Docker Hub:

You should get an output as below, which shows that Docker is working:

You can find various Docker images on the Docker Hub by using the search command. For example, see the below command to search for a CentOS image:

The search query will show up a list of all the images which matched with the substring. In your case the output should be like:

In the search results, there are different columns describing information about the image. The OK in the OFFICIAL column determines that the image was created and supported by the company behind the application. Once you have finalized the image, you can download it to your local machine using the Docker pull command:

After downloading the image, you can run the container using the Docker run command. If you directly try to run an image without prior downloading, Docker will download the image and run the container afterward:

You can list the images that are downloaded to your local machine, using the below command:

You should get a similar output:

Later in this tutorial, you’ll be able to modify the images to run the containers. These new images can be added or pushed in the Docker Hub and other registries that host the Docker images.

Step 5: Running a Container Interactively

There are different types of containers. The hello-world container you ran in Step4 is a type of container which runs and exits after printing a message. Another type of container is the interactive one. You can use interactive containers in a similar fashion as a virtual machine.

Let’s create a container from the latest CentOS image. Using -i and -t flags in the Docker run command will give interactive access to the CentOS container:

The command prompt will change and it should look like the output below:

Now any command you execute will run inside the container. That is similar to running a command in a virtual machine. Let’s try installing MySQL server in the CentOS container. You can do this using:

Step 6: Committing Changes in a Container to a Docker Image

After starting the container, you can do all the operations which are doable in a similar virtual machine, like creating/modifying the files or setting up an app. Please note that these changes will only stay for that container, and after you destroy the container the changes you made will be lost.

In this part of the tutorial, you’ll learn how to create a new Docker image from a container with the changes you have made. After Step5 you have a CentOS container running with MySQL server installed. This container is now different than the plain CentOS image. You can save this state of container for further use. First, you need to exit the container using:

Commit the changes you have made in the container to a new Docker image using the below command:

In the command, -m refers to the commit message and should basically denote the changes made, -a tag is used to mention the author. Container-id is the one from Step5, which you got after running the container in interactive mode, and usually, the repository is your username for Docker Hub. For example:

Now that the image is committed, the Docker images command should list the new image as well as the old ones:

The output of the command should be similar as below:

As seen in the example, a new image centos-mysql is created using the CentOS image from the Docker Hub. The difference in size determines that some changes were made. In this example, it was the addition of the MySQL server in the container. Next time if you need a container with a MySQL server, you can just run the new image, and voila! You have a CentOS container with a pre-installed MySQL server running.

Step 7: Managing Docker Containers

Now that you are familiar with Docker, after using it for some time you already have some running and some inactive containers. To get the list of active containers you should use:

You should see a similar output:

In order to list both the active and inactive containers, you should use the -a flag with the command:

To find the last container you created, you can provide -l flag:

To stop a running/active container run a simple command:

You can find the container-id in the output of Docker ps command.

Step 8: Publishing the Images to a Repository

The next step after creating the new image is to share that with your friends. You can also make it available for the whole world using Docker Hub or any other registry. You are required to login into the respective registry before pushing the image.

In the next part of the tutorial, you’ll learn how to push the images to Docker Hub. First, sign up on Docker Hub. You’ll need to log into Docker Hub to push your image using the command below:

Once you provide the right password, and authentication is successful, you can push your image. To push the image use the below command:

The output for the command will be similar to this:

Once you have pushed the image, it should show up on your account’s dashboard, as shown in the image below:

docker account dashboard

In case of failure in a similar way, chances are that you have not logged in:

You can log in, and repeat the push attempt.

Conclusion

There are a number of ways in which you can make use of Docker. This tutorial should provide you with enough information to get you started. And since Docker is a really trending project, you can find many details about the usage as well as different use cases from the project’s blog page.

You can also check out our other Docker tutorials to learn more about what you can do with Docker:

Happy Computing!

About Pranay Kapgate

Software Engineer | Open Source Software aficionado