Cannot stop or restart a docker container
Asked Answered
B

15

164

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.

Beason answered 12/7, 2015 at 8:19 Comment(3)
I would guess you can destroy a container but still have logs about it, when it has been destroyed. Otherwise your observation does not make sense.Adsorbate
Do you want to run a fresh container with all the data and changes wiped away or do you want to get like important files out of the one you used?Unschooled
This can happen if your docker image does not have proper process handling.Fervid
A
217

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.
Annulation answered 12/1, 2018 at 6:12 Comment(4)
I had to use the force option docker rm -f <container id>, but it works. Thank you.Myrtamyrtaceous
Thanks for the feedback @chaseisabelle. It make sense. I'll incorporate it in the answer.Annulation
On windows machine close Docker Desktop then stop Docker.Service from task manager. Then rerun Docker Desktop.Opener
Tried everything else... sudo systemctl restart docker.socket docker.service was the only thing that did it for me.Bracken
F
105

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.

Fourscore answered 16/9, 2019 at 19:35 Comment(6)
The outcome for me is kill: illegal process id: [USER][PROCESS_ID]. Do you know of any alternatives?Leeann
I get No such processSanalda
If you are having No such process, there is good chance that ps aux returned the command you just run (ps aux | grep)Mesdemoiselles
Yes officer, this is the man that killed the container. (Thanks!)Whelp
Single command that'll do it: sudo pkill -f <<container id>>Bren
And if you have jq installed, a one-liner that you can give the container name instead of the id: docker container inspect <<container_name>> | jq -r 'first | .Id' | sudo xargs pkill -fBren
B
46

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
Botello answered 12/7, 2015 at 8:26 Comment(4)
Thx, yes restarting the machine helped. Unfortunately it's a server and shouldn't be restarted too often, hope they'll fix the bug. As I have docker 1.7Beason
I agree, this really is a workaround, not a full resolution. I will monitor that bug report.Botello
I had an unhealthy container that I could not stop or kill : docker stop -f # helped, thanks!Pokeweed
This is a very good suggestion. For me one channels connection was open to the container and I couldn't stop or kill the container. Then I simply closed the browser which was communicating with the container. Afterwards it was simple to stop and kill the container.Bereave
C
18

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.

Commensal answered 21/1, 2021 at 1:18 Comment(0)
W
16

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
Wrap answered 15/1, 2016 at 13:8 Comment(2)
what would be the reason for this? i cannot find any reference for itAllseed
Absolutely no idea, I just spotted it when messing aroundWrap
G
14

Enjoy

sudo aa-remove-unknown

This is what worked for me.

Gel answered 18/10, 2019 at 14:21 Comment(2)
Yes, this has successfully removed the infected container.Cawnpore
One should mention that you may need to reboot your machine after that since this will break a number of other things as well, especially snaps -- e.g., spotify. Just run sudo aa-remove-unknown -n for a dry-run first to see what all will be affected.Pleasurable
S
12

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 .

Sociability answered 25/2, 2020 at 11:25 Comment(0)
V
11

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.

Viewable answered 9/7, 2021 at 14:10 Comment(0)
G
6

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.

Gorlicki answered 31/10, 2019 at 11:41 Comment(0)
B
5

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

Bereave answered 10/6, 2022 at 10:54 Comment(0)
S
2

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

Schmaltz answered 14/8, 2020 at 20:58 Comment(0)
C
2

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?

Craven answered 2/9, 2020 at 11:26 Comment(0)
S
2

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.

Severance answered 25/12, 2021 at 23:38 Comment(0)
P
-1

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

systemctl restart docker
Pagoda answered 22/10, 2021 at 5:25 Comment(0)
S
-5

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

Sire answered 29/7, 2021 at 15:45 Comment(1)
ATTENTION: Be careful using this command! It will remove all of your docker containers (stopped! Unless you have your data stored in a volume running this command might cause unintended data loss! Furthermore this doesn't address the POs question. The question is about stopping and restarting a container.Bisayas

© 2022 - 2024 — McMap. All rights reserved.