94

I cannot stop, remove or kill my docker container. Commands given below with their respective error messages:

1. docker stop <container-id>
2. docker kill <container-id>
3. docker rm <container-id>

I get

1. Error response from daemon: cannot stop container: <container-id>: tried to kill container, but did not receive an exit event
2. Error response from daemon: cannot kill container: <container-id>: tried to kill container, but did not receive an exit event
3. Error response from daemon: You cannot remove a running container <container-id>. Stop the container before attempting removal or force remove

Same error messages if i prefix everything with sudo and also the same messages if I run all of commands above with --force. How do I solve this? It seems like I can't stop, kill or remove the container because it does not "receive an exit event". Nothing here helps: Error response from daemon: cannot stop container - signaling init process caused "permission denied" .

9
  • 7
    Try restarting docker service by running systemctl restart docker if it works
    – Saeed
    Commented Dec 21, 2021 at 11:56
  • 4
    Restarting fixed it. Thanks. But there is no other way?
    – lapurita
    Commented Dec 21, 2021 at 12:23
  • Glad it worked. Not sure why, but maybe there was something wrong within docker containers, and restarting docker worked. Now try docker stop container_id to see if now it's fixed or you still have the issue.
    – Saeed
    Commented Dec 21, 2021 at 13:13
  • 18
    All of the answers more or less say "kill docker altogether, including all running containers and docker itself". But isn't the point of the question, to find out how to force kill a running container without having to restart docker itself (or stop other containers)?
    – ugliest
    Commented Jul 8, 2022 at 18:44
  • 1
    I intermittently see a similar problem, mostly with a mysql container that 'hangs', while nearly all of the other contains I have stop normally. Easily resolved by restarting the docker engine, but it would be nice to know why it won't stop in the first place so it is not an issue at all ?
    – SScotti
    Commented Oct 28, 2022 at 7:38

5 Answers 5

114

I used wilon's answer from https://forums.docker.com/t/restart-docker-from-command-line/9420/2

I ran killall Docker && open /Applications/Docker.app

Once that was done, I ran docker-compose down and all containers stopped as expected.

1
  • After the command, it showed an error on Mac 12.6 - ~"Cannot exit. Exit code: 0." and opened the Docker app window with an infinite message "stopping...". Reopened the app manually and the app showed and immediately hid an error with additional infinite "waiting..." message + "Engine null" in the left bottom. Manually restarted the app again and it worked as a charm (+ was able to stop some containers which ran automatically). Oh dear... never ever had such issues on Linux (e.g. Ubuntu 20.04). What's the main reason? Commented Oct 11, 2022 at 17:42
23

Some of the solutions that you can try:

  1. Restart the docker service: sudo systemctl restart docker.service
  2. Restart the Host Machine
  3. Enter inside the container docker exec -it ContainerName /bin/bash and then Kill the container kill 1
  4. You can disable the apparmor service so first check the status sudo apparmor_status then disable it sudo systemctl disable apparmor then teardown the apparmor sudo service apparmor teardown. Now check the status again sudo apparmor_status
17

If you cannot connect/attach to your container using docker exec -it ContainerName /bin/bash (or /bin/sh), you might look to kill the container's process directly.

Find the pid, based on the container id. If it's started with compose the id might be the start of the longer internal id.

ps auxw | grep $(docker container ls | grep containername | awk '{print $1}') | awk '{print $2}'
kill -9 12345678

Chances are almost certain that the process cannot be killed (as docker tried to kill it), due to other reasons, like hanging mounts or other networking issues. kill -9 is a sigkill, so if that doesn't work, you have a bigger issue, which often means that a server restart is your only solution.

1
  • 1
    Saves having to grep twice: ps aux | grep $(docker container ls -q --filter 'name=$container_name') | awk '{print $2}'
    – alimbada
    Commented Feb 4 at 17:57
3

I had the same issue.

I was running docker-desktop as well. I restarted docker and then stoped the container from docker-desktop. Finally was able to delete from from docker-desktop.

-2

I had the same when running docker-compose down for one of my containers, everything worked fine again after running docker-compose restart.

I didn't need to restart whole Docker service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.