Kill a Docker Container
Asked Answered
S

5

38

I've set up a Docker container for my node app and ran it using

docker run -p 4500:4500 my_node_app

It launched pm2 in no-daemon mode. CTRL+C and exit don't work. I've tried docker stop my_node_app in another terminal window but to no avail. Appreciate any help.

Shere answered 25/6, 2018 at 2:29 Comment(0)
P
53

You will be able to see currently running docker containers using below command.

docker ps

Then copy the CONTAINER ID of the running container and execute the following command

docker stop <container_id>

Please replace with a real value.

Peking answered 25/6, 2018 at 2:35 Comment(5)
I've run docker ps in another terminal window and receive the message: Cannot connect to the Docker daemon. Is the docker daemon running on this host?Shere
I figured it out. I had to run eval $(docker-machine env dev2) in the new terminal window before I could use the commands. Thanks for your help.Shere
You should update your original question with this information. In that case you'll need to turn on the docker daemon -- try running dockerd in the console.Hakenkreuz
I thought the answer below that used docker kill was the correct answer until I read superuser.com/questions/756999/…, so it is worth pointing out that stop will actually kill the container. It just kills it by trying to be polite (at first). :-)Gerardogeratology
When you execute stop command, it stops the container gracefully. Kill command will do the same thing forcefully. So always try to use stop command.Peking
H
5

Do docker container ls to find the container name if you don't know it already, then docker kill container_name.

Source: Docker documentation

Hakenkreuz answered 25/6, 2018 at 2:33 Comment(2)
how is this distinguished from docker ps -a -q?Shape
There's a good response on that here @NicholasSaundersHakenkreuz
L
5

You can try this, pretty easy script

docker container kill $(docker ps | awk '/lookup_value/ {print $1}')

Explained

List containers in tabular structure

docker ps

Result

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
763e1a00808e        7bb2586065cd        "docker-entrypoint.s…"   2 months ago        Up 2 weeks          3306/tcp, 33060/tcp   mysql_1
763e1a00999b        7bb2586065cd        "docker-entrypoint.s…"   1 months ago        Up 2 weeks          3307/tcp, 33061/tcp   mysql_2

Isolate your container

awk /mysql_1/

Result

763e1a00808e        7bb2586065cd        "docker-entrypoint.s…"   2 months ago        Up 2 weeks          3306/tcp, 33060/tcp   mysql_1

Isolate first tab value which is the container ID

awk ' /mysql_1/ {print $1}'

Result

763e1a00808e

So in conclusion this will isolate and send the kill or stop command the container id matching the image name

Lollis answered 25/6, 2019 at 11:50 Comment(4)
You win the useless use of grep award. You can save your fork by changing this to docker container kill $(docker ps | awk '/image_name/ {print $1}')Audi
omitting the useless grep, it's actuall a quite useful answer. hard to upvote it, though, because of the bad grep usage.Shape
Nice to win something every now and againLollis
or if u want to kill all running ones use docker kill $(docker ps -q)Blurb
K
0
  1. “Docker container ls “ to view existing containers
  2. “Docker container kill container_id” to terminate forcefully or “docker container stop container_id” for graceful termination
Komsomolsk answered 11/8, 2021 at 6:15 Comment(0)
N
0

alternative aws

docker kill $(docker ps -q)

docker ps -q get id all containers

The command docker kill $(docker ps -q) uses to stop running containers.

Breaking it down:

docker ps -q: This part of the command is used to list the IDs of the currently running Docker containers. The -q option stands for "quiet" and is used to only display the container IDs without any additional information.

$(docker ps -q): This command substitution syntax ($(...)) takes the output of docker ps -q (which is a list of container IDs) and substitutes it into the overall command.

docker kill: sending a signal to stop a running container. It is equivalent to running docker stop, but with a default signal of SIGKILL, which forcefully terminates the container.

Putting it all together, docker kill $(docker ps -q) stops all running Docker containers by obtaining their IDs from the output of docker ps -q and then using docker kill to send the stop signal to each identified container.

This command is useful when you want to quickly stop all running containers on your Docker host. Keep in mind that this forcefully terminates the containers, and any unsaved data or state in those containers may be lost. Use it with caution, especially in a production environment.

Nutmeg answered 21/11, 2023 at 8:47 Comment(1)
Please add some explanation to your answer such that others can learn from it. Why should killing all containers be a better solution than the ones given in the other answers?Enzyme

© 2022 - 2024 — McMap. All rights reserved.