【问题标题】:Check docker-compose services are running or not检查 docker-compose 服务是否正在运行
【发布时间】:2021-08-15 19:35:40
【问题描述】:

我正在编写一个脚本来检查 docker 服务,但我想检查 docker-compose 内部的服务而不进入它。 例如:我们有像 tass 和 inception 这样的自定义服务,基本上我们通过这个命令“service tass status”检查它的状态

有没有办法在 Docker-compose 中检查这个服务

【问题讨论】:

  • 容器通常不运行“服务”;大致来说,像service 这样的命令在 Docker 中根本不起作用。您是否有要监控的示例,其中“容器”可能存在但“服务”不存在?
  • 请定义“检查”的确切含义,如“检查 docker-compose 中的服务”?你做了什么研究?就像做docker-compose --help 和阅读输出一样——你没有任何命令感兴趣吗?

标签: linux bash docker shell docker-compose


【解决方案1】:

Docker compose 只是构建 docker 镜像的工具。

您应该依靠 docker 命令来检查每个服务的健康状况,例如:

  • docker ps
  • docker stat
  • docker inspect
  • docker container ls

在这个How to check if the docker engine and a docker container are running? 线程中,您可以找到很多关于容器检查的替代方案。

【讨论】:

  • 以上命令仅适用于docker,不适用于docker-compose
  • 正如我已经说过的,docker-compose 只是一个在底层使用 docker 的工具。
【解决方案2】:

你可以使用:

docker-compose ps <name>
docker-compose top <name>
docker-compose logs <name>

“检查”名称为&lt;name&gt; 的服务。您可以通过执行 `docker-compose --help 来了解更多命令。

最后,您可以运行 docker-compose exec &lt;name&gt; &lt;shell&gt; 并在 cont inaner 中获得一个交互式 shell,它与一些 unix-utilities 将允许您更轻松地进一步“检查”容器。

最后,您可以提取正在运行的容器的名称,如 How do I retrieve the exact container name started by docker-compose.yml ,并使用其他答案中提到的任何 docker 命令来“检查”。从docker inspect &lt;the container name&gt;你可以得到cgroup名称和挂载的文件系统,你可以“检查”。

【讨论】:

    【解决方案3】:

    检查 .State.Status、.State.Running 等会告诉您它是否正在运行,但最好确保容器的 health。下面是一个您可以运行的脚本,它将确保两个容器在第二个容器中执行命令之前运行良好。如果已达到等待时间/尝试次数阈值,它会打印出 docker 日志。

    示例取自npm sql-mdb

    
        #!/bin/bash
        # Wait for two docker healthchecks to be in a "healthy" state before executing a "docker exec -it $2 bash $3"
        ##############################################################################################################################
        # $1 Docker container name that will wait for a "healthy" healthcheck (required)
        # $2 Docker container name that will wait for a "healthy" healthcheck and will be used to run the execution command (required)
        # $3 The actual execution command that will be ran (required)
        attempt=0
        health1=checking
        health2=checking
        while [ $attempt -le 79 ]; do
          attempt=$(( $attempt + 1 ))
          echo "Waiting for docker healthcheck on services $1 ($health1) and $2 ($health2): attempt: $attempt..."
          if [[ health1 != "healthy" ]]; then
            health1=$(docker inspect -f {{.State.Health.Status}} $1)
          fi
          if [[ $health2 != "healthy" ]]; then
            health2=$(docker inspect -f {{.State.Health.Status}} $2)
          fi
          if [[ $health1 == "healthy" && $health2 == "healthy"  ]]; then
            echo "Docker healthcheck on services $1 ($health1) and $2 ($health2) - executing: $3"
            docker exec -it $2 bash -c "$3"
            [[ $? != 0 ]] && { echo "Failed to execute \"$3\" in docker container \"$2\"" >&2; exit 1; }
            break
          fi
          sleep 2
        done
        if [[ $health1 != "healthy" || $health2 != "healthy"  ]]; then
          echo "Failed to wait for docker healthcheck on services $1 ($health1) and $2 ($health2) after $attempt attempts"
          docker logs --details $1
          docker logs --details $2
          exit 1
        fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-04
      • 2015-06-26
      • 2011-05-25
      • 2022-01-25
      • 2011-01-20
      • 2018-08-13
      • 2012-04-30
      • 1970-01-01
      相关资源
      最近更新 更多