Hosting Reflex on your own infra using Docker for efficient containerization.
Tom Gotsman
·
Reflex offers a powerful way to build reactive web apps in pure Python, complete with one-line deployment for easy hosting. For those who want to self host on their own infra we'll explore how to leverage Docker to containerize and deploy your app, taking your Reflex app from development to production.
This tutorial assumes you have a Reflex app ready for deployment and Docker Compose installed on your machine. If you haven't already, you can install Docker Compose by following the instructions on the Docker website.
The first file is the compose.yml file. This file will define the services that will be run in the Docker containers and makes them work together. Here is an example of a compose.yml file:
To run your Dockerized Reflex app, navigate to the top level of your app and run the following command:
Now check out localhost:3000.
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
When you make changes to your app and want the changes to be reflected in the docker containers you will need to rebuild the images using docker compose with the build flag:
Note: the following commands were used on a EC2 Amazon Linux vm, your commands may differ slightly.
To deploy your Dockerized Reflex app to a remote server, you will need to push your Docker images to a container registry like Docker Hub or Amazon ECR. Once your images are in the registry, you can pull them onto your remote server and run them using Docker Compose.
Here are the steps to deploy your Dockerized Reflex app to a remote server:
1- Build and tag your image
example_app is the name of the repository. -f is used to specify the file that we want to build into an image. -t is for tagging and is used to name the docker image. After the colon is our tag, which we normally use to do versioning.
--platform linux/amd64 is setting what platform this docker image will run on, this may differ depending on what machine you are going to host this on.
2- Push image to your remote repository
Ensure that you authenticate with your remote repository, for AWS it may look something like this:
Now repeat the commands above with the web.Dockerfile to build and push these to the remote repository.
Note you will likely need to update your web.Dockerfile to include the API_URL for the hostname of where you are hosting