Skip to content

pavelkraleu/cloud-run-django

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django on Google Cloud Run

I am particularly fond of Google Cloud Run, but there is limited information available on running Django applications on this service. In this article, I aim to share my experiences and insights gained from deploying Django on Cloud Run.

All things I'm describing here can be seen in my repository OptiFleet.

Migrations

During the migration process, it necessary to execute a series of commands, which collectively took several minutes to complete:

./manage.py collectstatic
./manage.py migrate

To streamline this process, I created a migrate.sh script that runs these commands within the Docker container. This Docker container is the same one used to handle web requests on Cloud Run.

I incorporated this script into the Github Actions CI/CD pipeline for execution. The deployment process proceeds only if the migrations are successful.

Cloud Run Task

Cost Optimization

During my usage of Cloud Run, I encountered unexpectedly high costs associated with running instances. To address this issue, I implemented a strategy to scale Cloud Run to zero instances and start only when necessary.

Cloud Run Cost

However, I observed that the response time during the scaling process, especially when transitioning from zero running instances, was uncharacteristically high.

Cold Start Response Time

To optimize the response time during cold starts, I discovered that configuring health checks to run more frequently proved to be effective.

Health Checks Configuration

By implementing this approach, the waiting time for instances to become active significantly decreased.

Warm Start Response Time

In logs we can see something like that. In this case it took 9.6s to serve the request and health check succeeded after fifth try.
To improve that we could try to decrease the docker image size (currently 1.06GB) or some other steps to decrease image startup time.

Cloud Run Logs

About

Notes how to run Django on Google Cloud Run

Resources

Stars

Watchers

Forks