How to manage Docker containers on CentOS/RHEL 7.x

How to manage Docker containers on CentOS/RHEL 7.x

How to manage Docker containers on CentOS/RHEL 7.x

We have learnt “How to install Docker on CentOS 7.x” in last post and have even run your first container. Here’s
“How to manage Docker containers on CentOS/RHEL 7.x” and images through the command line. Now, we will learn in this post, so let’s start it.

How to install Docker on CentOS 7.x

Executing Docker Command Without Sudo (Optional)

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

Log out and log back in so that the group membership is refreshed.

Docker command line interface

We have installed Docker and it’s working fine. Now  the time to become familiar with the command line utility. Using docker consists of passing it a chain of options and subcommands followed by arguments. The syntax takes this form:

~]$ docker  [option] [command] [arguments]

To view all available subcommands, type:

~]$ docker
How to manage Docker containers on CentOS/RHEL 7.x

To view system-wide information, use:

~]$ docker info
How to manage Docker containers on CentOS/RHEL 7.x

Docker Images :

Docker containers are run from Docker images. A Docker image is a file, comprised of multiple layers, used to execute code in a Docker container. An image is essentially built from the instructions for a complete and executable version of an application, which relies on the host OS kernel.

What is a Docker Image?

  • A Docker Image is created by the sequence of commands written in a file called as Dockerfile.
  • When this Dockerfile is executed using a docker command it results into a Docker Image with a name which we specify.
  • When this Image is executed by “docker run” command it will by itself start whatever application or a service it has to start on its execution.

I would like to quote something from Docker word glossary :

“Definition of: image”

Docker images are the basis of containers. An Image is an ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime. An image typically contains a union of layered filesystems stacked on top of each other. An image does not have state and it never changes.”

In short, a Docker image is essentially a snapshot of a Docker container.

To check whether you can access and download images from Docker Hub, type:

~]$ docker run hello-world
How to manage Docker containers on CentOS/RHEL 7.x

The output, which should include the above information “Hello from Docker!”, should indicate that Docker in working correctly.

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 script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:

How to manage Docker containers on CentOS/RHEL 7.x

You can see the search results prints 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.

If we want to download the official build of CentOS 7, we can do that by using the image pull subcommand:

~]$ docker pull centos
How to manage Docker containers on CentOS/RHEL 7.x

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

After an image has been downloaded, you may then run a container using the downloaded image with the run subcommand. If an image has not been downloaded when docker is executed with the run subcommand, the Docker client will first download the image, then run a container using it:

Docker Containers

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 run centos

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.

The combination of the -i and -t switches gives you interactive shell access into the container. Below command will start a new container and provide you access to that container with /bin/bash shell

~]$ docker run -it centos

Your command prompt should change to reflect the fact that you’re now working inside the container.

To list active containers, type:

~]$ docker container ls

Note : If you don’t have any running containers the output will be empty.

To view both active and inactive containers, pass it the -a switch:

~]$ docker container ls -a

To exit from docker container type CTRL + P + Q. This will leave container running in the background an provide you host system console.

To delete one or more containers just copy the container ID (or IDs) from above and paste them after the container rm subcommand:

Note : you have to stop the containers before removing it.

~]$ docker container stop 58dbd51de59f
~]$ docker container rm 58dbd51de59f

Hopefully “
“How to manage Docker containers on CentOS/RHEL 7.x”, this will helps your understanding. More post related to Docker

Tagged , , . Bookmark the permalink.

12 Responses to How to manage Docker containers on CentOS/RHEL 7.x

  1. Pingback: where to buy ciprofloxacin

  2. Pingback: albuterol inhaler

  3. Pingback: naltrexone tablets india

  4. Pingback: costco tylenol 500 mg

  5. Pingback: chloroquine 500 mg

  6. Pingback: cheap cialis

  7. Pingback: hydroxychloroquine virus

  8. Pingback: sildenafil 100 mg coupons

  9. Pingback: viagra vs cialis

  10. Pingback: best time to take viagra 50mg

  11. Pingback: bimatoprost ophthalmic solution 0 03 %

  12. Pingback: generic cialis 20mg