如何删除旧的Docker容器

本文翻译自:How to remove old Docker containers

This question is related to Should I be concerned about excess, non-running, Docker containers? 此问题与我是否应该担心多余的,未运行的Docker容器有关? .

I'm wondering how to remove old containers. 我想知道如何删除旧容器。 The docker rm 3e552code34a lets you remove a single one, but I have lots already. docker rm 3e552code34a可让您删除一个,但我已经有很多。 docker rm --help doesn't give a selection option (like all, or by image name). docker rm --help没有提供选择选项(像所有选项一样,或按图像名称)。

Maybe there is a directory in which these containers are stored where I can delete them easily manually? 也许有一个存储这些容器的目录,可以方便地手动删除它们?


#1楼

参考:https://stackoom.com/question/1AK4q/如何删除旧的Docker容器


#2楼

Since Docker 1.13.x you can use Docker container prune : Docker 1.13.x开始,您可以使用Docker容器prune

docker container prune

This will remove all stopped containers and should work on all platforms the same way. 这将删除所有停止的容器,并且应在所有平台上以相同的方式工作。

There is also a Docker system prune : 还有一个Docker系统修剪

docker system prune

which will clean up all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes, in one command. 它将在一个命令中清除所有未使用的容器,网络,映像(悬空和未引用),以及可选的卷。


For older Docker versions, you can string Docker commands together with other Unix commands to get what you need. 对于较旧的Docker版本,可以将Docker命令与其他Unix命令一起使用以获取所需的内容。 Here is an example on how to clean up old containers that are weeks old: 这是有关如何清理已使用数周的旧容器的示例:

$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552 . 为了得到应得的荣誉,此示例来自https://twitter.com/jpetazzo/status/347431091415703552


#3楼

Another method, which I got from Guillaume J. Charmes (credit where it is due): 我从Guillaume J. Charmes(在应交的贷方)获得的另一种方法:

docker rm `docker ps --no-trunc -aq`

will remove all containers in an elegant way. 将以一种优雅的方式移除所有容器。

And by Bartosz Bilicki, for Windows: 对于Windows,由Bartosz Bilicki撰写:

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

For PowerShell: 对于PowerShell:

docker rm @(docker ps -aq)

An update with Docker 1.13 (Q4 2016), credit to VonC ( later in this thread ): Docker 1.13的更新(2016年第四季度),归功于VonC在此线程的后面 ):

docker system prune will delete ALL unused data (ie, in order: containers stopped, volumes without containers and images with no containers). docker system prune会删除所有未使用的数据(即,顺序为:容器已停止,无容器的卷和无容器的映像)。

See PR 26108 and commit 86de7c0 , which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess. 请参阅PR 26108commit 86de7c0 ,它们引入了一些新命令,以帮助促进可视化Docker守护程序数据在磁盘上占用了多少空间,并允许轻松清除“不需要的”多余空间。

docker system prune

WARNING! This will remove:
    - all stopped containers
    - all volumes not used by at least one container
    - all images without at least one container associated to them
Are you sure you want to continue? [y/N] y

#4楼

First, stop running containers before attempting to remove them 首先,在尝试删除容器之前先停止运行它们

Remove running containers 删除正在运行的容器

docker rm $(docker stop -t=1 $(docker ps -q))

You could use kill instead of stop . 您可以使用kill而不是stop In my case I prefer stop since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely. 就我而言,我宁愿stop因为我倾向于重新运行它们,而不是每次都创建一个新的,所以我试图很好地关闭它们。

Note: Trying to stop a container will give you an error: 注意:尝试停止容器会给您一个错误:

Error: Impossible to remove a running container, please stop it first 错误:无法删除正在运行的容器,请先将其停止

Remove all containers 取出所有容器

docker rm $(docker ps -a -q)

#5楼

If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID> or with docker ps --since <container-ID> . 如果你不喜欢删除所有容器,您可以选择前或在特定容器后创建的所有集装箱docker ps --before <container-ID>docker ps --since <container-ID> This feature is at least in Docker version 0.6.5. 此功能至少在Docker 0.6.5版中。

Let's say you have developed your system, and now it is working, but there are a number of containers left. 假设您已经开发了系统,现在可以使用了,但是还剩下许多容器。 You want to remove containers created before that working version. 您要删除在该工作版本之前创建的容器。 Determine the ID of the working container with docker ps . 使用docker ps确定工作容器的ID。

Remove containers created before an other container 删除在其他容器之前创建的容器

docker rm $(docker ps --before 9c49c11c8d21 -q)

Another example. 另一个例子。 You have your database already running on a Docker container. 您的数据库已经在Docker容器上运行。 You have developed your application to run on another container and now you have a number of unneeded containers. 您已经开发了可以在另一个容器上运行的应用程序,现在您有了许多不需要的容器。

Remove containers created after a certain container 删除在某个容器之后创建的容器

docker rm $(docker ps --since a6ca4661ec7f -q)

Docker stores containers in /var/lib/docker/containers in Ubuntu. Docker将容器存储在Ubuntu的/var/lib/docker/containers中。 I think extra containers do no other harm, but take up disk space. 我认为额外的容器没有其他危害,但是会占用磁盘空间。


#6楼

I always use docker rmi $(docker ps -a -q) to remove all images. 我总是使用docker rmi $(docker ps -a -q)删除所有图像。

You can remove directory /var/lib/docker/graph when docker rmi failed. docker rmi失败时,您可以删除目录/var/lib/docker/graph

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值