如何从我的 docker-compose 文件中查看容器的容器运行状况

How do I see container health status for containers from my docker-compose file

自 docker-compose 1.10.0 there is support for health checks。我开始在我的 docker-compose 文件之一中实现这些。

有几个站点建议可以从 docker ps 中查看容器的健康状态。见下文。

但是,当 运行 docker ps 时,我没有看到任何健康状况。我的 docker-compose 文件的摘录如下:

version: "2.1"

services:

  my-service:
    container_name: my-service
    image: "our-registry:5000/my-service:1.0.1"
    expose: [3000]
    restart: always
    depends_on:
      - other-service-1
      - other-service-2
    healthcheck:
      test: ["nc", "-z", "127.0.0.1", "3000", "||", "exit", "1"]
      interval: "2s"
      timeout: "1

我的 docker ps 输出的摘录如下:

# docker ps
CONTAINER ID        IMAGE                                  COMMAND                  CREATED             STATUS              PORTS          NAMES
af680b9fc6c3        our-registry/pr-georegion:1.0.1        "node index.js"          8 minutes ago       Up 8 minutes        3000/tcp       pr-georegion

Docker 和 docker-撰写版本:

# docker -v
Docker version 1.12.6, build 78d1802
# docker-compose -v
docker-compose version 1.10.1, build b252738

Example 1

Example 2

我在 docker-compose 文件中发现了问题。来自[官方文档](测试必须是字符串或列表。如果是列表,第一项必须是 NONE、CMD 或 CMD-SHELL。如果是字符串,则相当于指定 CMD-SHELL 后跟该字符串。):

test must be either a string or a list. If it’s a list, the first item must be either NONE, CMD or CMD-SHELL. If it’s a string, it’s equivalent to specifying CMD-SHELL followed by that string.

所以我改变了:

test: ["nc", "-z", "127.0.0.1", "3000", "||", "exit", "1"]

用于:

test: ["CMD-SHELL", "nc -z 127.0.0.1 3000 || exit 1"]