Skip to content

Instantly share code, notes, and snippets.

@miklosbagi
Last active May 28, 2024 08:07
Show Gist options
  • Save miklosbagi/def69a1fa60b27e84288cf767a0b2efa to your computer and use it in GitHub Desktop.
Save miklosbagi/def69a1fa60b27e84288cf767a0b2efa to your computer and use it in GitHub Desktop.
Docker Compose gRPC Health

Minimal linux version for all of those gRPC services that don't provide health port.

Example with gripmock

  gripmock:
    image: gripmock/gripmock
    ports:
      - "${STUB_GRPC_PORT}:${STUB_GRPC_PORT}"
      - "${STUB_ADMIN_PORT}:${STUB_ADMIN_PORT}"
    volumes:
      - ./stubs/service/protos:/proto:ro
      - ./stubs/service/stubs:/stubs:ro
    healthcheck:
        # note: this checks for without netstat/nc/other tools. Also, docker is tcp6.
        test: ["CMD-SHELL", "grep -q :$(printf '%04X' $$STUB_GRPC_PORT) /proc/net/tcp6"]
        interval: 3s
        timeout: 2s
        retries: 15
    command: --admin-port ${STUB_ADMIN_PORT} --grpc-port ${STUB_GRPC_PORT} --stub=/stubs /proto/service/v1/pservice.proto

The trick is here: grep -q :$(printf '%04X' $$STUB_GRPC_PORT) /proc/net/tcp6:

  • printf '%04X' $$STUB_GRPC_PORT will HEX the readable (decimal) port number
  • /proc/net/tcp6 is docker default, the port will appear here once it's opened (i.e. service started successfully)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment