Self-Hosting Python DASH Apps with Docker
Dash is a popular Python framework for building interactive web applications, and Docker is a powerful tool for containerizing and deploying these applications.
When it comes to deploying a Dash Docker app, you have two main options: self-hosting or using a cloud service like Google Cloud Run.
In this blog post, we’ll compare these two options, highlighting their respective pros and cons, and share a personal experience of unexpected billing when deploying a Dash app on Google Cloud Run.
Self-Hosting vs Google Cloud Run
Self-Hosting a Dash Docker App
- Control: Self-hosting gives you complete control over your infrastructure, including the server hardware, operating system, and software stack. This can provide greater flexibility and customization options.
- Cost: Self-hosting can be more cost-effective than using a cloud service, particularly if you already have the necessary hardware and infrastructure in place.
- Security: Self-hosting can provide greater security and privacy, as you are not relying on a third-party service to store or process your data.
- Maintenance: Self-hosting requires ongoing maintenance and upkeep of the infrastructure, which can be time-consuming and resource-intensive.
- Scalability: Self-hosting can be more challenging to scale up or down, particularly if you experience unexpected spikes in traffic or demand.
Deploying with Google Cloud Run
I already created a post to deploy with GCR a dockerized DASH app, and I discovered the hard way about one of the CONS of it.
- Scalability: Google Cloud Run provides automatic scaling, which means that your app can automatically handle increases or decreases in traffic without any manual intervention.
- Flexibility: Google Cloud Run provides flexibility in terms of the programming languages and frameworks you can use, which can be particularly useful if you are developing in multiple languages.
- Integration: Google Cloud Run integrates with other Google Cloud services, such as Cloud Storage and Cloud SQL, which can simplify deployment and management.
- Cost: Google Cloud Run can be more expensive than self-hosting, particularly if you experience high levels of traffic or demand.
- Control: Google Cloud Run limits your control over the infrastructure and software stack, which can be limiting in terms of customization options.
Why Self-Hosting DASH apps? - My experience with GCR
At the end of the last month, I received an unexpected bill for running the trip-planner app, which turned out to be much higher than I anticipated. After investigating, I realized that I had not optimized the app’s resources and configuration for Google Cloud Run, which resulted in higher costs.
This experience taught me the importance of understanding the cost structure and resource utilization of cloud services, and the need for careful planning and optimization.
At the same time, it motivated me to create this guide to help you have your Python DASH app dockerized and deployed to the internet securely with the help of Cloudflare tunnels, for which I also create a guide that you might want to follow to complete the setup.
Self-Hosting Python DASH Apps with Docker
- Step 1: Have your app ready in a repository
- Step 2: Clone it
git clone https://github.com/JAlcocerT/Py_Trip_Planner.git ./Py_Trip_Planner && cd Py_Trip_Planner
- Step 3: Have docker installed and build the Docker Image:
docker build -t py_trip_planner .
This might take a while, depending in the device where you will run it.
- Step 4: Deploy your App together with Cloudflare to expose it securely
docker run --name py_trip_planner --network tunnel -p 8050:8050 --detach py_trip_planner
version: '3.8' services: py_trip_planner: image: py_trip_planner container_name: py_trip_planner ports: - "8050:8050" networks: - tunnel restart: unless-stopped networks: tunnel: external: true
- Step 5: go to the Cloudflare UI and add the public host names as explained in the Cloudflare guide.
How can I try the App?
You can see the result from any browser at:
How can I Contribute?
I have made all the code Open Source and this is the public Github repository where I have built the code, please feel free to have a look, experiment with the code and suggest any improvements: