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.
Before proceeding with this tutorial, make sure that the following prerequisites are met:
- CentOS 7 server
- You are logged in as a non-root user with sudo privileges. You check this guide about how to create a new sudo user.
Install Docker on CentOS
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.
To install Docker on your CentOS 7 server follow the steps below:
Start by updating your system packages and install the required dependencies:
sudo yum update
Next, run the following command which will add the Docker stable repository to your system:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Now that the Docker repository is enabled, install the latest version of Docker CE (Community Edition) using yum by typing:
sudo yum install docker-ce
Once the Docker package is installed, start the Docker daemon and enable it to automatically start at boot time:
sudo systemctl start docker
To verify that the Docker service is running type:
sudo systemctl status docker
The output should look something like this:
● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2018-10-31 08:51:20 UTC; 7s ago Docs: https://docs.docker.com Main PID: 2492 (dockerd) CGroup: /system.slice/docker.service ├─2492 /usr/bin/dockerd └─2498 docker-containerd --config /var/run/docker/containerd/containerd.toml
At the time of writing, the current stable version of Docker is,
18.06.1, to print the Docker version type:
Docker version 18.06.1-ce, build e68fc7a
Executing the Docker Command Without Sudo
By default managing, Docker requires administrator privileges. If you want to run Docker commands as a non-root user without prepending
sudo you need to add your user to the docker group which is created during the installation of the Docker CE package. You can do that by typing:
sudo usermod -aG docker $USER
$USER is an environment variable that holds your username.
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, 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
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.
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:
If you need more help on any
[subcommand], just type:
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 including the application and all other dependencies such as 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 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 look 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 prints a table with five columns,
AUTOMATED. The official image is an image that Docker develops in conjunction with upstream partners.
If 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: centos@sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16 Deleted: sha256:e934aafc22064b7322c0250f1e32e5ce93b2d19b356f4537f5864bd102e8531f Deleted: sha256:43e653f84b79ba52711b0f726ff5a7fd1162ae9df4be76ca1de8370b8bbf9bb0
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 class and 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 CentoOS 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 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.
-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:
To list running 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
If you don’t have any running containers the output will be empty.
To view both running and stopped containers, pass it the
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 one or more containers just copy the container ID (or IDs) from above and paste them after the
container rm subcommand:
docker container rm c55680af670c
You have learned how to install Docker on your CentOS 7 machine and how to download Docker images and manage Docker containers. You may also want to read about Docker Compose, which allows you to define and run multi-container Docker applications.
This tutorial barely scratches the surface of the Docker ecosystem. In some of our next articles, we will continue to dive into other aspects of Docker. To learn more about Docker check out the official Docker documentation.
If you have any questions or remarks, please leave a comment below.