drift-labs / gateway

Self hosted API gateway to easily interact with Drift V2 Protocol
Apache License 2.0
18 stars 11 forks source link

Add healthcheck to Dockerfile #65

Closed utgarda closed 4 weeks ago

utgarda commented 1 month ago

Feature request: Add healthcheck to Dockerfile for Drift Gateway

Description

The current Dockerfile uses gcr.io/distroless/base-debian12, which lacks a shell and network tools like curl. This complicates running Drift Gateway in Docker Compose, as other services need a way to verify the Gateway is running.

Proposal

Switch to debian:12 with curl installed and add a health check. At this stage, a functional image with tools is more beneficial than a size-optimized one.

Current Dockerfile

FROM rust:1.76.0 as builder

# docker automatically sets this to architecture of the host system
# requires DOCKER_BUILDKIT=1
ARG TARGETARCH

RUN apt-get update && apt-get install -y libgcc1
WORKDIR /build
COPY  . .
RUN cargo build --release
RUN if [ "$TARGETARCH" = "arm64" ]; then \
    cp /lib/aarch64-linux-gnu/libgcc_s.so.1 /build/target/release/; \
    elif [ "$TARGETARCH" = "amd64" ]; then \
    cp /lib/x86_64-linux-gnu/libgcc_s.so.1 /build/target/release/; \
    fi

FROM gcr.io/distroless/base-debian12
COPY --from=builder /build/target/release/libgcc_s.so.1 /lib/
COPY --from=builder /build/target/release/drift-gateway /bin/drift-gateway
ENTRYPOINT ["/bin/drift-gateway"]

Proposed Dockerfile

FROM rust:1.76.0 as builder

# docker automatically sets this to architecture of the host system
# requires DOCKER_BUILDKIT=1
ARG TARGETARCH

RUN apt-get update && apt-get install -y libgcc1
WORKDIR /build
COPY  . .
RUN cargo build --release
RUN if [ "$TARGETARCH" = "arm64" ]; then \
    cp /lib/aarch64-linux-gnu/libgcc_s.so.1 /build/target/release/; \
    elif [ "$TARGETARCH" = "amd64" ]; then \
    cp /lib/x86_64-linux-gnu/libgcc_s.so.1 /build/target/release/; \
    fi

FROM debian:12
COPY --from=builder /build/target/release/libgcc_s.so.1 /lib/
COPY --from=builder /build/target/release/drift-gateway /bin/drift-gateway
RUN apt update && apt install -y curl && rm -rf /var/cache/apt/archives /var/lib/apt/lists/*

ENTRYPOINT ["/bin/drift-gateway"]

HEALTHCHECK --interval=1m --start-period=1m --start-interval=5s CMD curl -f localhost:8080/v2/positions || exit 1

Benefits

jordy25519 commented 1 month ago

Agreed switching to full debian makes more sense as the default (will accept PR).

however, the health check is too opinionated imo for the default image. Should be left up to the user's own infrastructure / requirements

seems easy enough to wrap the base image if wanting to use the Docker provided healthcheck

FROM gateway:latest
HEALTHCHECK --interval=1m --start-period=1m --start-interval=5s CMD curl -f localhost:8080/v2/positions || exit 1
utgarda commented 1 month ago

Indeed, healthcheck can be put in compose.yaml, but the supported way to do so relies on shell tools in the container itself and that will require to alter the image to install curl at least.

I'll dig some more

utgarda commented 4 weeks ago

I found a way to add curl to the current Gateway image by adding a Dockerfile to my compose configurations, similar to

FROM drift-gateway
RUN apt-get update && apt-get install -y curl && rm -rf /var/cache/apt/archives /var/lib/apt/lists/*

HEALTHCHECK --interval=30s --start-period=1m --start-interval=5s CMD curl -f localhost:8080/v2/positions ||  exit 1

then using it in a service definition

services:
  gateway:
    build: ./docker/gateway
docker compose up --build
# Attaching to gateway-1, bot-1
# gateway-1  | [2024-08-20T13:42:18Z INFO  gateway] 🏛 gateway listening at http://0.0.0.0:8080
# gateway-1  | [2024-08-20T13:42:18Z INFO  gateway] 🪪: authority: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, default sub-account: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# gateway-1  | [2024-08-20T13:42:19Z INFO  gateway] 127.0.0.1 | 200 | GET /v2/positions HTTP/1.1 | (368.316764ms)

Now I'm quite happy with just having debian:12 as base image, sending that PR!

@jordy25519 would you be OK with adding curl to the image?

RUN apt-get update && apt-get install -y curl && rm -rf /var/cache/apt/archives /var/lib/apt/lists/*
jordy25519 commented 4 weeks ago

it sounds good, can include curl in the PR

utgarda commented 4 weeks ago

it sounds good, can include curl in the PR

done

jordy25519 commented 4 weeks ago

closed in #68