File size: 4,489 Bytes
d69340d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de97c46
81a3988
d69340d
de97c46
d69340d
81a3988
e0c9c73
1794eb9
de97c46
 
d69340d
 
 
de97c46
d69340d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de97c46
bbfd266
 
 
 
 
 
 
 
 
 
 
 
d69340d
 
 
 
de97c46
e0c9c73
 
 
 
 
6c95417
 
 
 
e0c9c73
6c95417
 
e0c9c73
 
 
de97c46
bbfd266
de97c46
 
 
d69340d
de97c46
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Hugging Face Space adapter for official chenyme/grok2api (thin image mode).
#
# WHY THIS EXISTS
# Upstream publishes a multi-arch image to GHCR on every push to main
# (chenyme/grok2api/.github/workflows/ghcr-image.yml). That image is already
# compiled, tested (go test / go vet / pnpm lint) and labelled with the exact
# upstream revision. This adapter re-bases that artifact and adds the Hugging
# Face runtime contract (port 7860, /data persistent storage, config seeding).
#
# Building the Space therefore takes seconds and cannot break because upstream
# changed a build script, bumped Go/Node, or added a file the compiler needs.
# That reliability is what makes unattended auto-updates safe.
#
# PINNING
# The auto-update workflow always patches GROK2API_IMAGE below to an immutable
# digest (ghcr.io/chenyme/grok2api@sha256:...) and GROK2API_CONFIG_REF to the
# matching upstream commit, so every deployed Space is reproducible and can be
# rolled back by re-deploying an older .hf-sync record.
#
# Build mode "source" (./Dockerfile) remains available as a fallback if
# upstream ever stops publishing images.

ARG GROK2API_IMAGE=ghcr.io/chenyme/grok2api@sha256:d24ca6a7efdbeaf5c7aa98f90973768aac444df1170f221d383ebfe6376f77be
FROM ${GROK2API_IMAGE}

ARG GROK2API_IMAGE
ARG GROK2API_CONFIG_REF=47810328db6dc939efe04f45b627ada61cd7517a
ARG GROK2API_VERSION=v3.1.6
ARG GROK2API_ADAPTER_HASH=b6eb9fc32d8a12e551db01a96e1ec0d5850b091ee0ae565fc5f1306f3ecc6cd9

ENV TZ=Asia/Shanghai \
    SERVER_PORT=7860 \
    DATA_DIR=/data \
    GROK2API_CONFIG_SOURCE=/data/config.yaml

# The upstream image ships /app/grok2api and /app/frontend/dist but NOT
# config.example.yaml, because its own entrypoint expects the operator to mount
# a config. start.sh seeds ${DATA_DIR}/config.yaml from that example on first
# boot, so fetch it from the exact upstream revision this image was built from.
# Also assert the layout we depend on, so a future upstream restructure fails
# the build loudly instead of deploying a broken Space.
RUN set -eux; \
    command -v wget >/dev/null; \
    test -x /app/grok2api; \
    test -d /app/frontend/dist; \
    url="https://raw.githubusercontent.com/chenyme/grok2api/${GROK2API_CONFIG_REF}/config.example.yaml"; \
    ok=0; \
    for attempt in 1 2 3; do \
      if wget -qO /app/config.example.yaml "$url"; then ok=1; break; fi; \
      echo "config.example.yaml fetch attempt ${attempt} failed" >&2; \
      sleep 3; \
    done; \
    test "$ok" = 1; \
    test -s /app/config.example.yaml

# sqlite3 CLI powers the boot-time database guard (sqlite-preflight.sh), which
# verifies /data/backend.db and repairs it if a rebuild left it malformed.
RUN set -eux; \
    ok=0; \
    for attempt in 1 2 3; do \
      if apk add --no-cache sqlite; then ok=1; break; fi; \
      echo "apk add sqlite attempt ${attempt} failed" >&2; \
      sleep 5; \
    done; \
    test "$ok" = 1; \
    sqlite3 --version

LABEL org.opencontainers.image.source="https://github.com/chenyme/grok2api" \
      io.viciy.adapter="huggingface/Dockerfile.image" \
      io.viciy.upstream.ref="${GROK2API_CONFIG_REF}" \
      io.viciy.upstream.image="${GROK2API_IMAGE}"

# Deployment marker served by the app itself (upstream's frontend handler serves
# any existing file under frontend/dist). The deploy workflow polls this to
# prove which upstream revision the *running container* actually is, instead of
# trusting Hugging Face's build-stage API, which can report RUNNING on the old
# container for a short window after a push.
#
# "adapter" is the fingerprint of every adapter file, so a change that only
# touches the adapter (and leaves the upstream revision alone) still changes the
# marker and therefore still forces verification to wait for the rebuild.
RUN set -eux; \
    printf 'revision=%s\nversion=%s\nimage=%s\nbuild_mode=image\nadapter=%s\n' \
      "${GROK2API_CONFIG_REF}" "${GROK2API_VERSION}" "${GROK2API_IMAGE}" "${GROK2API_ADAPTER_HASH}" \
      > /app/frontend/dist/__deployed-revision.txt; \
    cat /app/frontend/dist/__deployed-revision.txt

COPY --chmod=0755 start.sh /usr/local/bin/hf-start.sh
COPY --chmod=0644 sqlite-preflight.sh /usr/local/bin/sqlite-preflight.sh

EXPOSE 7860

HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
    CMD wget -qO- "http://127.0.0.1:${SERVER_PORT}/healthz" >/dev/null || exit 1

ENTRYPOINT ["/usr/local/bin/hf-start.sh"]
CMD ["/app/grok2api", "--config", "/app/config.yaml", "--listen", "0.0.0.0:7860"]