docker-撰写健康检查问题

docker-compose healthcheck issue

我目前正在使用 docker-compose 开发一个微服务项目。所以我正在为我的容器添加健康检查。但除了 mysql 其他一切都保持不健康,但我不知道为什么。

这是我的 docker-compose.yml 文件。

version: "3"
services:
  zookeeper:
    image: wurstmeister/zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"

  kafka:
    image: wurstmeister/kafka
    container_name: kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 52.78.52.254
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - zookeeper

  mysql:
    image: mysql
    container_name: mysql
    ports:
      - "3306:3306"
    volumes:
      - ./data/mysql:/var/lib/mysql:rw
    environment:
      - MYSQL_ROOT_PASSWORD=dnjscksdn98@
      - MYSQL_DATABASE=explanet_dev
      - MYSQL_USER=alex
      - MYSQL_PASSWORD=dnjscksdn98@
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "mysql", "-u$$MYSQL_USER", "-p$$MYSQL_ROOT_PASSWORD" ]
      interval: 30s
      timeout: 10s
      retries: 3

  api-gateway:
    container_name: api-gateway
    ports:
      - "80:80"
    build:
      context: ./api-gateway
      args:
        ENVIRONMENT: dev
      dockerfile: Dockerfile
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://api-gateway/health-check" ]
      interval: 30s
      timeout: 10s
      retries: 3
    depends_on:
      - mysql

  chatting-service:
    container_name: chatting-service
    ports:
      - "8080:8080"
    build:
      context: ./chatting-service
      args:
        ENVIRONMENT: dev
      dockerfile: Dockerfile
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://chatting-service:8080/health-check" ]
      interval: 30s
      timeout: 10s
      retries: 3
    depends_on:
      - mysql
      - kafka
      - api-gateway

如果我输入 docker inspect ${any container name},它会在 State 中显示为这样。

"Health": {
                "Status": "unhealthy",
                "FailingStreak": 12,
                "Log": [
                    {
                        "Start": "2020-12-01T04:47:28.090046668Z",
                        "End": "2020-12-01T04:47:28.158193366Z",
                        "ExitCode": -1,
                        "Output": "OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused \"exec: \\"curl\\": executable file not found in $PATH\": unknown"
                    },
                    {
                        "Start": "2020-12-01T04:47:58.162656864Z",
                        "End": "2020-12-01T04:47:58.233280387Z",
                        "ExitCode": -1,
                        "Output": "OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused \"exec: \\"curl\\": executable file not found in $PATH\": unknown"
                    },
                    {
                        "Start": "2020-12-01T04:48:28.238446336Z",
                        "End": "2020-12-01T04:48:28.325520772Z",
                        "ExitCode": -1,
                        "Output": "OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused \"exec: \\"curl\\": executable file not found in $PATH\": unknown"
                    },
                    {
                        "Start": "2020-12-01T04:48:58.330407168Z",
                        "End": "2020-12-01T04:48:58.398503364Z",
                        "ExitCode": -1,
                        "Output": "OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused \"exec: \\"curl\\": executable file not found in $PATH\": unknown"
                    },
                    {
                        "Start": "2020-12-01T04:49:28.403163576Z",
                        "End": "2020-12-01T04:49:28.477627297Z",
                        "ExitCode": -1,
                        "Output": "OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused \"exec: \\"curl\\": executable file not found in $PATH\": unknown"
                    }
                ]
            }

我的 java Dockerfile 看起来像这样。

FROM adoptopenjdk/openjdk11:alpine-jre
COPY build/libs/base-service-0.0.1-SNAPSHOT.jar base-service.jar
ARG ENVIRONMENT
ENV SPRING_PROFILES_ACTIVE=${ENVIRONMENT}
ENTRYPOINT ["java", "-jar", "base-service.jar"]

我目前 运行 在 Ubuntu 20.04 服务器上。有什么解决办法吗?

您好,错误信息很清楚,您不能运行 curl 进行健康检查,因为它不包含在您使用的基础映像中,您需要安装它。
alpine images

的示例