问题
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.
回答1:
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.
回答2:
Do docker container ls
to find the container name if you don't know it already, then docker kill container_name
.
Source: Docker documentation
回答3:
You can try this, pretty easy script
docker container kill $(docker ps | grep "image_name" | awk '{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
grep "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 '{print $1}'
Result
763e1a00808e
So in conclusion this will isolate and send the kill or stop command the container id matching the image name
来源:https://stackoverflow.com/questions/51015597/kill-a-docker-container