33

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.

Coder_Nick
  • 781
  • 1
  • 8
  • 25

4 Answers4

45

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.

srimaln91
  • 1,176
  • 10
  • 21
  • 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? – Coder_Nick Jun 25 '18 at 02:46
  • 1
    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. – Coder_Nick Jun 25 '18 at 02:57
  • 1
    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. – Hugo Jun 25 '18 at 02:58
  • I thought the answer below that used `docker kill` was the correct answer until I read https://superuser.com/questions/756999/whats-the-difference-between-docker-stop-and-docker-kill, so it is worth pointing out that *stop* will actually kill the container. It just kills it by trying to be polite (at first). :-) – PatS Mar 12 '19 at 21:57
  • 1
    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. – srimaln91 Mar 13 '19 at 04:48
4

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

Illegal Operator
  • 656
  • 6
  • 14
3

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

Source: Docker documentation

Hugo
  • 546
  • 5
  • 12
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
asapmk
  • 1