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 everywhere.
In this guide, we will show you how to install Docker CE on CentOS 7 and explore the basic Docker concepts and commands.
Be sure to check that you have the following prerequisites for this tutorial:
- CentOS 7 server
- You have access to a non-root user with sudo privileges. Check this guide about how to create a new sudo user.
Even though the Docker package is available in the official CentOS 7 repository, it may not always be the latest version. The best approach is to install Docker from Docker’s repositories.
Install the needed dependencies:
sudo yum install yum-utils device-mapper-persistent-data lvm2
and append the stable Docker repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
After the repository is added, to install the latest version of Docker CE (Community Edition), use the following command:
sudo yum install docker-ce
Once the Docker package is installed, we initiate the Docker daemon with the following command:
sudo systemctl start docker
To confirm that the Docker service is running, use the following command:
sudo systemctl status docker
docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2018-05-08 09:53:33 UTC; 5s ago Docs: https://docs.docker.com Main PID: 2327 (dockerd) Tasks: 16 Memory: 36.4M CGroup: /system.slice/docker.service ├─2327 /usr/bin/dockerd └─2331 docker-containerd --config /var/run/docker/containerd/containerd.toml
Enable the Docker service so it is automatically started at boot time:
sudo systemctl enable docker
At the time this guide was made, the stable version of Docker was 18.03.1; we can check our Docker version by typing:
docker -v Docker version 18.03.1-ce, build 9ee9f40
By default, managing Docker requires administrator privileges. If you want to run Docker commands as a non-root user without sudo, you need to add your user to the docker group, which is created during the installation of the Docker CE package:
sudo usermod -aG docker $USER
Log out and log back in so that the group membership is refreshed.
To verify Docker is installed successfully and that you can run docker commands without sudo, run 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
The output should look like the following:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 9bb5a5d4561a: Pull complete Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77 Status: Downloaded newer image for hello-world:latest Hello from Docker!
This message shows that your installation appears to be working correctly.
Basic Docker Syntaxes
Since we already have Docker installed, before we jump in, we can go over the basic syntax of the docker CLI:
docker [option] [subcommand] [arguments]
To list all available commands, run docker with no parameters:
If you need more help on any [subcommand], you can use the following:
docker [subcommand] --help
A Docker image is made up of a series of layers representing instructions in the image’s Dockerfile that make up an executable software application. An image is an immutable binary file that includes the application and all other dependences, including other binaries, libraries, and instructions necessary for running the application. In short, a Docker image is essentially a snapshot of a Docker container.
The Docker Hub is a cloud-based registry service which, among other functionalities, is used for keeping the Docker images either in a public or private repository.
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
The output should looks like the following:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4257 [OK] ansible/centos7-ansible Ansible on Centos7 109 [OK] jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x86_… 94 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 52 [OK] imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 40 [OK] tutum/centos Simple CentOS docker image with SSH access 39
As you can see, the search results print a table with five columns: NAME, DESCRIPTION, STARS, OFFICIAL, and AUTOMATED. The official image is an image that Docker develops in conjunction with upstream partners.
Let’s say that we want to download the official build of CentOS 7; we can do that by using the image pull subcommand:
docker image pull centos Using default tag: latest latest: Pulling from library/centos 469cfcc7a4b3: Pull complete Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16 Status: Downloaded newer image for centos:latest
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 The output should look something like the following: REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest e38bc07ac18e 3 weeks ago 1.85kB centos latest e934aafc2206 4 weeks ago 199MB
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 Untagged: centos:latest Untagged: [email protected]:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16 Deleted: sha256:e934aafc22064b7322c0250f1e32e5ce93b2d19b356f4537f5864bd102e8531f Deleted: sha256:43e653f84b79ba52711b0f726ff5a7fd1162ae9df4be76ca1de8370b8bbf9bb0
What is a container?
An instance of an image is called a container. A container represents a runtime for a single application, process, or service.
It may not be the most appropriate comparison, but if you are a programmer, you can think of a Docker image as a class and a Docker container as an instance of a class.
We can start, stop, remove, and manage a container with the docker container subcommand.
The following command will start a Docker container based on the CentOS image. If you don’t have the image locally, it will download it first:
docker container run centos
At first sight, it may seem to you that nothing has happened at all. Well, that is not true. The CentOS container stops immediately after booting up because it does not have a long-running process and we didn’t provide any command, so the container booted up, ran an empty command, and then exited.
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
To list active containers, type:
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79ab8e16d567 centos "/bin/bash" 22 minutes ago Up 22 minutes ecstatic_ardinghelli
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:
[[email protected] /]#
If you don’t have any running containers, the output will be empty.
To view both active and inactive containers, pass the -a switch:
docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79ab8e16d567 centos "/bin/bash" 22 minutes ago Up 22 minutes ecstatic_ardinghelli c55680af670c centos "/bin/bash" 30 minutes ago Exited (0) 30 minutes ago modest_hawking c6a147d1bc8a hello-world "/hello" 20 hours ago Exited (0) 20 hours ago sleepy_shannon
To delete several containers, just copy the container ID (or IDs) from above and paste them after the container rm subcommand:
docker container rm c55680af670c
This guide just barely scratches the surface of the Docker ecosystem.