Reflex Cloud - Fast, secure & scalable hosting. One command to deploy.

Back to Blog

Self Hosting Reflex with Docker

Hosting Reflex on your own infra using Docker for efficient containerization.

Tom Gotsman

·

Image for blog post: Self Hosting Reflex with Docker

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.

Prerequisites

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.

Dockerizing Your Reflex App

There will only be 4 files needed to Dockerize your Reflex app:

  1. compose.yml
  2. Dockerfile
  3. web.Dockerfile
  4. nginx.conf

You need to create these files at the top level of your app, the same folder level as the rxconfig.py file. See below for an example folder structure:

compose.yml

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:

Dockerfile

The Dockerfile is used to build the Docker image for the Reflex app. Here is an example of a Dockerfile:

web.Dockerfile

The web.Dockerfile is used to build the Docker image for the web server. Here is an example of a web.Dockerfile:

nginx.conf

The nginx.conf file is used to configure the Nginx web server. Here is an example of an nginx.conf file:

Running Your Dockerized Reflex App (locally)

To run your Dockerized Reflex app, navigate to the top level of your app and run the following command:

Now check out localhost:3000.

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:

Deploying your dockerized app to a remote server

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

Update the compose.yml file

3- Add an updated the compose.yml file to pull the images from the remote repository to your remote host.

4- In side your EC2 Machine run the following commands (ignoring the comments):

More Posts

Newsletter

Get the latest updates and news about Reflex

$ pip install reflex

$ reflex init

$ reflex run

Need help? Learn how to use Reflex.

Built with Reflex