|
1 | | -# Reflex Docker Container |
| 1 | +# Reflex Docker Examples |
2 | 2 |
|
3 | | -This example describes how to create and use a container image for Reflex with your own code. |
| 3 | +This directory contains several examples of how to deploy Reflex apps using docker. |
4 | 4 |
|
5 | | -## Update Requirements |
| 5 | +In all cases, ensure that your `requirements.txt` file is up to date and |
| 6 | +includes the `reflex` package. |
6 | 7 |
|
7 | | -The `requirements.txt` includes the reflex package which is needed to install |
8 | | -Reflex framework. If you use additional packages in your project you have to add |
9 | | -this in the `requirements.txt` first. Copy the `Dockerfile`, `.dockerignore` and |
10 | | -the `requirements.txt` file in your project folder. |
| 8 | +## `simple-two-port` |
11 | 9 |
|
12 | | -## Build Simple Reflex Container Image |
| 10 | +The most basic production deployment exposes two HTTP ports and relies on an |
| 11 | +existing load balancer to forward the traffic appropriately. |
13 | 12 |
|
14 | | -The main `Dockerfile` is intended to build a very simple, single container deployment that runs |
15 | | -the Reflex frontend and backend together, exposing ports 3000 and 8000. |
| 13 | +## `simple-one-port` |
16 | 14 |
|
17 | | -To build your container image run the following command: |
| 15 | +This deployment exports the frontend statically and serves it via a single HTTP |
| 16 | +port using Caddy. This is useful for platforms that only support a single port |
| 17 | +or where running a node server in the container is undesirable. |
18 | 18 |
|
19 | | -```bash |
20 | | -docker build -t reflex-app:latest . |
21 | | -``` |
| 19 | +## `production-compose` |
22 | 20 |
|
23 | | -## Start Container Service |
| 21 | +This deployment is intended for use with a standalone VPS that is only hosting a |
| 22 | +single Reflex app. It provides the entire stack in a single `compose.yaml` |
| 23 | +including a webserver, one or more backend instances, redis, and a postgres |
| 24 | +database. |
24 | 25 |
|
25 | | -Finally, you can start your Reflex container service as follows: |
| 26 | +## `production-app-platform` |
26 | 27 |
|
27 | | -```bash |
28 | | -docker run -it --rm -p 3000:3000 -p 8000:8000 --name app reflex-app:latest |
29 | | -``` |
30 | | - |
31 | | -It may take a few seconds for the service to become available. |
32 | | - |
33 | | -Access your app at http://localhost:3000. |
34 | | - |
35 | | -Note that this container has _no persistence_ and will lose all data when |
36 | | -stopped. You can use bind mounts or named volumes to persist the database and |
37 | | -uploaded_files directories as needed. |
38 | | - |
39 | | -# Production Service with Docker Compose and Caddy |
40 | | - |
41 | | -An example production deployment uses automatic TLS with Caddy serving static files |
42 | | -for the frontend and proxying requests to both the frontend and backend. |
43 | | - |
44 | | -Copy the following files to your project directory: |
45 | | - * `compose.yaml` |
46 | | - * `compose.prod.yaml` |
47 | | - * `compose.tools.yaml` |
48 | | - * `prod.Dockerfile` |
49 | | - * `Caddy.Dockerfile` |
50 | | - * `Caddyfile` |
51 | | - |
52 | | -The production app container, based on `prod.Dockerfile`, builds and exports the |
53 | | -frontend statically (to be served by Caddy). The resulting image only runs the |
54 | | -backend service. |
55 | | - |
56 | | -The `webserver` service, based on `Caddy.Dockerfile`, copies the static frontend |
57 | | -and `Caddyfile` into the container to configure the reverse proxy routes that will |
58 | | -forward requests to the backend service. Caddy will automatically provision TLS |
59 | | -for localhost or the domain specified in the environment variable `DOMAIN`. |
60 | | - |
61 | | -This type of deployment should use less memory and be more performant since |
62 | | -nodejs is not required at runtime. |
63 | | - |
64 | | -## Customize `Caddyfile` (optional) |
65 | | - |
66 | | -If the app uses additional backend API routes, those should be added to the |
67 | | -`@backend_routes` path matcher to ensure they are forwarded to the backend. |
68 | | - |
69 | | -## Build Reflex Production Service |
70 | | - |
71 | | -During build, set `DOMAIN` environment variable to the domain where the app will |
72 | | -be hosted! (Do not include http or https, it will always use https). |
73 | | - |
74 | | -**If `DOMAIN` is not provided, the service will default to `localhost`.** |
75 | | - |
76 | | -```bash |
77 | | -DOMAIN=example.com docker compose build |
78 | | -``` |
79 | | - |
80 | | -This will build both the `app` service from the `prod.Dockerfile` and the `webserver` |
81 | | -service via `Caddy.Dockerfile`. |
82 | | - |
83 | | -## Run Reflex Production Service |
84 | | - |
85 | | -```bash |
86 | | -DOMAIN=example.com docker compose up |
87 | | -``` |
88 | | - |
89 | | -The app should be available at the specified domain via HTTPS. Certificate |
90 | | -provisioning will occur automatically and may take a few minutes. |
91 | | - |
92 | | -### Data Persistence |
93 | | - |
94 | | -Named docker volumes are used to persist the app database (`db-data`), |
95 | | -uploaded_files (`upload-data`), and caddy TLS keys and certificates |
96 | | -(`caddy-data`). |
97 | | - |
98 | | -## More Robust Deployment |
99 | | - |
100 | | -For a more robust deployment, consider bringing the service up with |
101 | | -`compose.prod.yaml` which includes postgres database and redis cache, allowing |
102 | | -the backend to run with multiple workers and service more requests. |
103 | | - |
104 | | -```bash |
105 | | -DOMAIN=example.com docker compose -f compose.yaml -f compose.prod.yaml up -d |
106 | | -``` |
107 | | - |
108 | | -Postgres uses its own named docker volume for data persistence. |
109 | | - |
110 | | -## Admin Tools |
111 | | - |
112 | | -When needed, the services in `compose.tools.yaml` can be brought up, providing |
113 | | -graphical database administration (Adminer on http://localhost:8080) and a |
114 | | -redis cache browser (redis-commander on http://localhost:8081). It is not recommended |
115 | | -to deploy these services if they are not in active use. |
116 | | - |
117 | | -```bash |
118 | | -DOMAIN=example.com docker compose -f compose.yaml -f compose.prod.yaml -f compose.tools.yaml up -d |
119 | | -``` |
120 | | - |
121 | | -# Container Hosting |
122 | | - |
123 | | -Most container hosting services automatically terminate TLS and expect the app |
124 | | -to be listening on a single port (typically `$PORT`). |
125 | | - |
126 | | -To host a Reflex app on one of these platforms, like Google Cloud Run, Render, |
127 | | -Railway, etc, use `app.Dockerfile` to build a single image containing a reverse |
128 | | -proxy that will serve that frontend as static files and proxy requests to the |
129 | | -backend for specific endpoints. |
130 | | - |
131 | | -If the chosen platform does not support buildx and thus heredoc, you can copy |
132 | | -the Caddyfile configuration into a separate Caddyfile in the root of the |
133 | | -project. |
| 28 | +This example deployment is intended for use with App hosting platforms, like |
| 29 | +Azure, AWS, or Google Cloud Run. It is the backend of the deployment, which |
| 30 | +depends on a separately hosted redis instance and static frontend deployment. |
0 commit comments