Using Docker in CentOS 7

Instructions for working with Docker Community Edition (CE) in CentOS 7

Docker is a software for automating the deployment and management of applications in an operating system-level virtualization environment. It allows you to package an application with all its environment and dependencies into a container that can be ported to any Linux system with kernel cgroups support and provides a container management environment

file

You can learn how to install Docker in this manual

Using the Docker command

The docker command allows you to use various options, commands with arguments. The syntax is as follows: ``.shell docker [option] [command] [arguments]

To see all available subcommands, type:
``.shell
docker

The complete list will look like this: ``.shell attach Attach local standard input, output, and error streams to a running container build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull Pull an image or a repository from a registry push Push Push an image or a repository to a registry rename Rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run Run a command in a new container save Save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes

To see the options to use a certain command, type
``.shell
docker docker-subcommand --help

To view all the information about Docker, you can use the command: ``.shell docker info

**Working with Docker images**

By default, Docker gets images from the [Docker Hub](https://hub.docker.com/), which is a registry of images maintained by Docker 

To check if you can access and download images from Docker Hub, type the following command:
``.shell
docker run hello-world

The correct result of this command, which means that Docker is working correctly, is shown below: ``.shell [root@kvmde54-19861 ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76 Status: Downloaded newer image for hello-world:latest

Hello from Docker! This message shows that your installation appears to be working correctly.

To search for the desired image, use the following command format:
``.shell
docker search <имя>

For example, to search for an nginx image, use the following command: ``.shell docker search nginx

This will bring up a list of available images:
``.shell
[root@kvmde54-19861 ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx.                        13117 [OK]    
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 1791 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of... 771 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS... 108   
bitnami/nginx Bitnami nginx Docker Image 83 [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp... 70 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou... 58 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho... 56  
nginxdemos/hello NGINX webserver that serves a simple page co... 48 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 40 [OK]
nginx/unit NGINX Unit is a dynamic web and application ... 37  
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 30  
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm &amp; Al... 24 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra... 18 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 14  
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n... 13  
centos/nginx-112-centos7 Platform for running nginx 1.12 or building ... 13  
raulr/nginx-wordpress Nginx front-end for the official wordpress:f... 12 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 12  
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a... 7 [OK]
mailu/nginx mailu nginx frontend 6 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr... 5 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK]
wodby/nginx Generic nginx 1 [OK]
centos/nginx-110-centos7 Platform for running nginx 1.10 or building ... 0 

In the OFFICIAL column, the line OK shows that the image is built and supported by the company developing this project. When the desired image has been selected, you can download it to your computer using the pull subcommand.

For example, to download the official ubuntu image to your computer: ``.shell docker pull nginx

You will see a similar result:
``.shell
[root@kvmde54-19861 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
54fec2fa59d0: Pull complete 
4ede6f09aefe: Pull complete 
f9dc69acb465: Pull complete 
Digest: sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

After the image has been downloaded to your server, you can run it using the run option: ``.shell docker run <имя>

To view the downloaded images, type ``:
``.shell
docker images

You will see a similar result: ``.shell [root@kvmde54-19861 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 602e111c06b6 12 days ago 127MB centos latest 470671670cac 3 months ago 237MB hello-world latest bf756fb1ae65 4 months ago 13.3kB

**Starting the Docker container**

The hello-world container is an example of a container that starts and finishes after displaying a test message. Containers are similar to virtual machines, but are less demanding on resources.

The combination of the parameters **-i** and **-t** gives interactive access to the container's command processor:
``.shell
docker run -it centos

The command line will show that we are working in the container: ``.shell [root@dadc89ffcb35 /]#

Next, you can run commands inside the container 

Let's install MariaDB:
``.shell
yum install mariadb-server

Changes that are executed inside a container apply only to that container. To exit the container, type exit.

Manage Docker containers

Once you start using Docker, you'll have many active and inactive containers on your machine To view active containers, type the command: ``.shell docker ps

``.shell
[root@kvmde54-19861 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

To see the active and inactive containers, run docker ps with parameter -a: ``.shell docker ps -a

``.shell
[root@kvmde54-19861 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dadc89ffcb35 centos "/bin/bash" 13 minutes ago Exited (0) 52 seconds ago nifty_jang
9ff36f3a478b hello-world "/hello" 45 minutes ago Exited (0) 45 minutes ago naughty_shirley

To see the last of the created containers, specify parameter -l: ``.shell docker ps -l

``.shell
[root@kvmde54-19861 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dadc89ffcb35 centos "/bin/bash" 14 minutes ago Exited (0) About a minute ago nifty_jang

To start a stopped container, use the command docker start and specify the container ID or name ``.shell docker start dadc89ffcb35

You can now use **docker ps** to see its status:
``.shell
[root@kvmde54-19861 ~]# docker ps
IMAGE ID COMMAND CREATED STATUS PORTS NAMES
dadc89ffcb35 centos "/bin/bash" 23 minutes ago Up 38 seconds nifty_jang

To stop a running container, use the command docker stop and specify the container id or its name ``.shell docker stop nifty_jang

If you don't need the container anymore, delete it with the command **docker rm** specifying the container ID or name. To find the container ID or name, use the command **docker ps -a**. The container can then be deleted.
``.shell
docker rm nifty_jang

Saving changes to the container in a Docker image

When you run the container from a Docker image, you can create, modify, and delete files, just as you would on a virtual machine.

After installing MariaDB in a Centos container, you will have a container running from the image, but it will be different from the image used to create it. However, you may need such a MariaDB container as the basis for future images.

Then confirm the changes to the new Docker image with the following command ``.shell docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name

Parameter **-m** allows you to specify a confirmation message, parameter **-a** allows you to specify an author. The container_id identifier is the identifier that was used before. If you have not created additional repositories in Docker Hub, the repository name is usually your user name in Docker Hub.

For example, for the user test and the container ID dadc89ffcb35, the command would look like this:
``.shell
docker commit -m "added mariadb-server" -a "test" dadc89ffcb35 test/centos-mariadb

After confirming (commit) the image, the new image is saved locally on your computer

If you browse the list of Docker images, you will find both the new image and the original image on which it was based: ``.shell docker images

You will see a similar result:
``.shell
[root@kvmde54-19861 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/centos-mariadb latest bd8ad6193efb 29 seconds ago 493MB
nginx latest 602e111c06b6 12 days ago 127MB
centos latest 470671670cac 3 months ago 237MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB

In this example, centos-mariadb is a new image created from an existing centos image from the Docker Hub.</имя></имя>

Need help?Our engineers will help you free of charge with any question in minutesContact us