Reputation: 1552
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
Upvotes: 4
Views: 11520
Reputation: 1539
Do you have by any chance any other process/user login to container sometimes when it did not stopped ? Since as you mentioned this is something not happening frequently : have you checked the state of environment when this happens for parameters like CPU utilization, memory etc.
Have you tried
a) stopping the container with overriding -t flag
Use "-t" flag of "docker stop" command
-t, --time=10
Seconds to wait for stop before killing it
b) Use "-f" flag of "docker rm" command
-f, --force[=false]
Force the removal of a running container (uses SIGKILL)
Upvotes: 2
Reputation: 1013
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
Upvotes: 6