In this 4-article series, we will discuss Docker, which is an open-source lightweight virtualization tool that runs at top of Operating System level, allowing users to create, run and deploy applications, encapsulated into small containers.

This type of Linux containers are proven to be fast, portable, and secure. The processes that run in a Docker container are always isolated from the main host, preventing outside tampering.

Part 1: Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 8/7

Part 4: How to Monitor Docker Containers in Linux

This tutorial provides a starting point on how to install Docker, create and run Docker containers on CentOS/RHEL 8/7, but barely scratches the surface of Docker.

Step 1: Install and Configure Docker

1. Earlier versions of Docker were called docker or docker-engine, if you have these installed, you must uninstall them before installing a newer docker-ce version.

# yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine

2. To install the latest version of the Docker Engine you need to set up the Docker repository and install the yum-utils package to enable Docker stable repository on the system.

# yum install -y yum-utils # yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo

3. Now install the newer docker-ce version from the Docker repository and containerd manually, because due to some issues, Red Hat blocked the installation of containerd.io > 1.2.0-3.el7 , which is a dependency of docker-ce.

# yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm # yum install docker-ce docker-ce-cli

4. After, Docker package has been installed, start the daemon, check its status and enable it system-wide using the below commands:

# systemctl start docker # systemctl status docker # systemctl enable docker

5. Finally, run a container test image to verify if Docker works properly, by issuing the following command:

# docker run hello-world

If you can see the below message, then everything is in the right place.

Sample Output

Verify Docker Installation

Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/

6. Now, you can run a few basic Docker commands to get some info about Docker:

For system-wide information on Docker

# docker info

For Docker version

# docker version

7. To get a list of all available Docker commands type docker on your console.

# docker

Step 2: Download a Docker Image

8. In order to start and run a Docker container, first, an image must be downloaded from Docker Hub on your host. Docker Hub offers a lot of free images from its repositories.

To search for a Docker image, Ubuntu, for instance, issue the following command:

# docker search ubuntu

9. After you decided on what image you want to run based on your needs, download it locally by running the below command (in this case an Ubuntu image is downloaded and used):

# docker pull ubuntu

10. To list all the available Docker images on your host issue the following command:

# docker images

11. If you don’t need a Docker image anymore and you want to remove it from the host issue the following command:

# docker rmi ubuntu

Step 3: Run a Docker Container

When you execute a command against an image you basically obtain a container. After the command that is executing into the container ends, the container stops (you get a non-running or exited container). If you run another command into the same image again a new container is created and so on.

All the containers created will remain on the host filesystem until you choose to delete them by using the docker rm command.

12. In order to create and run a container, you need to run command into a downloaded image, in this case, Ubuntu, so a basic command would be to display the distribution version file inside the container using cat command, as in the following example:

# docker run ubuntu cat /etc/issue

The above command is divided as follows:

# docker run [local image] [command to run into container]

13. To run one of the containers again with the command that was executed to create it, first, you must get the container ID (or the name automatically generated by Docker) by issuing the below command, which displays a list of the running and stopped (non-running) containers:

# docker ps -l

14. Once the container ID has been obtained, you can start the container again with the command that was used to create it, by issuing the following command:

# docker start 923a720da57f

Here, the string 923a720da57f represents the container ID.

15. In case the container is running state, you can get its ID by issuing docker ps command. To stop the running container issue docker stop command by specifying the container ID or auto-generated name.

# docker stop 923a720da57f OR # docker stop cool_lalande # docker ps

16. A more elegant alternative so you don’t have to remember the container ID would be to allocate a unique name for every container you create by using the --name option on the command line, as in the following example:

# docker run --name ubuntu20.04 ubuntu cat /etc/issue

17. Then, using the name that you allocated for the container, you can manipulate container (start, stop, remove, top, stats) further just by addressing its name, as in the below examples:

# docker start ubuntu20.04 # docker stats ubuntu20.04 # docker top ubuntu20.04

Be aware that some of the above commands might display no output if the process of command that was used to create the container finishes. When the process that runs inside the container finishes, the container stops.

Step 4: Run an Interactive Session into a Container

18. In order to interactively connect into a container shell session, and run commands as you do on any other Linux session, issue the following command:

# docker run -it ubuntu bash

The above command is divided as follows:

-i is used to start an interactive session. -t allocates a TTY and attaches stdin and stdout. ubuntu is the image that we used to create the container. bash (or /bin/bash) is the command that we are running inside the Ubuntu container.

19. To quit and return to host from the running container session you must type exit command. The exit command terminates all the container processes and stops it.

# exit

20. If you’re interactively logged on container terminal prompt and you need to keep the container in running state but exit from the interactive session, you can quit the console and return to host terminal by pressing Ctrl+p and Ctrl+q keys.

21. To reconnect to the running container you need the container ID or name. Issue docker ps command to get the ID or name and, then, run docker attach command by specifying container ID or name, as illustrated in the image above:

# docker attach <container id>

22. To stop a running container from the host session issue the following command:

# docker kill <container id>

That’s all for basic container manipulation. In the next tutorial, we will discuss how to save, delete, and run a web server into a Docker container.