Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop and Remove Container does not work sometimes

Tags:

shell

docker

sh

I have a shell script .sh to stop and remove my running containers, which usually works very well. But in one out of ten times the docker rm command seems to be executed before docker stop finished, so it raises an error that the container is still running. What can i do to prevent that error?

docker stop nostalgic_shirley
docker rm nostalgic_shirley 

Output

nostalgic_shirley
nostalgic_shirley
Error response from daemon: You cannot remove a running container xxxx. Stop the container before attempting removal or force remove

if i then try to use docker rm nostalgic_shirley again, it works fine.

I should not use docker stop --force as the shutdown should be gracefully

like image 240
ChrisRob Avatar asked Sep 16 '25 15:09

ChrisRob


1 Answers

If you really care to stop the container before deleting:

 docker stop nostalgic_shirley
 while [ ! -z "$(docker ps | grep nostalgic_shirley)" ]; do sleep 2; done
 docker rm nostalgic_shirley

If not, simply delete it with "force" option (You can even skip stopping):

 docker rm -f nostalgic_shirley
like image 79
Eduard Dubilyer Avatar answered Sep 18 '25 08:09

Eduard Dubilyer