api / Dockerfile
cpa01's picture
Create Dockerfile
1313c62 verified
Raw History Blame Contribute Delete
2.03 kB
# --- STAGE 1: Builder ---
FROM node:20-slim AS builder
WORKDIR /app
# Install build tools yang dibutuhkan untuk native modules & git
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
git \
python3 \
make \
g++ \
&& rm -rf /var/lib/apt/lists/*
# Clone repository
RUN git clone https://github.com/decolua/9router.git .
# Install dependencies & build aplikasi
RUN npm install --legacy-peer-deps
RUN npm run build
# --- STAGE 2: Runner (Production) ---
FROM node:20-slim AS runner
WORKDIR /app
# Install ca-certificates, curl, dan gunakan script resmi Cloudflare
# Ini otomatis mendeteksi arsitektur CPU (AMD64 / ARM64) di Hugging Face
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl \
&& curl -L https://pkg.cloudflare.com/cloudflare-main.gpg -o /usr/share/keyrings/cloudflare-main.gpg \
&& echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared bookworm main" | tee /etc/apt/sources.list.d/cloudflared.list \
&& apt-get update && apt-get install -y cloudflared \
&& apt-get purge -y --auto-remove curl \
&& rm -rf /var/lib/apt/lists/*
# Set environment ke production
ENV NODE_ENV=production
ENV PORT=7860
ENV HOSTNAME=0.0.0.0
# Hanya salin hasil build dan file yang dibutuhkan dari stage builder
COPY --from=builder /app /app
# Buat startup script secara inline untuk menjalankan Tunnel dan Node app
RUN echo '#!/bin/sh\n\
if [ -z "$TUNNEL_TOKEN" ]; then\n\
echo "Menjalankan Cloudflare Quick Tunnel (Random URL)..."\n\
cloudflared tunnel --url http://127.0.0.1:$PORT &\n\
else\n\
echo "Menjalankan Cloudflare Tunnel dengan Token..."\n\
cloudflared tunnel --no-autoupdate run --token $TUNNEL_TOKEN &\n\
fi\n\
echo "Menjalankan aplikasi Node.js..."\n\
exec npm run start' > /app/start.sh
# Berikan akses eksekusi pada script
RUN chmod +x /app/start.sh
EXPOSE 7860
# Gunakan script sebagai command utama
CMD ["/app/start.sh"]