Docker-Compose: How to healthcheck OpenJDK:8 container?

给你一囗甜甜゛ 提交于 2020-12-06 16:48:58

问题


I'm trying to healthcheck a container that extends openjdk:8. The corresponding Dockerfile can be found at https://github.com/jhipster/jhipster-registry/blob/master/Dockerfile

I have the following directory structure:

test/
├── central-server-config
│   └── application.yml
├── docker-compose.yml
├── jhipster-registry.yml
└── Ping.jar

application.yml

#common configuration shared between all applications
configserver:
    name: Docker JHipster Registry
    status: Connected to the JHipster Registry running in Docker

jhipster:
    security:
        authentication:
            jwt:
                secret: 3ac0a39ed9a2a58ca74d9d36c5227e51225480e2

eureka:
    client:
        service-url:
            defaultZone: http://admin:${jhipster.registry.password}@jhipster-registry:8761/eureka/

docker-compose.yml

version: '2.1'
services:
    jhipster-registry:
        extends:
            file: jhipster-registry.yml
            service: jhipster-registry
        mem_limit: 512m
        ports:
            - 8761:8761
        healthcheck:
          test: ["CMD", "java", "-jar", "Ping.jar", "localhost", "8761"]
          interval: 30s
          retries: 10 

jhipster-registry.yml

version: '2.1'
services:
    jhipster-registry:
        image: jhipster/jhipster-registry:v4.0.2
        volumes:
            - ./central-server-config:/central-config
        # By default the JHipster Registry runs with the "dev" and "native"
        # Spring profiles.
        # "native" profile means the filesystem is used to store data, see
        # http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html
        environment:
            - SPRING_PROFILES_ACTIVE=dev,native
            - SECURITY_USER_PASSWORD=password
            - JHIPSTER_REGISTRY_PASSWORD=password
            # - GIT_URI=https://github.com/jhipster/jhipster-registry/
            # - GIT_SEARCH_PATHS=central-config
        ports:
            - 8761:8761

Ping.java

package ping;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Main {
    public static void main(String[] args) {
        if (args.length != 2) {
            System.exit(-1);
        }

        String host = args[0];
        int port = 0;

        try {
            port = Integer.parseInt(args[1]);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            System.exit(-2);
        }

        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(host, port), 10 * 1000);
            System.exit(0);
        } catch (IOException e) {
            System.exit(1);
        }
    }
}

I start the containers by running

docker-compose up -d --force-recreate

inside the test directory, and I'm able to access the jhipster-registry by browser. It is also reachable by the above jar program:

izio@1z10:~$ cd Desktop/
izio@1z10:~/Desktop$ java -jar Ping.jar localhost 8761
izio@1z10:~/Desktop$ echo $?
0
izio@1z10:~/Desktop$ 

which returns 0, meaning it can connect to the service.

If anyone is wondering, I tried also using curl but without succes (almost sure it's not provided in the docker image to reduce size), so I switched to this Java snippet hoping to have no problem since it's openjdk...

The jhipster-registry is up in about 3 minutes with this configuration, but no matter how much time I wait it always shows as health starting and finally unhealthy (after 10 x 30'' = 300'' = 5').

Any idea on what is wrong with this healthcheck? I provided my minimal not working example so that it can be tested by anyone. I need this check in order to be able to start another service, which will need to connect to the registry, only when it is ready to accept connections and not just after the container has started, otherwise the app will fail and stop, unless I set restart: on-failure, but I would prefer to only start it when the other service is up, instead of it keeping restarting hoping to find the registry ready.


回答1:


The problem here was that the Jar file was not available on the filesystem of the container. To make it available, we need to use volumes in the following way:

jhipster-registry:
    extends:
        file: jhipster-registry.yml
        service: jhipster-registry
    mem_limit: 512m
    ports:
        - 8761:8761
    networks:
      - backend
    volumes:
      - ${PWD}/Ping.jar:/Ping.jar
    healthcheck:
      test: ["CMD", "java", "-jar", "/Ping.jar", "localhost", "8761"]
      interval: 30s
      retries: 20

and reference the Jar file starting from the root directory (/).



来源:https://stackoverflow.com/questions/52347152/docker-compose-how-to-healthcheck-openjdk8-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!