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 stopbecause it doesnโt wait for the process to exit.
Basic Syntax
docker kill [OPTIONS] CONTAINER [CONTAINER...]
Code language: CSS (css)
Options:
--signalโ Specify the signal to send to the container (SIGKILL,SIGTERM,SIGHUP, etc.).
Difference Between docker kill and docker stop
docker kill | docker stop |
|---|---|
Sends SIGKILL by default | Sends SIGTERM, then SIGKILL after a timeout |
| Immediate and forceful stop | Graceful shutdown with time for cleanup |
| Faster | Slower but safer for running processes |
| Suitable for non-responsive containers | Suitable 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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
5. Kill All Running Containers
docker kill $(docker ps -q)
Code language: JavaScript (javascript)
This command kills all currently running containers.
6. Check the Exit Status of a Killed Container
docker inspect --format='{{.State.ExitCode}}' my_container
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
10. Monitor and Kill Non-Responsive Containers
if [ "$(docker inspect --format='{{.State.Status}}' my_container)" == "running" ]; then
docker kill my_container
fi
Code language: CSS (css)
This checks if the container is still running before killing it.
List of Common docker kill Commands
| Command | Description |
|---|---|
docker kill my_container | Kill a running container with SIGKILL |
docker kill --signal="SIGTERM" my_container | Send SIGTERM to a running container |
docker kill container1 container2 | Kill multiple containers |
docker kill --signal="SIGHUP" my_app_container | Send SIGHUP to reload configuration |
docker kill $(docker ps -q) | Kill all running containers |
docker inspect --format='{{.State.ExitCode}}' my_container | Check the exit status of a killed container |
Best Practices for Using docker kill:
- Use
docker stopfor graceful shutdowns unless immediate termination is required. - Send custom signals for applications that support them (e.g.,
SIGHUPfor reloads). - Avoid data loss by ensuring the containerโs process does not have unsaved data when killing it.
- Monitor exit codes to understand why a container was killed (
137indicatesSIGKILL).
Common Errors and Solutions
- “Cannot kill container: No such container”
โ Ensure the container is running. Usedocker psto check. - “Permission denied”
โ You may need elevated permissions (sudo) to kill certain containers. - 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 withSIGKILL.143โ The container was stopped withSIGTERM.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals