single command to stop and remove docker container

2019-03-10 11:38发布

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
9条回答
做自己的国王
2楼-- · 2019-03-10 12:24

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)
查看更多
【Aperson】
3楼-- · 2019-03-10 12:33

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

docker rmi -f $(docker images -a -q)
查看更多
Anthone
4楼-- · 2019-03-10 12:34

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.

查看更多
一纸荒年 Trace。
5楼-- · 2019-03-10 12:37

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

查看更多
叛逆
6楼-- · 2019-03-10 12:39

In my case to remove all running containers, I used

docker rm -f $(docker ps -a -q); docker rmi $(docker images -q)
查看更多
Lonely孤独者°
7楼-- · 2019-03-10 12:40

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

查看更多
登录 后发表回答