Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker in Production Graceful Shutdown and Signal Handling STOP vs KILL stopsignal
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-in-production-graceful-shutdown-and-signal-handling-stop-vs-kill-stopsignal

Docker Docker Docker in Production Graceful Shutdown and Signal Handling STOP vs KILL stopsignal

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Gracefully shutting down containers without losing data or causing data corruption is achieved by using the docker stop command instead of docker kill.

2. Step-by-Step

  1. Run a container in detached mode: docker run -d --name my-container my-image
  2. ⚠️ Check the container's status: docker ps -a
  3. Stop the container: docker stop my-container
  4. Check the container's status again: docker ps -a
  5. ⚠️ This deletes all stopped containers: docker rm $(docker ps -aq -f status=exited)
  6. Clean up: docker rm my-container

3. Real Commands with Examples


Example 1: Stopping a container

  • Purpose: Stop a running container
  • Command: docker stop my-container
  • Brief explanation of flags: No flags needed, but you can use -t to specify a timeout (e.g., docker stop -t 10 my-container to stop the container within 10 seconds).

Example 2: Stopping a container with a custom stop signal

  • Purpose: Stop a running container with a custom stop signal (e.g., SIGTERM)
  • Command: docker stop -s SIGTERM my-container
  • Brief explanation of flags: -s flag specifies the signal to send to the container.

Example 3: Stopping a container with a timeout

  • Purpose: Stop a running container within a specified timeout
  • Command: docker stop -t 10 my-container
  • Brief explanation of flags: -t flag specifies the timeout in seconds.

4. Common Errors


Error 1: Container not found

  • Error message or symptom: Error response from daemon: No such container: my-container
  • Why it happens: The container does not exist or has been deleted.
  • One-line fix: docker run -d --name my-container my-image (start the container again).

Error 2: Container is not running

  • Error message or symptom: Error response from daemon: Cannot stop container my-container: Container my-container is not running
  • Why it happens: The container is not running.
  • One-line fix: docker start my-container (start the container).

Error 3: Invalid signal

  • Error message or symptom: Error response from daemon: Invalid signal: SIGUSR1
  • Why it happens: The signal is not valid.
  • One-line fix: docker stop -s SIGTERM my-container (use a valid signal).

5. Quick Checks


Scenario 1: Container is running

  • Situation: You want to check if a container is running.
  • Command: docker ps -a
  • What a correct response looks like: The container's status should be "Up" or "Running".

Scenario 2: Container is stopped

  • Situation: You want to check if a container is stopped.
  • Command: docker ps -a
  • What a correct response looks like: The container's status should be "Exited" or "Stopped".

Scenario 3: Container is deleted

  • Situation: You want to check if a container has been deleted.
  • Command: docker ps -a
  • What a correct response looks like: The container should not be listed.

6. Last-Minute Reference

docker stop vs docker kill: Use docker stop to stop a container gracefully, and docker kill to force-kill a container.
• ⚠️ docker rm deletes all stopped containers: Use docker rm -f to force-remove a container.
docker stop -t specifies a timeout: Use docker stop -t 10 to stop a container within 10 seconds.
docker stop -s specifies a custom stop signal: Use docker stop -s SIGTERM to stop a container with a custom signal.
• ⚠️ docker rm does not remove volumes: Use docker volume rm to remove volumes.
docker stop does not remove networks: Use docker network rm to remove networks.
• ⚠️ docker rm does not remove images: Use docker rmi to remove images.
docker stop does not remove containers with mounted volumes: Use docker rm -f to force-remove containers with mounted volumes.
docker stop does not remove containers with running processes: Use docker rm -f to force-remove containers with running processes.
• ⚠️ docker stop does not remove containers with attached shells: Use docker detach to detach from the shell before stopping the container.




ADVERTISEMENT