# IBKR's Client Portal Gateway in a container, for `docker compose --profile ibkr up -d`.
# IBKR publishes the gateway as a zip, not an image. The build downloads it once and refuses it unless its sha256
# matches the one you pass, so the image you run is the gateway you checked:
#   curl -fsSLO https://download2.interactivebrokers.com/portal/clientportal.gw.zip && shasum -a 256 clientportal.gw.zip
#   IBKR_GATEWAY_SHA256=<that hash> docker compose --profile ibkr build ibkr-gateway
# The gateway never sees a password from the fund: you log in on its page yourself, and it keeps only the session.
# The fund trusts the gateway's certificate only by its SHA-256 fingerprint. Print it to compare with the wizard's:
#   docker compose exec ibkr-gateway keytool -list -v -keystore root/vertx.jks -storepass mywebapi | grep SHA256
# The stock keystore (vertx.jks, password mywebapi) ships in every download; replace it with your own for a gateway
# reached from another machine, and trust that certificate instead (docs/operating.md).
FROM eclipse-temurin:21-jre-alpine
ARG GATEWAY_URL=https://download2.interactivebrokers.com/portal/clientportal.gw.zip
ARG GATEWAY_SHA256
RUN test -n "$GATEWAY_SHA256" || (echo "Set IBKR_GATEWAY_SHA256 to the sha256 of clientportal.gw.zip; see deploy/ibkr-gateway/Dockerfile" >&2; exit 1)
RUN apk add --no-cache curl unzip \
 && addgroup -S gw && adduser -S -G gw -h /gw gw \
 && curl -fsSL -o /tmp/gw.zip "$GATEWAY_URL" \
 && echo "$GATEWAY_SHA256  /tmp/gw.zip" | sha256sum -c - \
 && unzip -q /tmp/gw.zip -d /gw && rm /tmp/gw.zip \
 && apk del curl unzip \
 && chown -R gw:gw /gw
COPY --chown=gw:gw conf.yaml /gw/root/conf.yaml
USER gw
WORKDIR /gw
EXPOSE 5000
# Process health only: the port answers. Whether you are logged in is the fund's to report, not the container's.
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s CMD nc -z 127.0.0.1 5000 || exit 1
CMD ["sh", "bin/run.sh", "root/conf.yaml"]
