single command to stop and remove docker container

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.


docker stop CONTAINER_ID | xargs docker rm

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 (e.g. docker run --rm -it alpine), 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.


You can stop and remove the container with a single command the $_ gives you the last echo

docker stop CONTAINER && docker rm $_

Tags:

Docker