67

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" .

lapurita
  • 945
  • 1
  • 5
  • 11
  • 4
    Try restarting `docker` service by running `systemctl restart docker` if it works – Saeed Dec 21 '21 at 11:56
  • 3
    Restarting fixed it. Thanks. But there is no other way? – lapurita Dec 21 '21 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 Dec 21 '21 at 13:13
  • 10
    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 Jul 08 '22 at 18:44
  • 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 Oct 28 '22 at 07:38
  • echo'ing @lapurita - restarting docker service worked for me too (to clear out zombie containers that wouldn't die) – Tim Mar 09 '23 at 15:41
  • @ugliest that's definitely what I'm trying to do! Restarting the entire Docker system (versus just killing a container) takes AGES and the particular container I'm using is very buggy (SQL Server) and often requires a restart (thanks Microsoft!). – Brian Birtle Mar 09 '23 at 23:08
  • Docker containers on mac seem to hang way more than they do on Linux. Not sure why everyone seems to like mac as a dev laptop unless they're strictly coming from a Windows environment. – Harlin Apr 20 '23 at 13:44
  • for windows (docker-desktop) restart of the engine helps as well – diman82 May 14 '23 at 20:57

5 Answers5

94

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.

Thor
  • 1,064
  • 8
  • 3
  • 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? – Artfaith Oct 11 '22 at 17:42
20

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
3

If you cannot connect to your container using docker exec -it ContainerName /bin/bash, you might look the 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.

Tim Chaubet
  • 498
  • 1
  • 7
  • 15
1

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.

Prosunjit Biswas
  • 935
  • 9
  • 16
-1

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.

WarioNeila86
  • 176
  • 2
  • 4