peter
peter

Reputation: 15139

Cannot stop or restart a docker container

When trying to stop or restart a docker container I'm getting the following error message:

$ docker restart 5ba0a86f36ea
Error response from daemon: Cannot restart container 5ba0a86f36ea: [2] Container does not exist: container destroyed
Error: failed to restart containers: [5ba0a86f36ea]

But when I run

$ docker logs -f 5ba0a86f36ea

I can see the logs, so obviously the container does exist. Any ideas?

Edit:

sorry, I forgot to mention this:

When I run docker ps -a I see the container as up and running. However the application inside it is malfunctioning so I want to restart it, or just get a fresh version of that application online. But when I can't stop and remove the container, I also can't get a new application up and running, which would be listening to the same port.

Upvotes: 174

Views: 424714

Answers (15)

moritzgvt
moritzgvt

Reputation: 444

If you're on a Mac and try this via Terminal: Use killall Docker to quit Docker.

Restart it in the Applications folder or with open /Applications/Docker.app.

Subsequently you can run a docker rm <id> for the concerned container.

EDIT:

A faster and much cooler command for opening Docker from command line:

open -a Docker.app

Upvotes: 10

Abhishek Kashyap
Abhishek Kashyap

Reputation: 3648

I came up with this, which worked for me:

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

You may check if it helps you.

Although, it should be your last resort for critical systems, because restarting docker socket and services while you have running containers have some potential complications. Some of them are as follows:

  • Loss of Logs: You might loose some logs during startup.
  • Orphaned Processes: In some rare cases, restarting Docker might leave behind orphaned container processes. These can consume resources and might need to be manually killed.
  • Potential for Data Loss: In very rare cases, there might be potential for data loss, especially if containers were in the middle of write operations when Docker was restarted.

Upvotes: 228

Dmitriy
Dmitriy

Reputation: 551

If you're on Ubuntu, make sure docker-compose isn't installed as a snap. This will cause all kinds of random issues, including the above.

Remove the snap:

sudo snap remove docker-compose

And install manually from the compose repository:

Docker compose installation instruction

Upvotes: 0

Sardar Faisal
Sardar Faisal

Reputation: 671

Ubuntu Stop the container by using its system process ID. Get the main process ID using:

docker inspect -f '{{.State.Pid}}' container-id

This will return an id as ´25430´. Kill this with the command

sudo kill -9 25430

Upvotes: 6

Adrian C
Adrian C

Reputation: 101

i forgot that i had made the container start as a system service.
so if i stopped or killed the container, the service would bring it back.

if you are using systemctl, you can list all the running services with systemctl | grep running and find the name of the service.

then use sudo systemctl disable <your_service_name> to stop it.

Upvotes: 2

蔡火胜
蔡火胜

Reputation: 69

Sometimes this is caused by problem of the docker daemon. I solved the problem by restarting the docker service. On Linux:

systemctl restart docker

Upvotes: -1

Panda
Panda

Reputation: 17

In my case, docker rm $(docker ps -aq) works for me.

Upvotes: -4

Jerinaw
Jerinaw

Reputation: 5529

For anyone on a Mac who has Docker Desktop installed. I was able to just click the tray icon and say Restart Docker. Once it restarted was able to delete the containers.

Upvotes: 19

Artem Kozlenkov
Artem Kozlenkov

Reputation: 1264

in my case, i couldn't delete container created with nomad jobs, there's no output for the docker logs <ContainerID> and, in general, it looks like frozen.

until now the solution is: sudo service docker restart, may someone suggest better one?

Upvotes: 2

ninohead
ninohead

Reputation: 364

Check if there is any zombie process using "top" command.

docker ps | grep <<container name>> 

Get the container id.

ps -ef | grep <<container id>>

ps -ef|grep defunct | grep java

And kill the container by Parent PID .

Upvotes: 12

metalhead
metalhead

Reputation: 567

I had the same problem on a windows host machine and none of the other options here worked for me. I ended up just needing to delete the physical container folder, which was located here:

C:\ProgramData\Docker\containers\[container guid]

I had stopped the docker service first just to be safe and when I restarted it, the broken containers were now gone and I was able to create new ones. I suspect the same will work on a linux host machine, but I do not know where the container folders are kept on that OS.

Upvotes: 6

FabianoLothor
FabianoLothor

Reputation: 2985

Enjoy

sudo aa-remove-unknown

This is what worked for me.

Upvotes: 14

Teddy Belay
Teddy Belay

Reputation: 1735

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.

Upvotes: 117

danday74
danday74

Reputation: 57205

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

Upvotes: 16

VonC
VonC

Reputation: 1328282

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

Upvotes: 46

Related Questions