derek-thomas commited on
Commit
dc7ccc1
·
verified ·
1 Parent(s): 271d872

Trying with reflex dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +35 -35
Dockerfile CHANGED
@@ -1,51 +1,51 @@
1
- # Use Python 3.11 base image
2
- FROM python:3.11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # Create a non-root user for security
5
  RUN useradd -m -u 1000 user
6
 
7
- # Set environment variables and paths
8
- ENV PATH="/home/user/.local/bin:/app/prompt_order_experiment:$PATH"
9
-
10
- # Set work directory
11
  WORKDIR /app
12
 
13
- # Install necessary tools and dependencies as root
14
- RUN apt-get update -y && apt-get install -y \
15
- caddy \
16
- redis-server \
17
- && apt-get clean && rm -rf /var/lib/apt/lists/*
18
 
19
- # Install Python requirements as root
20
- COPY ./requirements.txt requirements.txt
21
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
22
-
23
- # Switch to the non-root user
24
- USER user
25
 
26
- # Copy application code
27
- COPY --chown=user . .
28
 
29
- # Switch back to root to perform privileged operations
30
- USER root
31
 
32
- # Compile frontend assets and move to /srv
33
  RUN reflex export --frontend-only --no-zip && mv .web/_static/* /srv/ && rm -rf .web
34
 
35
- # Needed until Reflex properly passes SIGTERM on backend.
36
- STOPSIGNAL SIGKILL
37
-
38
- # Ensure the non-root user has ownership of the app directory
39
- RUN chown -R user:user /app
40
-
41
- # Revert to non-root user for running the app
42
  USER user
43
 
44
- # Apply migrations before starting the backend (if applicable)
45
- RUN [ -d alembic ] && reflex db migrate || true
46
 
47
- # Expose the default port
48
- EXPOSE 8080
49
 
50
- # Set the entry point for the container
51
- ENTRYPOINT ["reflex", "run", "--env", "dev", "--loglevel", "debug"]
 
 
 
 
1
+ # This Dockerfile is used to deploy a single-container Reflex app instance
2
+ # to services like Render, Railway, Heroku, GCP, and others.
3
+
4
+ # It uses a reverse proxy to serve the frontend statically and proxy to backend
5
+ # from a single exposed port, expecting TLS termination to be handled at the
6
+ # edge by the given platform.
7
+ FROM python:3.13
8
+
9
+ # If the service expects a different port, provide it here (f.e Render expects port 10000)
10
+ ARG PORT=8080
11
+ # Only set for local/direct access. When TLS is used, the API_URL is assumed to be the same as the frontend.
12
+ ARG API_URL
13
+ ENV PORT=$PORT API_URL=${API_URL:-http://localhost:$PORT} REDIS_URL=redis://localhost PYTHONUNBUFFERED=1
14
+
15
+ # Install Caddy and redis server inside image
16
+ RUN apt-get update -y && apt-get install -y caddy redis-server && rm -rf /var/lib/apt/lists/*
17
 
18
  # Create a non-root user for security
19
  RUN useradd -m -u 1000 user
20
 
21
+ # Switch to the app directory
 
 
 
22
  WORKDIR /app
23
 
24
+ # Copy local context to `/app` inside container (see .dockerignore)
25
+ COPY . .
 
 
 
26
 
27
+ # Adjust permissions for the app directory to allow the non-root user to access it
28
+ RUN chown -R user:user /app
 
 
 
 
29
 
30
+ # Install app requirements and reflex in the container
31
+ RUN pip install -r requirements.txt
32
 
33
+ # Deploy templates and prepare app
34
+ RUN reflex init
35
 
36
+ # Download all npm dependencies and compile frontend
37
  RUN reflex export --frontend-only --no-zip && mv .web/_static/* /srv/ && rm -rf .web
38
 
39
+ # Switch to non-root user
 
 
 
 
 
 
40
  USER user
41
 
42
+ # Needed until Reflex properly passes SIGTERM on backend.
43
+ STOPSIGNAL SIGKILL
44
 
45
+ EXPOSE $PORT
 
46
 
47
+ # Apply migrations before starting the backend.
48
+ CMD [ -d alembic ] && reflex db migrate; \
49
+ caddy start && \
50
+ redis-server --daemonize yes && \
51
+ exec reflex run --env prod --backend-only