Learn which Docker command stops a container gracefully. Understand the difference between docker stop and docker kill commands for managing Docker containers.
Table of Contents
Question
Which of the following docker commands allows us to stop a docker container?
A. docker stop container_name
B. docker kill it Container_name
C. docker stop it container_name
D. docker purge it container_name
Answer
A. docker stop container_name
Explanation
The docker stop command is used to gracefully stop a running Docker container. When executed, it sends a SIGTERM signal to the main process inside the container, allowing it to perform cleanup tasks such as saving state or closing connections. If the container does not shut down within the default timeout period (10 seconds), Docker then sends a SIGKILL signal to forcefully terminate the container.
Key Features of docker stop
- Graceful shutdown with cleanup.
- Allows specifying a custom timeout using the –time or -t option.
- Can stop multiple containers simultaneously by listing their names or IDs.
Example Usage
docker stop my_container
In contrast
- The docker kill command (e.g., option B) sends only a SIGKILL signal, immediately terminating the container without cleanup. This is typically used as a last resort when docker stop fails.
- Options C and D (docker stop it container_name and docker purge it container_name) are invalid commands as they do not exist in Docker’s CLI syntax.
Why Not Other Options?
Option A (docker stop container_name): Correct. It is the standard command for stopping containers gracefully.
Option B (docker kill it Container_name): Incorrect. While docker kill can stop containers, it skips the graceful shutdown process, making it less ideal unless necessary.
Option C (docker stop it container_name): Incorrect. The syntax is invalid; “it” is not part of any recognized Docker command.
Option D (docker purge it container_name): Incorrect. There is no docker purge command in Docker.
Additional Notes
Use docker ps to list running containers and identify their names or IDs before stopping them.
To stop all running containers at once, you can use:
docker stop $(docker ps -q)
Always prefer docker stop over docker kill to avoid potential data loss or corruption.
Introduction to DevOps certification exam assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the Introduction to DevOps exam and earn Introduction to DevOps certification.