Report this

What is the reason for this report?

Solution to indefinite Python builds on App platform

Posted on November 11, 2021

There is an ongoing issue with using Python Cloud Native buildpacks on the App platform with very long or indefinite build times as noted by multiple users.

A working solution is using a Docker build process instead. Simply including a Dockerfile in the root of the app will kick off a Docker build process which completes fairly quickly.

I banged my head on the wall for a while trying to get a Django working on DO App, hopefully this solution will save others the headache.

Example Dockerfile: https://github.com/wagtail/docker-wagtail-develop/blob/master/Dockerfile



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Switching to a Docker-based build process on the DigitalOcean App Platform can indeed be a good workaround for issues related to build times or other limitations with Cloud Native Buildpacks, especially for Python and Django applications. Using Docker offers more control over the environment and dependencies, potentially leading to faster and more predictable builds.

Here’s a simplified step-by-step guide to setting up a Django application with a Dockerfile, inspired by the example you mentioned:

Step 1: Create a Dockerfile

In the root of your Django project, create a Dockerfile that specifies how to build your application. Here’s a basic example that you can adapt:

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /app

# Install dependencies
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

# Copy the current directory contents into the container at /app
COPY . /app/

# Collect static files
RUN python manage.py collectstatic --noinput

# Run the application
CMD gunicorn your_project_name.wsgi:application --bind 0.0.0.0:$PORT

Make sure to replace your_project_name with the name of your Django project. This Dockerfile uses Gunicorn as the WSGI HTTP Server to serve the Django application. Ensure Gunicorn is included in your requirements.txt.

Step 2: Define requirements.txt

Ensure you have a requirements.txt file in your project root that lists all your Python dependencies, including Django and Gunicorn.

Django>=3.0,<4.0
gunicorn>=20.0,<21.0
# Other dependencies

Step 3: Configure Your App on DigitalOcean App Platform

  1. Push Your Code: Make sure your code, including the Dockerfile and requirements.txt, is pushed to a Git repository.
  2. Create a New App: In the DigitalOcean App Platform, create a new app and link it to your Git repository.
  3. Choose Dockerfile: The App Platform should automatically detect the Dockerfile and configure the build accordingly.
  4. Set Environment Variables: If your Django app requires environment variables (like DJANGO_SECRET_KEY or database credentials), add them in the App Platform’s settings under the Environment Variables section.
  5. Deploy: Launch your app. The App Platform will use the Dockerfile for the build and deployment process.

After deploying, monitor the build and deployment process in the DigitalOcean App Platform dashboard. If there are any issues, the logs provided there can be very helpful for troubleshooting.

Using Docker can not only resolve issues related to build times but also provides a more flexible environment that might be closer to your production setup or specific requirements. This approach is particularly useful when dealing with complex dependencies or when you need to ensure consistency across development, testing, and production environments.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.