MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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

Related Posts

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

How to Access a Running Docker Container Shell: docker exec vs docker attach

Need a shell inside a running container that is already in detached mode? This updated guide shows the correct workflow, compares docker exec and docker attach, and explains when SSH does and does not make sense.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More

Top 5 DevOps Tools To Learn Before Writing Your Dissertation

Writing a technical dissertation is rarely just an exercise in reading sources and formatting chapters. On paper, that sounds like the job. In reality, students in computer…

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