How to install and configure Docker on CentOS 7

Docker is a container technology that allows you to quickly build, test, and deploy applications as portable, stand-alone containers that can run anywhere.

Docker has become the de facto standard for container deployment and is an essential tool for DevOps engineers and their integration and delivery pipelines.

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

precondition

Before embarking on this tutorial, make sure you have:

  • CentOS 7 servers
  • You have a non-root user with rights sudo. Please check this guide on 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 way is to install Docker from the Docker repository.

To install Docker on a CentOS 7 server, follow these steps:

  1. Update installed packages to the latest version:
    sudo yum update
    sudo yum upgrade
    
  2. Install required dependencies:
    sudo yum install yum-utils device-mapper-persistent-data lvm2
  3. Next, run the following command which will add the stable Docker repository to your system:
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  4. Now that you have the Docker repository enabled, install the latest version of Docker CE (Community Edition) with:
    sudo yum install docker-ce
  5. When the installation is complete, launch Docker and enable it on boot with the command:
    sudo systemctl start docker
    sudo systemctl enable docker

  6. You can check if the Docker service is running, use the command:
    sudo systemctl status docker

    The output will look 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 0820 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
  7. At the time of this writing, the latest version of Docker available for CentOS 7 is 18.06.1. Check your Docker version by typing:
    docker -v
    Docker version 18.06.1-ce, build e68fc7a

Run Docker commands without sudo

By default, Docker requires administrator rights. If you want to run Docker commands as a non-root user without sudo , you need to add your user to the docker group created during the installation of the Docker CE package. You can do this by running the following command:

sudo usermod -aG docker $USER

$USER It is an environment variable that holds your username.

See also  A tutorial on configuring Apache Virtual Hosts on CentOS 8

Sign out and sign back in for the group membership to update.

To verify that Docker was installed successfully and that you can run docker commands without it sudoType the following command to download and run the test image in the container and print the message “Hello from Dockerand exit:

docker container run hello-world

The output will look like this:

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

Docker CLI commands take this form:

docker [option] [subcommand] [arguments]

To list all available commands, run docker without parameters:

docker

If you need more help with [subcommand] Whatever it is, you can use it --help As shown below:

docker [subcommand] --help

Definition of Docker Images

Docker Images consist of a series of file system layers that are the instructions in the Dockerfile that make up the executable file of the software application. The image is an immutable binary file including the application and all other dependencies such as libraries, binaries, and instructions needed to run the application.

You can think of Docker Images as snapshots of Docker containers.

Most Docker images are available on the Docker Hub. Docker Hub is a cloud-based logging service that is used among other functions to store Docker images either in a public or private repository.

How to search for Docker images

To find an image from the Docker Hub registry, just use the command search.

For example, to search for a CentOS image, use the following command:

docker search centos

The output will look like this:

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 result prints a table with five columns. NAMEAnd the DESCRIPTIONAnd the STARSAnd the OFFICIAL And the AUTOMATED.

Official images are the images that Docker develops with partners.

See also  Easy ways to rename files and directories in Linux Terminal

Most images in Docker Hub are marked with a version number. When no flags are specified, Docker will “pull” the latest image.

Download Docker Images

If we want to download the official version of the Ubuntu image, we can do so with the command:

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.

To list all types of downloaded images:

docker image ls

The output will look like this:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              e38bc07ac18e        3 weeks ago         1.85kB
centos              latest              e934aafc2206        4 weeks ago         199MB

Remove Docker Images

If for some reason you want to delete an image, you can do so using the sub command rm with the name of the photo you want to delete.

docker image rm centos

The output will be more or less like the following

Untagged: centos:latest
Untagged: [email protected]:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Deleted: sha256:e934aafc22064b7322c0250f1e32e5ce93b2d19b356f4537f5864bd102e8531f
Deleted: sha256:43e653f84b79ba52711b0f726ff5a7fd1162ae9df4be76ca1de8370b8bbf9bb0

Docker containers

The image instance is called the container. Containers represent the runtime of a single application, process, or service.

This may not be the most appropriate comparison but if you are a programmer you can consider the Docker image as a class and the Docker container as an example class.

We can start, stop, delete and manage a container with subcommands docker container

Start Docker containers

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

docker container run centos

At first glance nothing happened at all. The CentOS container will stop immediately after startup, because the command does not have any long running processes, and we have not issued any commands, so the container boots, runs an empty command, and exits.

See also  How to install and configure TensorFlow on CentOS 8

keys -it It allows us to interact with the container through the command line. To get started interactively, run the following command:

docker container run -it centos /bin/bash
[[email protected] /]#

As you can see from the output above, once the container is started, the command prompt changes to mean that you are now running from within the container.

View the list of Docker containers

To see a list of 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

If you don’t have any containers running, the output will be empty.
To see the active and inactive containers, issue a key -a:

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

Remove Docker containers

To remove one or more containers, simply copy and paste the container id after the rm command:

docker container rm c55680af670c

conclusion

You have learned how to install Docker on CentOS 7, 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 does not cover the entire Docker tutorial. In our next few articles, we’ll continue to dive into other aspects of Docker. To learn more about Docker, see the official Docker documentation.

Source link

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply