如何在Docker中列出容器

本文介绍了如何在Docker中列出容器,包括显示正在运行、所有、最新创建以及指定数量的容器的命令。还提到了Docker命令的更新,新增了管理命令,以及如何查看容器大小。同时提醒注意不同命令在显示容器大小方面的区别,特别是对于大量运行容器的情况。
摘要由CSDN通过智能技术生成

本文翻译自:How to list containers in Docker

There's a command to list images, docker images , but there doesn't seem to be a corresponding docker containers . 有一个命令可以列出镜像, docker images ,但是似乎没有相应的docker containers

Other than becoming root and looking into /var/lib/docker there doesn't seem a way to do that. 除了成为root并查看/var/lib/docker ,似乎没有其他方法可以做到。 Am I missing something? 我想念什么吗? Is that something one isn't supposed to do? 那是不应该做的事情吗?


#1楼

参考:https://stackoom.com/question/18exV/如何在Docker中列出容器


#2楼

To show only running containers use the given command: 要仅显示正在运行的容器,请使用给定命令:

docker ps

To show all containers use the given command: 要显示所有容器,请使用给定命令:

docker ps -a

To show the latest created container (includes all states) use the given command: 要显示最新创建的容器 (包括所有状态),请使用给定命令:

docker ps -l

To show n last created containers (includes all states) use the given command: 要显示n个最后创建的容器 (包括所有状态),请使用给定命令:

docker ps -n=-1

To display total file sizes use the given command: 要显示总文件大小,请使用给定命令:

docker ps -s

The content presented above is from docker.com . 上面显示的内容来自docker.com

In the new version of Docker, commands are updated, and some management commands are added: 在新版本的Docker中,命令已更新,并添加了一些管理命令:

docker container ls

It is used to list all the running containers. 它用于列出所有正在运行的容器。

docker container ls -a

And then, if you want to clean them all, 然后,如果您要清洁它们,

docker rm $(docker ps -aq)

It is used to list all the containers created irrespective of its state. 它用于列出所有创建的容器,无论其状态如何。

And to stop all the Docker containers (force) 并停止所有Docker容器(强制执行)

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

Here the container is the management command. 这里的容器是管理命令。


#3楼

Note that some time ago there was an update to this command. 请注意,前段时间对该命令进行了更新。 It will not show the container size by default (since this is rather expensive for many running containers). 默认情况下,它不会显示容器的大小(因为这对于许多正在运行的容器来说是相当昂贵的)。 Use docker ps -s to display container size as well. 使用docker ps -s可以显示容器大小。


#4楼

docker ps -s will show the size of running containers only. docker ps -s将仅显示正在运行的容器的大小。

To check the size of all containers use docker ps -as 要检查所有容器的大小,请使用docker ps -as


#5楼

To list only the containers SHA1: 仅列出容器SHA1:

docker ps -aq --no-trunc

That way, you can use the list of all containers for other commands (which accept multiple container ids as parameters). 这样,您可以将所有容器的列表用于其他命令(这些命令接受多个容器ID作为参数)。

For example, to list only the name of all containers (since docker ps list only their names with other information): 例如, 列出所有容器的名称 (因为docker ps仅列出其名称以及其他信息):

docker inspect --format='{{.Name}}' $(sudo docker ps -aq --no-trunc)

#6楼

I got the error message Cannot connect to the Docker daemon. 我收到错误消息Cannot connect to the Docker daemon. I forgot I am running the daemon as root and needed sudo : 我忘了我以root身份运行守护进程,并且需要sudo

$ sudo docker ps
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值