Dockerfile 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # This docker file is intended to be used with container hosting services
  2. #
  3. # After deploying this image, get the URL pointing to the backend service
  4. # and run API_URL=https://path-to-my-container.example.com reflex export frontend
  5. # then copy the contents of `frontend.zip` to your static file server (github pages, s3, etc).
  6. #
  7. # Azure Static Web App example:
  8. # npx @azure/static-web-apps-cli deploy --env production --app-location .web/_static
  9. #
  10. # For dynamic routes to function properly, ensure that 404s are redirected to /404 on the
  11. # static file host (for github pages, this works out of the box; remember to create .nojekyll).
  12. #
  13. # For azure static web apps, add `staticwebapp.config.json` to to `.web/_static` with the following:
  14. # {
  15. # "responseOverrides": {
  16. # "404": {
  17. # "rewrite": "/404.html"
  18. # }
  19. # }
  20. # }
  21. #
  22. # Note: many container hosting platforms require amd64 images, so when building on an M1 Mac
  23. # for example, pass `docker build --platform=linux/amd64 ...`
  24. # Stage 1: init
  25. FROM python:3.13 as init
  26. ARG uv=/root/.local/bin/uv
  27. # Install `uv` for faster package bootstrapping
  28. ADD --chmod=755 https://astral.sh/uv/install.sh /install.sh
  29. RUN /install.sh && rm /install.sh
  30. # Copy local context to `/app` inside container (see .dockerignore)
  31. WORKDIR /app
  32. COPY . .
  33. RUN mkdir -p /app/data /app/uploaded_files
  34. # Create virtualenv which will be copied into final container
  35. ENV VIRTUAL_ENV=/app/.venv
  36. ENV PATH="$VIRTUAL_ENV/bin:$PATH"
  37. RUN $uv venv
  38. # Install app requirements and reflex inside virtualenv
  39. RUN $uv pip install -r requirements.txt
  40. # Deploy templates and prepare app
  41. RUN reflex init
  42. # Stage 2: copy artifacts into slim image
  43. FROM python:3.13-slim
  44. WORKDIR /app
  45. RUN adduser --disabled-password --home /app reflex
  46. COPY --chown=reflex --from=init /app /app
  47. # Install libpq-dev for psycopg (skip if not using postgres).
  48. RUN apt-get update -y && apt-get install -y libpq-dev && rm -rf /var/lib/apt/lists/*
  49. USER reflex
  50. ENV PATH="/app/.venv/bin:$PATH" PYTHONUNBUFFERED=1
  51. # Needed until Reflex properly passes SIGTERM on backend.
  52. STOPSIGNAL SIGKILL
  53. # Always apply migrations before starting the backend.
  54. CMD [ -d alembic ] && reflex db migrate; \
  55. exec reflex run --env prod --backend-only --backend-port ${PORT:-8000}