File size: 2,862 Bytes
c847fd6
da55f75
c847fd6
 
 
 
 
da55f75
c847fd6
da55f75
c847fd6
 
6dd017b
 
 
c847fd6
 
 
 
 
 
 
 
 
 
 
 
6dd017b
c847fd6
da55f75
 
191bdf0
6dd017b
 
da55f75
191bdf0
 
 
c847fd6
 
 
 
 
 
 
 
 
 
 
 
 
da55f75
 
c847fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dd017b
 
 
 
c847fd6
6dd017b
c847fd6
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
# MyAPI Hub on Hugging Face Spaces — single-container sidecar:
# postgres:18-alpine + redis + app + periodic pg_dump → /backup (HF bucket FUSE).
#
# PGDATA lives on Space ephemeral disk (FUSE is unsafe for live DB fsync).
# /backup is a mounted HF bucket; we dump there every BACKUP_INTERVAL_MIN minutes
# and restore on cold boot if PGDATA is empty and latest.sql.gz exists.

ARG APP_IMAGE=ghcr.io/jiayi-1994/sub2api:2.2.1

FROM ${APP_IMAGE} AS app

FROM postgres:18-alpine

USER root

# Renumber postgres user to uid/gid 1000 so HF bucket FUSE writes succeed under
# the same uid the container runs as.
RUN apk add --no-cache shadow redis supervisor tzdata ca-certificates bash \
    && groupmod -g 1000 postgres \
    && usermod -u 1000 -g 1000 postgres \
    && find / -xdev -uid 70 -exec chown 1000 {} + 2>/dev/null || true \
    && find / -xdev -gid 70 -exec chgrp 1000 {} + 2>/dev/null || true \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone

# Pull sub2api binary + entrypoint from upstream image.
COPY --from=app /app /app

# Anti-abuse rename of binary + pre-seed install lock so setup wizard is skipped
# (config comes from env via viper AutomaticEnv). Also scrub upstream binary
# name from any remaining references so /proc/*/cmdline never leaks it.
RUN cp /app/sub2api /app/svc \
    && chmod +x /app/svc \
    && rm -f /app/sub2api \
    && sed -i 's|/app/sub2api|/app/svc|g; s|sub2api|svc|g' /app/docker-entrypoint.sh \
    && mkdir -p /app/data \
    && printf 'installed_at=2026-01-01T00:00:00Z\n' > /app/data/.installed \
    && chmod 0400 /app/data/.installed \
    && printf '# managed by env vars (viper AutomaticEnv)\n' > /app/data/config.yaml

RUN mkdir -p /var/lib/postgresql/data /var/run/postgresql /var/log/supervisor /tmp/pg \
    && chown -R 1000:1000 /var/lib/postgresql /var/run/postgresql /var/log/supervisor /app /tmp/pg \
    && chmod 0700 /var/lib/postgresql/data

COPY entrypoint.sh /usr/local/bin/sidecar-entrypoint.sh
COPY backup.sh /usr/local/bin/backup.sh
COPY supervisord.conf /etc/supervisord.conf
RUN chmod +x /usr/local/bin/sidecar-entrypoint.sh /usr/local/bin/backup.sh \
    && chown 1000:1000 /etc/supervisord.conf

ENV PGDATA=/var/lib/postgresql/data \
    POSTGRES_USER=myapi \
    POSTGRES_DB=myapi \
    BACKUP_DIR=/backup \
    BACKUP_INTERVAL_MIN=30 \
    HOME=/var/lib/postgresql \
    SERVER_HOST=0.0.0.0 \
    SERVER_PORT=7860 \
    LOG_OUTPUT_TO_FILE=false \
    LOG_OUTPUT_TO_STDOUT=true \
    TZ=Asia/Shanghai \
    SERVER_MODE=release \
    RUN_MODE=standard \
    DATA_DIR=/app/data \
    DATABASE_HOST=127.0.0.1 \
    DATABASE_PORT=5432 \
    DATABASE_SSLMODE=disable \
    REDIS_HOST=127.0.0.1 \
    REDIS_PORT=6379 \
    REDIS_ENABLE_TLS=false

EXPOSE 7860

USER 1000
WORKDIR /app

ENTRYPOINT ["/usr/local/bin/sidecar-entrypoint.sh"]