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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With