single command to stop and remove docker container

Docker

Docker Problem Overview


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 execute 2 commands sequentially, I wonder if there is a combined command can simplify this process.

docker stop CONTAINER_ID
docker rm CONTATINER_ID

Docker Solutions


Solution 1 - Docker

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.

Solution 2 - Docker

docker stop CONTAINER_ID | xargs docker rm

Solution 3 - Docker

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

docker stop CONTAINER && docker rm $_

Solution 4 - Docker

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.

Solution 5 - Docker

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.

Solution 6 - Docker

Remove all containers: docker ps -aq | xargs docker rm -f

Solution 7 - Docker

This will stop and remove all images including running containers as we are using -f

docker rmi -f $(docker images -a -q)

Solution 8 - Docker

Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove

docker ps -a

To remove: $ docker rm ID_or_Name ID_or_Name

Remove a container upon exit:

If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run --rm to automatically delete it when it exits.

Run and Remove : docker run --rm image_name

Remove all exited containers:

You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status. When you've verified you want to remove those containers, using -q to pass the IDs to the docker rm command.

List:

docker ps -a -f status=exited

docker rm $(docker ps -a -f status=exited -q)

Remove containers using more than one filter:

Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either Created (a state which can result when you run a container with an invalid command) or Exited, you can use two filters:

docker ps -a -f status=exited -f status=created

Stop and Remove all the containers:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Solution 9 - Docker

For removing a single container

docker rm -f CONTAINER_ID

For removing all containers

docker rm -f `docker container ps -qa`

Solution 10 - Docker

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

Solution 11 - Docker

remove all container with xargs

docker ps -a -q | xargs docker rm 

for stop all

sudo docker ps -a -q |sudo  xargs docker stop 

remove single container

docker rm -f <container_id>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmainframerView Question on Stackoverflow
Solution 1 - DockervmontecoView Answer on Stackoverflow
Solution 2 - DockerortexView Answer on Stackoverflow
Solution 3 - DockerAxel MonroyView Answer on Stackoverflow
Solution 4 - Dockeruser9869932View Answer on Stackoverflow
Solution 5 - DockerobizuesView Answer on Stackoverflow
Solution 6 - DockerAndriy TolstoyView Answer on Stackoverflow
Solution 7 - DockerPranoy GnView Answer on Stackoverflow
Solution 8 - DockerPrakash AWSView Answer on Stackoverflow
Solution 9 - DockerAbhinav MehtaView Answer on Stackoverflow
Solution 10 - DockerNuriddin KudratovView Answer on Stackoverflow
Solution 11 - Dockerdılo sürücüView Answer on Stackoverflow