简体   繁体   English

如何在全球范围内显示由 docker-compose 创建的所有正在运行的容器,而不考虑 docker-compose.yml

[英]How to show all running containers created by docker-compose, globally, regardless of docker-compose.yml

I have several docker-compose.yml files, running ok with docker-compose up , individually.我有几个docker-compose.yml文件,分别运行docker-compose up正常。

Each docker-compose runs several containers.每个 docker-compose 运行多个容器。

After they are up, I can't see what containers are up with docker ps .在它们启动后,我看不到docker ps有哪些容器。
I can see something with docker-compose ps , but only for a specific docker-compose.yml.我可以看到docker-compose ps的一些东西,但仅限于特定的 docker-compose.yml。

I want access to global polling of the containers state.我想访问容器 state 的全局轮询。

How can I list all running containers, no matter their origin?如何列出所有正在运行的容器,无论其来源如何?

Here is kind of a guess based on the clues of your question.这是根据您的问题的线索进行的猜测。

When you do run containers via docker-compose up , please do mind that hitting CTRL+C will actually stop your containers.当您通过docker-compose up运行容器时,请注意按CTRL+C实际上会停止您的容器。

Here is an example of doing this:这是执行此操作的示例:

$ docker-compose up
[+] Running 1/0
 ⠿ Container docker-test-1  Created                                  0.0s
Attaching to docker-test-1
^CGracefully stopping... (press Ctrl+C again to force)
[+] Running 1/1
 ⠿ Container docker-test-1  Stopped                                 10.2s
canceled

The important part here being: ^CGracefully stopping...这里的重要部分是: ^CGracefully stopping...

If you want to keep them running, you will have to run docker-compose up in a detached mode, with the help of the -d option.如果你想让它们继续运行,你必须在-d选项的帮助下以分离模式运行docker-compose up

 -d, --detach Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit.

Source: https://docs.docker.com/compose/reference/up/资料来源: https://docs.docker.com/compose/reference/up/


So doing,这样做,

docker-compose up -d && docker ps

Should actually give you what you expected.实际上应该给你你所期望的。
Eg:例如:

$ docker-compose up -d && docker ps
[+] Running 2/2
 ⠿ Network docker_default   Created                                                                                   0.1s
 ⠿ Container docker-test-1  Started                                                                                   0.5s
CONTAINER ID   IMAGE     COMMAND               CREATED        STATUS                  PORTS     NAMES
ab9f422247c9   alpine    "tail -f /dev/null"   1 second ago   Up Less than a second             docker-test-1

And if you want to see all container, regardless of their state, you can use the -a option of docker ps .如果你想查看所有容器,不管它们的 state,你可以使用docker ps-a选项。

 --all, -a Show all containers (default shows just running)

Source: https://docs.docker.com/engine/reference/commandline/ps/资料来源: https://docs.docker.com/engine/reference/commandline/ps/

Eg:例如:

$ docker-compose up  
[+] Running 2/2
 ⠿ Network docker_default   Created                                                                                   0.1s
 ⠿ Container docker-test-1  Created                                                                                   0.1s
Attaching to docker-test-1
^CGracefully stopping... (press Ctrl+C again to force)
[+] Running 1/1
 ⠿ Container docker-test-1  Stopped                                                                                  10.2s
canceled
$ docker ps -a
CONTAINER ID   IMAGE                 COMMAND               CREATED          STATUS                       PORTS     NAMES
615bcc6f1e50   alpine                "tail -f /dev/null"   18 seconds ago   Exited (137) 3 seconds ago             docker-test-1

Docker compose adds labels to each container that it creates. Docker compose 将标签添加到它创建的每个容器。 If you want to get all containers created by compose, you can perform a container ls and apply a filter.如果要获取 compose 创建的所有容器,可以执行容器 ls 并应用过滤器。

docker container ls --filter label=com.docker.compose.project

This will show all running container created by compose, regardless of the project name.这将显示由 compose 创建的所有正在运行的容器,无论项目名称如何。

For example, I created 2 apps in 2 different compose projects.例如,我在 2 个不同的撰写项目中创建了 2 个应用程序。 loadbalancer and manager .负载均衡器管理器 With the command, I get both back, but no other container that have not been created by compose and therefore don't container a project label.使用该命令,我将两者都返回,但没有其他尚未由 compose 创建的容器,因此不包含项目 label。

$ docker container ls --filter label=com.docker.compose.project --format \
   'table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Label "com.docker.compose.project"}}\t{{.Networks}}'

CONTAINER ID   NAMES                STATUS          project        NETWORKS
791f4059b4af   loadbalancer_app_1   Up 11 minutes   loadbalancer   loadbalancer_default
8d26e4503721   manager_app_1        Up 24 minutes   manager        manager_default

To list all running containers in docker列出 docker 中所有正在运行的容器

$docker ps $码头工人ps

When containers are running and are created as a part of docker definition present in docker-compose.yml files.当容器正在运行并作为 docker-compose.yml 文件中存在的 docker 定义的一部分创建时。 In a basic scenario, i would do在基本情况下,我会做

$docker-compose up -d $docker-compose up -d

to start the container and use the below command to check the containers are running启动容器并使用以下命令检查容器是否正在运行

$ docker-compose ps $ docker-compose ps

First command should contain the results from the compose command as well.第一个命令也应该包含来自 compose 命令的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有 docker-compose.yml 文件的情况下杀死 docker-compose 容器 - Kill docker-compose containers without docker-compose.yml file 在docker-compose.yml中了解容器的主机名 - Understanding Hostname of Containers in docker-compose.yml docker-compose在docker-compose.yml中具有多个服务的构建 - docker-compose build with multiple services in docker-compose.yml 如何找到用于启动容器的 docker-compose.yml? - How to find the docker-compose.yml that used to start containers? 使用sudo运行docker-compose时docker-compose.yml文件中的变量替换 - Variable substitution in docker-compose.yml file when running docker-compose with sudo docker-compose.yml运行docker-compose时的文件错误 - docker-compose.yml file errors on running docker-compose up 尽管docker-compose.yml中的container_name不同,但docker-compose仍会重新创建运行中的容器 - docker-compose recreates a running container despite having a different container_name in the docker-compose.yml docker-compose 显示所有不是在 docker-compose.yml 中定义的服务 - docker-compose displaying all services not the one defined in docker-compose.yml 如何将对 docker-compose.yml 所做的更改实施到分离的运行容器 - How to implement changes made to docker-compose.yml to detached running containers 如何配置 docker-compose.yml 以将 passbolt 与 docker-compose 一起使用? - How to configure docker-compose.yml to use passbolt with docker-compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM