Docker stop container:How to work with it and its advantages

Docker stop  container:How to work with it and its advantages
Techiio-author
Written by Nilima PaulFebruary 15, 2022
9 min read
Docker
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

In this blog post we will discuss about Docker stop container.How to work with it,its examples and its advantages.

Introduction of Docker stop container:

Docker stop compartment is utilized to stop the Docker holders. We can stop at least one Docker holders simultaneously by determining the name or compartment ID. Whenever we run this order, it initially sends SIGTERM to the cycle running inside the compartment and hangs tight for quite a while and it neglects to stop the interaction inside the holder then it conveys a SIGKILL message to kill the cycle.

Syntax:

docker stop [OPTIONS] CONTAINER [CONTAINER…]

Options:

We only have one option to specify in this command i.e. ‘–time’ or ‘-t’.

–time, -t: we can control the grace period using this option which means we can specify when Docker daemon should send SIGKILL signal it SIGTERM does not work.

docker stop --help
How does stop container command work in Docker:

Whenever we run the order 'docker prevent holder' from the Docker CLI, Docker CLI settles on an API decision to Docker daemon and Docker daemon conveys a SIGTERM message to the cycle running inside the compartment and sits tight for 10 seconds of course and conveys the SIGKILL message to kill the interaction. On the off chance that the order executes effectively, it shows holder ID as a result on STDOUT for example terminal.

Examples of Docker stop container:

Let’s understand the command with the below examples. Create a few nginx containers and let’s play with it. We have below containers running right now: –

docker ps

Simply stop a single container using its container ID: –

Syntax:

docker stop <container ID>

Example:

docker stop d207f7679ce5

Let’s stop a single container using its name: –

Syntax:

docker stop <container_name>

Example:

docker stop jovial_panini

Now, stop more than one container in the same command using its container ID: –

Syntax:

docker stop <container1 ID><container2 ID> ......

Example:

docker stop 034cb3abe35c11967042830d

Let’s do the same thing using container name as below: –

Syntax:

docker stop <container1 name><container2 name> .....

Example:

docker stop nifty_bellelated_dubinsky

Let’s say, we have multiple containers with names starting with the ‘web’ and we want to stop all the containers starting with the name ‘web’. We can use the ‘docker ps’ command with the ‘–filter’ and the ‘–format’ options as below: –

docker stop $(docker ps --filter "name=web" --format="{{.ID}}")

Explanation: In the above model, we can see that there are 3 compartments named 'web1', 'web2' and 'web3' are running. We need to utilize the 'sift' choice to channel through the holders with the name beginning with 'web' and afterward just rundown the compartment ID of the compartments so the 'docker stop' order will get just the holder ID to chip away at else entire result will be shipped off the 'docker stop' order and order will come up short. We can likewise utilize the compartment name rather than ID as beneath: -

docker stop $(docker ps --filter "name=web" --format="{{.Names}}")

We can also use the ‘-a’ flag to output all containers whether the containers are running or not, however, we are stopping the container here so that is not required because we can stop the running container only. We can use the ‘-a’ flag while starting multiple containers.

We can use the same method as above to stop the container running with a specific Docker image. Create few containers with same Docker image and try to delete those at once using below command: –

docker stop $(docker ps --filter "ancestor=nginx" --format="{{.Names}}")

Explanation: In the above example, we can see that we just need to change the filter. By using the different filters, we can stop containers as per our requirement, however, the format must be the ‘Names’ or the ‘ID’.

We can control the grace period before sending the SIGKILL signal to the process running inside the container using the ‘–time’ or ‘-t’ options as below: –

docker stop -t 20 web2

docker stop -t 20 web1

Explanation: In the above example, we don’t see any difference because the process running under these containers is stopped gracefully. We can use this if any process inside the container takes more than 10 secs to stop so that the docker daemon does not try to kill the process.

Note: We can use this option while stopping multiple containers or querying the Docker daemon as well.

Advantages of Docker stop container:

A portion of the benefits are given underneath:

  1. We can utilize this order to nimbly stop the interaction running inside the holder and we can begin the compartment later utilizing the 'docker start' order.
  2. We can stop at least one holders by determining the compartment name or ID in a similar order.
  3. We can likewise question or channel the holder according to our necessity and utilize the stop order to shut down those compartments.
Conclusion :

The 'docker stop' order is not difficult to recall the order to stop the compartment. There is another order that is somewhat greater yet does likewise and that order is 'docker compartment stop <container name or ID>'. We can utilize both of these orders according to our solace.

Docker
DockerStopContainer
DevOps
kubernetes
Docker swarm
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance