The Struggle with Django Deployments

The Struggle with Django Deployments

Nov 10, 2024

If you plan on deploying Django to these places for free, DON'T. I've tried and there are limits and to some part, does not work. Perhaps if the Django app is just one thing, it might work with the following online services, but if you have other running services that you definitely need, then read on to find out why.

First off: It is made with love 🥰 with Django Cookiecutter

It's based off of a Cookiecutter and I love how simple and well-made it has been. A docker-compose and Dockerfile already in place for quick and easy continuous development and that it works on my machine™️ that its just a no-brainer to use it. It also comes in extra features like Celery Workers for scheduled cron jobs and tasks that one may need in a "background" running, or if you wanna test and send emails with Mailpit without risking your own domain name and getting blacklisted by other mail servers.

So with that said, the deployment options are, as stated by the docs, is limited to actually provisioning in the big cloud services (the usual AWS, GCP, Azure) and a few others like PythonAnywhere, and Heroku. Considering that my Django app is quite small and may not need a lot of traffic, I figure, "Why not try host this on free services?" It has been THREE DAYS of trial by fire. Here is what I found out.

Render.com's Free Limitation

It goes without saying, you can't expect a lot out of free services. So Render only has free service on Web Service, Postgresql database instance and Redis. But I need a background worker for my Celery. And even if I tried running Celery as a Web Service, I get some weird libGL.so.1 error missing.

Gave up and move on.

Fly.io won't let it start for Mysterious Reasons 👻

Nevermind that Redis is a paid service. I made do with a CloudAMQP service. This tiny app is not going to be very busy on a daily basis. And if it does, I don't mind paying for it. What I want to do is deploy this service on a Freemium service first and as it scales, pay the difference. But after a day of just trying out different regions, change in commands, change in environment variables, change in scales, edit fly.toml file, machine restarts, nothing. It won't start and the logs won't tell me why.

[PM05] failed to connect to machine: gave up after 15 attempts (in 24.537989991s)

NEXT!

Django as a Serverless Function on Vercel

The deployment guides on Vercel are all in JS Frameworks, except for a Serverless Function, they allow Python runtime. However, this means that it may not be a properly running Django admin and pages running on Vercel but as a function. I even tried asking their very helpful RAG AI support to see what it can advise on deploying a Django instance with a Celery instance. Great support, bad news for my project though.

Full answer in quote:

Deploying a Django project with a Celery instance on Vercel requires some considerations, as Vercel does not support running Docker instances directly. However, you can deploy your Django application using Vercel's serverless functions, but for Celery, you would need to use an external service or a different approach since Vercel does not support background task processing like Celery natively.

Here's a general approach you can take:

  1. Deploy Django on Vercel: You can deploy your Django application using Vercel's Python runtime. Ensure your Django app is set up to handle HTTP requests via serverless functions. You can refer to Vercel's documentation on deploying Python applications for guidance.

  2. External Celery Worker: Since Vercel does not support running persistent processes like Celery workers, you will need to deploy your Celery workers on a different platform that supports long-running processes. This could be a cloud provider like AWS, Google Cloud, or Heroku.

  3. Docker for Local Development: You can use your Dockerfile for local development to ensure your Django and Celery setup works as expected. This can help you simulate the production environment locally.

  4. Task Queue: Use a task queue service like Redis or RabbitMQ, which can be hosted externally, to manage your Celery tasks. Ensure your Django app on Vercel can communicate with this service.

  5. Environment Variables: Configure your environment variables on Vercel to ensure your Django app can connect to the external task queue and any other services it needs.

This setup allows you to leverage Vercel for deploying your Django application while using another service to handle Celery tasks.

TL:DR; Deploy it on a normal VPS like everybody else

So what does this mean for my Django + Celery project deployment? It means I can't deploy this stack for free. And that also means, just buying a VPS and do the set up the normal way.

If you have any other service that offers freemium plans that you want me to try and deploy my project to, let me know in the comments below. I'm always curious on how far a free service can get us.

Vous aimez cette publication ?

Achetez un café à Qoyyuum

Plus de Qoyyuum