Dockerfile 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # This docker file is intended to be used with docker compose to deploy a production
  2. # instance of a Reflex app.
  3. # Stage 1: init
  4. FROM python:3.13 as init
  5. ARG uv=/root/.local/bin/uv
  6. # Install `uv` for faster package bootstrapping
  7. ADD --chmod=755 https://astral.sh/uv/install.sh /install.sh
  8. RUN /install.sh && rm /install.sh
  9. # Copy local context to `/app` inside container (see .dockerignore)
  10. WORKDIR /app
  11. COPY . .
  12. RUN mkdir -p /app/data /app/uploaded_files
  13. # Create virtualenv which will be copied into final container
  14. ENV VIRTUAL_ENV=/app/.venv
  15. ENV PATH="$VIRTUAL_ENV/bin:$PATH"
  16. RUN $uv venv
  17. # Install app requirements and reflex inside virtualenv
  18. RUN $uv pip install -r requirements.txt
  19. # Deploy templates and prepare app
  20. RUN reflex init
  21. # Export static copy of frontend to /app/.web/_static
  22. RUN reflex export --frontend-only --no-zip
  23. # Copy static files out of /app to save space in backend image
  24. RUN mv .web/_static /tmp/_static
  25. RUN rm -rf .web && mkdir .web
  26. RUN mv /tmp/_static .web/_static
  27. # Stage 2: copy artifacts into slim image
  28. FROM python:3.13-slim
  29. WORKDIR /app
  30. RUN adduser --disabled-password --home /app reflex
  31. COPY --chown=reflex --from=init /app /app
  32. # Install libpq-dev for psycopg (skip if not using postgres).
  33. RUN apt-get update -y && apt-get install -y libpq-dev && rm -rf /var/lib/apt/lists/*
  34. USER reflex
  35. ENV PATH="/app/.venv/bin:$PATH" PYTHONUNBUFFERED=1
  36. # Needed until Reflex properly passes SIGTERM on backend.
  37. STOPSIGNAL SIGKILL
  38. # Always apply migrations before starting the backend.
  39. CMD [ -d alembic ] && reflex db migrate; \
  40. exec reflex run --env prod --backend-only