Dockerfile 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # This Dockerfile is used to deploy a single-container Reflex app instance
  2. # to services like Render, Railway, Heroku, GCP, and others.
  3. # It uses a reverse proxy to serve the frontend statically and proxy to backend
  4. # from a single exposed port, expecting TLS termination to be handled at the
  5. # edge by the given platform.
  6. FROM python:3.11
  7. # If the service expects a different port, provide it here (f.e Render expects port 10000)
  8. ARG PORT=8080
  9. # Only set for local/direct access. When TLS is used, the API_URL is assumed to be the same as the frontend.
  10. ARG API_URL
  11. ENV PORT=$PORT API_URL=${API_URL:-http://localhost:$PORT} REDIS_URL=redis://localhost PYTHONUNBUFFERED=1
  12. # Install Caddy and redis server inside image
  13. RUN apt-get update -y && apt-get install -y caddy redis-server && rm -rf /var/lib/apt/lists/*
  14. WORKDIR /app
  15. # Copy local context to `/app` inside container (see .dockerignore)
  16. COPY . .
  17. # Install app requirements and reflex in the container
  18. RUN pip install -r requirements.txt
  19. # Deploy templates and prepare app
  20. RUN reflex init
  21. # Download all npm dependencies and compile frontend
  22. RUN reflex export --frontend-only --no-zip && mv .web/_static/* /srv/ && rm -rf .web
  23. # Needed until Reflex properly passes SIGTERM on backend.
  24. STOPSIGNAL SIGKILL
  25. EXPOSE $PORT
  26. # Apply migrations before starting the backend.
  27. CMD [ -d alembic ] && reflex db migrate; \
  28. caddy start && \
  29. redis-server --daemonize yes && \
  30. exec reflex run --env prod --backend-only