1
0

Dockerfile 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # This Dockerfile is used to deploy a single-container Reflex app instance
  2. # to services like Render, Railway, Heroku, GCP, and others.
  3. # If the service expects a different port, provide it here (f.e Render expects port 10000)
  4. ARG PORT=8080
  5. # Only set for local/direct access. When TLS is used, the API_URL is assumed to be the same as the frontend.
  6. ARG API_URL
  7. # It uses a reverse proxy to serve the frontend statically and proxy to backend
  8. # from a single exposed port, expecting TLS termination to be handled at the
  9. # edge by the given platform.
  10. FROM python:3.13 as builder
  11. RUN mkdir -p /app/.web
  12. RUN python -m venv /app/.venv
  13. ENV PATH="/app/.venv/bin:$PATH"
  14. WORKDIR /app
  15. # Install python app requirements and reflex in the container
  16. COPY requirements.txt .
  17. RUN pip install -r requirements.txt
  18. # Install reflex helper utilities like bun/node
  19. COPY rxconfig.py ./
  20. RUN reflex init
  21. # Install pre-cached frontend dependencies (if exist)
  22. COPY *.web/bun.lockb *.web/package.json .web/
  23. RUN if [ -f .web/bun.lockb ]; then cd .web && ~/.local/share/reflex/bun/bin/bun install --frozen-lockfile; fi
  24. # Copy local context to `/app` inside container (see .dockerignore)
  25. COPY . .
  26. ARG PORT API_URL
  27. # Download other npm dependencies and compile frontend
  28. RUN API_URL=${API_URL:-http://localhost:$PORT} reflex export --loglevel debug --frontend-only --no-zip && mv .web/_static/* /srv/ && rm -rf .web
  29. # Final image with only necessary files
  30. FROM python:3.13-slim
  31. # Install Caddy and redis server inside image
  32. RUN apt-get update -y && apt-get install -y caddy redis-server && rm -rf /var/lib/apt/lists/*
  33. ARG PORT API_URL
  34. ENV PATH="/app/.venv/bin:$PATH" PORT=$PORT API_URL=${API_URL:-http://localhost:$PORT} REDIS_URL=redis://localhost PYTHONUNBUFFERED=1
  35. WORKDIR /app
  36. COPY --from=builder /app /app
  37. COPY --from=builder /srv /srv
  38. # Needed until Reflex properly passes SIGTERM on backend.
  39. STOPSIGNAL SIGKILL
  40. EXPOSE $PORT
  41. # Apply migrations before starting the backend.
  42. CMD [ -d alembic ] && reflex db migrate; \
  43. caddy start && \
  44. redis-server --daemonize yes && \
  45. exec reflex run --env prod --backend-only