42

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.

5 Answers 5

62

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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.
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). :-)
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.
6

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

4 Comments

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}')
omitting the useless grep, it's actuall a quite useful answer. hard to upvote it, though, because of the bad grep usage.
Nice to win something every now and again
or if u want to kill all running ones use docker kill $(docker ps -q)
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

2 Comments

how is this distinguished from docker ps -a -q?
There's a good response on that here @NicholasSaunders
2

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.

1 Comment

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.