🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker kill with examples

Here’s a complete tutorial on docker kill, including its purpose, how it works, and a comprehensive list of examples.


What is docker kill?

docker kill is a Docker command used to immediately stop a running container by sending a specified signal (default is SIGKILL). It’s different from docker stop, which shuts down the container gracefully by sending SIGTERM first and giving the process time to terminate.

Key Features:

  • Forcefully stops a running container.
  • Sends a custom signal to the main process (e.g., SIGTERM, SIGUSR1).
  • Useful for emergency stops or when a container fails to respond to docker stop.
  • Faster than docker stop because it doesn’t wait for the process to exit.

Basic Syntax

docker kill [OPTIONS] CONTAINER [CONTAINER...]

Options:

  • --signal → Specify the signal to send to the container (SIGKILL, SIGTERM, SIGHUP, etc.).

Difference Between docker kill and docker stop

docker killdocker stop
Sends SIGKILL by defaultSends SIGTERM, then SIGKILL after a timeout
Immediate and forceful stopGraceful shutdown with time for cleanup
FasterSlower but safer for running processes
Suitable for non-responsive containersSuitable for normal shutdowns

Examples of docker kill

1. Kill a Running Container

docker kill my_container

This immediately stops my_container by sending the SIGKILL signal.


2. Kill Multiple Containers

docker kill container1 container2 container3

This kills multiple containers at once.


3. Use a Custom Signal (SIGTERM)

docker kill --signal="SIGTERM" my_container

This sends SIGTERM to my_container, allowing it to shut down more gracefully.


4. Use SIGUSR1 to Trigger Custom Behavior

If your containerized application handles custom signals, you can send SIGUSR1:

docker kill --signal="SIGUSR1" my_container

5. Kill All Running Containers

docker kill $(docker ps -q)

This command kills all currently running containers.


6. Check the Exit Status of a Killed Container

docker inspect --format='{{.State.ExitCode}}' my_container

Returns the exit code (137 for SIGKILL).


7. Use docker kill in a Shell Script

#!/bin/bash
for container in $(docker ps -q); do
  docker kill $container
done

This script kills all running containers one by one.


8. Kill a Container Running an Interactive Process

If you have a container running a process that’s stuck, like an interactive bash shell:

docker kill my_interactive_container

9. Send SIGHUP to Reload Configuration

If your application supports reloading its configuration with SIGHUP, you can trigger it:

docker kill --signal="SIGHUP" my_app_container

10. Monitor and Kill Non-Responsive Containers

if [ "$(docker inspect --format='{{.State.Status}}' my_container)" == "running" ]; then
  docker kill my_container
fi

This checks if the container is still running before killing it.


List of Common docker kill Commands

CommandDescription
docker kill my_containerKill a running container with SIGKILL
docker kill --signal="SIGTERM" my_containerSend SIGTERM to a running container
docker kill container1 container2Kill multiple containers
docker kill --signal="SIGHUP" my_app_containerSend SIGHUP to reload configuration
docker kill $(docker ps -q)Kill all running containers
docker inspect --format='{{.State.ExitCode}}' my_containerCheck the exit status of a killed container

Best Practices for Using docker kill:

  1. Use docker stop for graceful shutdowns unless immediate termination is required.
  2. Send custom signals for applications that support them (e.g., SIGHUP for reloads).
  3. Avoid data loss by ensuring the container’s process does not have unsaved data when killing it.
  4. Monitor exit codes to understand why a container was killed (137 indicates SIGKILL).

Common Errors and Solutions

  1. “Cannot kill container: No such container”
    → Ensure the container is running. Use docker ps to check.
  2. “Permission denied”
    → You may need elevated permissions (sudo) to kill certain containers.
  3. Container keeps restarting
    → If the container is configured with a restart policy (--restart always), it will restart after being killed. Stop it with: docker stop my_container docker rm my_container

Exit Codes Explained

  • 137 → The container was killed with SIGKILL.
  • 143 → The container was stopped with SIGTERM.

Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x