Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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...]
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 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
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

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.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x