File size: 1,808 Bytes
e6ea4f8
 
 
 
 
 
 
 
 
 
 
 
a713f1a
 
 
 
 
 
e6ea4f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# syntax=docker/dockerfile:1

FROM alpine:3.19 AS source

WORKDIR /src

RUN apk add --no-cache tar

ADD https://codeload.github.com/james-6-23/codex2api/tar.gz/refs/heads/main /tmp/codex2api.tar.gz

RUN tar -xzf /tmp/codex2api.tar.gz --strip-components=1 -C /src

# Upstream currently stops the store twice during shutdown, which causes
# `panic: close of closed channel` on platforms that send SIGTERM during
# health/restart cycles. Remove the deferred stop and keep the explicit
# shutdown path.
RUN sed -i '/defer store.Stop()/d' /src/main.go

FROM node:20-alpine AS frontend-builder

WORKDIR /frontend

COPY --from=source /src/frontend/package.json /src/frontend/package-lock.json ./

RUN --mount=type=cache,target=/root/.npm \
    npm ci --no-audit --no-fund

COPY --from=source /src/frontend/ .

ARG BUILD_VERSION=hf-space

RUN VITE_APP_VERSION=${BUILD_VERSION} npm run build

FROM golang:1.25-alpine AS go-builder

WORKDIR /app

COPY --from=source /src/go.mod /src/go.sum ./

RUN --mount=type=cache,target=/go/pkg/mod \
    go mod download

COPY --from=source /src/ .
COPY --from=frontend-builder /frontend/dist ./frontend/dist

RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o /codex2api .

FROM alpine:3.19

RUN apk --no-cache add ca-certificates tzdata \
    && adduser -D -u 1000 user \
    && mkdir -p /data \
    && chmod 777 /data

ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH \
    CODEX_PORT=8080 \
    DATABASE_DRIVER=sqlite \
    CACHE_DRIVER=memory \
    DATABASE_PATH=/data/codex2api.db \
    TZ=Asia/Shanghai

USER user
WORKDIR $HOME/app

COPY --from=go-builder /codex2api /usr/local/bin/codex2api

EXPOSE 8080

ENTRYPOINT ["/usr/local/bin/codex2api"]