Is there any command which can combine the docker stop
and docker rm
command together ? Each time I want to delete a running container, I need to execu
In my case, to remove the running containers I used
docker rm -f $(docker ps -a -q)
In case you also need to remove the images, then run
docker rmi $(docker images -q)
afterwards.
Only run docker rmi $(docker images -q)
if you want to remove the images.
For removing a single container
docker rm -f CONTAINER_ID
For removing all containers
docker rm -f `docker container ps -qa`
To remove all stopped containers docker system prune
To stop live container, docker stop CONTAINER_ID
waits 10 sec and it calls docker kill CONTAINER_ID
Or with docker kill CONTAINER_ID
, you can immediately stop the container
https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/
You can use kill, and also by using rm and the force flag it will also use kill.
Remove all containers: docker ps -aq | xargs docker rm -f
You can use :
docker rm -f CONTAINER_ID
It will remove the container even if it is still running.
https://docs.docker.com/engine/reference/commandline/rm/
You can also run your containers with --rm
option, it will be automatically removed when stopped.
https://docs.docker.com/engine/reference/run/#clean-up-rm
Edit: The rm -f
might be dangerous for your data and is best suited for test or development containers. @Bernard's comment on this subject is worth reading.