Cannot stop or restart a docker container

I couldn't locate boot2docker in my machine. So, I came up with something that worked for me.

$ sudo systemctl restart docker.socket docker.service
$ docker rm -f <container id>

Check if it helps you as well.


Worth knowing:

If you are running an ENTRYPOINT script ... the script will work with the shebang

#!/bin/bash -x

But will stop the container from stopping with

#!/bin/bash -xe

That looks like docker/docker/issues/12738, seen with docker 1.6 or 1.7:

Some container fail to stop properly, and the restart

We are seeing this issue a lot in our users hosts when they upgraded from 1.5.0 to 1.6.0.
After the upgrade, some containers cannot be stopped (giving 500 Server Error: Internal Server Error ("Cannot stop container xxxxx: [2] Container does not exist: container destroyed")) or forced destroyed (giving 500 Server Error: Internal Server Error ("Could not kill running container, cannot remove - [2] Container does not exist: container destroyed")). The processes are still running on the host.
Sometimes, it works after restarting the docker daemon.

There are some workarounds:

I've tried all remote API calls for that unkillable container and here are results:

  • json, stats, changes, top, logs returned valid responses
  • stop, pause, wait, kill reported 404 (!)

After I finished with remote API, I double-checked docker ps (the container was still there), but then I retried docker kill and it worked! The container got killed and I could remove it.

Or:

What worked was to restart boot2docker on my host. Then docker rm -f

$ boot2docker stop
$ boot2docker start
$ docker rm -f 1f061139ba04

All the docker: start | restart | stop | rm --force | kill commands may not work if the container is stuck. You can always restart the docker daemon. However, if you have other containers running, that may not be the option. What you can do is:

ps aux | grep <<container id>> | awk '{print $1 $2}'

The output contains:

<<user>><<process id>>

Then kill the process associated with the container like so:

sudo kill -9 <<process id from above command>>

That will kill the container and you can start a new container with the right image.

Tags:

Docker