Skip to navigationSkip to main contentSkip to footerScaleway DocsAsk our AI
Ask our AI

Hosting a Django web app with Serverless Containers

deploy
host
website
webapp
python
django
containerize
application
docker
dockerfile

Django is a robust Python framework designed for rapid development of secure and maintainable web applications. Hosting Django on Scaleway Serverless Containers provides automatic scaling, simplified deployment, and reduced infrastructure management. This deployment method allows your application to efficiently handle varying workloads, while you can optimize resource usage, and focus on writing code and delivering features rather than maintaining servers.

Before you start

To complete the actions presented below, you must have:

Create and host a basic Django web application

To host a Django web application on Scaleway Serverless Containers, you need to create a Django project, define your views and URLs, and containerize it using a production-ready Dockerfile. The container will run a WSGI server (like Gunicorn) to serve your app. After building the image locally, push it to the Scaleway Container Registry and deploy it via Scaleway Serverless Containers.

Create your app and test it locally

  1. In a terminal, create a new directory and navigate into it:

    mkdir my-django-app
    cd my-django-app
  2. Create a virtual environment and install Django:

  3. Start a new Django project:

    django-admin startproject myproject .
  4. Verify the project structure:

    my-django-app/
    ├── myproject/
    │   ├── __init__.py
    │   ├── settings.py        # Configuration
    │   ├── urls.py            # URL routing
    │   └── wsgi.py            # WSGI application entry point
    ├── manage.py              # CLI for Django commands
    └── venv/                  # Virtual environment (excluded from Docker)
  5. Create a requirements.txt file and add the following code to it:

    Django==5.1.*
    gunicorn==21.2.*
  6. Run the following command to test the app locally:

    python manage.py runserver

    Visit http://localhost:8000 to see the default Django welcome page.

Build the app image and push it to Scaleway Container Registry

Before building and pushing your image, ensure you have created a Scaleway Container Registry namespace, and logged into it with the Docker CLI.

  1. Create a Dockerfile at the root of your Django app:

    # Use a lightweight Python image
    FROM python:3.11-slim
    
    # Set working directory
    WORKDIR /app
    
    # Install system dependencies
    RUN apt-get update && apt-get install -y --no-install-recommends \
        && rm -rf /var/lib/apt/lists/*
    
    # Copy and install Python dependencies
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    
    # Copy the rest of the application
    COPY . .
    
    # Expose port
    EXPOSE 8000
    
    # Run Gunicorn as the entrypoint
    CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi:application"]
  2. Edit the settings.py file of your project with the code below to allow the Scaleway endpoint corresponding to your region, and remove unwanted endpoints:

    ALLOWED_HOSTS = [
    '.fr-par.scw.cloud',
    '.nl-ams.scw.cloud',
    '.pl-waw.scw.cloud'
    ]
  3. Build, tag, and push the image to Scaleway Container Registry:

    docker build \
      --platform linux/amd64 \
      --push \
      -t <CONTAINER_REGISTRY_ENDPOINT>/my-django-app:latest .
    Note

    You can find your Container Registry endpoint in the Overview tab of your Container Registry namespace in the Scaleway console.

Deploy your app using Serverless Containers

  1. Deploy a Serverless Container with the following settings:

    • Registry: Scaleway Container Registry
    • Registry namespace: The namespace where you pushed your image
    • Container port: 8000 as it is the port exposed in the dockerfile.
    • Resources: 500 mVCPU and 512 MB memory
    • Autoscaling: Set minimum scale to 1 to avoid cold starts (optional)

    Deployment may take up to a minute.

  2. Once the container is ready, click the container endpoint in the Overview tab. Your Django app will be live and accessible to anyone with the link.

Going further

  • You can deploy an existing Django project by building and pushing its container image as shown above.
  • Add a custom domain to your deployed app.
Questions?

Visit our Help Center and find the answers to your most frequent questions.

Visit Help Center
No Results