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 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
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
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 stop
for graceful shutdowns unless immediate termination is required. - Send custom signals for applications that support them (e.g.,
SIGHUP
for 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 (
137
indicatesSIGKILL
).
Common Errors and Solutions
- “Cannot kill container: No such container”
→ Ensure the container is running. Usedocker ps
to 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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND