Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single command to stop and remove docker container

Tags:

docker

People also ask

What is the command to stop docker container?

Note that pressing `Ctrl+C` when the terminal is attached to a container output causes the container to shut down. Use `Ctrl+PQ` in order to detach the terminal from container output.


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.


docker stop CONTAINER_ID | xargs docker rm

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

docker stop CONTAINER && docker rm $_

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.


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