CodeBlanks Logo Icon CodeBlanks

docker stop and rm

Learn to stop and remove containers.

docker stop and rm

docker stop first sends SIGTERM to the main process, giving the application 10 seconds to shut down; if time runs out it kills it with SIGKILL. A stopped container stays on disk; docker rm deletes it:

docker stop web
docker rm web
docker rm -f cache        # force stop and remove even if running

docker start web restarts a stopped container with the same settings.

The difference between stop and kill

docker kill sends SIGKILL without waiting; the application can't close open files or save half-done work. That is why stop is normally preferred. For applications that take long to shut down, the timeout is increased:

docker stop -t 30 db     # give the database 30 seconds
docker kill web          # kill immediately
docker start web         # start again

Practice Graceful shutdown

Write three commands: stop the db container allowing 30 seconds; remove it; then force-remove the cache container even if it is running.

Starter code

terminal
# buraya yaz

Sample solution

terminal
docker stop -t 30 db
docker rm db
docker rm -f cache

Expected output

db
db
cache

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 By default, how many seconds does docker stop give the application to shut down?

  • 60
  • 1
  • 10 Correct answer
  • 0

Explanation: It can be changed with -t: docker stop -t 30 web.

2 Force-remove a running container.

Question
docker rm ____ cache
Answer
docker rm -f cache

Explanation: -f stops it first, then removes it.

3 Match the commands.

Term Meaning
docker stop SIGTERM, then SIGKILL
docker kill Immediate SIGKILL
docker rm Removes a stopped container
docker start Restarts a stopped one

All lessons in Beginner