How to Install and Use Docker on CentOS 7

Docker is a containerization technology that allows you to quickly build, test and deploy applications as portable, self-sufficient containers that can run virtually anywhere.

In this tutorial, we’ll go through how to install Docker CE on CentOS 7 and explore the basic Docker concepts and commands.

Install Docker

Although the Docker package is available in the official CentOS 7 repository, it may not always be the latest version. The recommended approach is to install Docker from the Docker’s repositories.

1. Start by updating your system packages and install the required dependencies:

yum update
yum install yum-utils device-mapper-persistent-data lvm2

2. Next, run the following command which will add the Docker stable repository to your system:

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

3. Now that the Docker repository is enabled, install the latest version of Docker CE using yum by typing:

yum install docker-ce

4. Once the Docker package is installed, start the Docker daemon and enable it to automatically start at boot time:

systemctl start docker
systemctl enable docker

5. To verify that the Docker service is running type:

systemctl status docker

6. To check the version of Docke:

docker -v

Executing the Docker Command

To verify Docker is installed successfully and that you can run docker commands, issue the following command which will download a test image, run it in a container, print a “Hello from Docker” message and exit:

docker container run hello-world

Docker command line interface

Now that we have a working Docker installation, let’s go over the basic syntax of the docker CLI.

The docker command line take the following form

docker [option] [subcommand] [arguments]

You can list all available commands by typing docker with no parameters:

docker

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

docker [subcommand] --help

To get detailed information about the system, use:

docker info

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:

To search the Docker Hub repository for an image just use the search subcommand. For example, to search for the CentOS image, run:

docker search centos

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 image pull command:

docker image pull centos

Depending on your Internet speed, the download may take a few seconds or a few minutes. Once the image is downloaded we can list the images with:

docker image ls

If for some reason you want to delete an image you can do that with the image rm [image_name] subcommand:

docker image rm centos

Docker Containers

There are different types of containers. The hello-world container you ran 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.

The following command will start a Docker container based on the CentoOS image. If you don’t have the image locally, it will download it first:

docker container run centos

The switch -it allows us to interact with the container via the command line. To start an interactive container type:

docker container run -it centos /bin/bash

As you can see from the output once the container is started the command prompt is changed which means that you’re now working from inside the container :

If you want to exit the container you can use the exit command:

exit

To list running containers : , type:

docker container ls

If you don’t have any running containers the output will be empty

To view both running and stopped containers, pass it the -a switch:

docker container ls -a

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

docker ps -l

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

docker stop container-id

Conclusion

You have learned how to install Docker on your CentOS 7 machine and how to download Docker images and manage Docker containers. This tutorial should provide you with enough information to get you started.

That’s all we guide you in this article, Good luck !


On this page