|
| 1 | +--- |
| 2 | +title: Hosting a Django web app with Serverless Containers |
| 3 | +description: This page provides guidelines on how to host and deploy dynamic Django web applications using Scaleway Serverless Containers |
| 4 | +tags: deploy host website webapp python django containerize application docker dockerfile |
| 5 | +products: |
| 6 | + - containers |
| 7 | + - container-registry |
| 8 | +dates: |
| 9 | + validation: 2025-10-06 |
| 10 | + posted: 2025-10-06 |
| 11 | + validation_frequency: 12 |
| 12 | +difficulty: beginner |
| 13 | +usecase: |
| 14 | + - application-hosting |
| 15 | + - deploy-external-software |
| 16 | + - website-hosting |
| 17 | +ecosystem: |
| 18 | + - third-party |
| 19 | +--- |
| 20 | + |
| 21 | +import Requirements from '@macros/iam/requirements.mdx' |
| 22 | + |
| 23 | +<Requirements /> |
| 24 | + |
| 25 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 26 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 27 | +- Installed [Docker](https://docs.docker.com/get-started/get-docker/) or [Docker Engine](https://docs.docker.com/engine/install/) |
| 28 | +- [Created a Scaleway Container Registry namespace](/container-registry/how-to/create-namespace/) and [logged into it](/container-registry/how-to/connect-docker-cli/) |
| 29 | + |
| 30 | +## Create and host a basic Django web application |
| 31 | + |
| 32 | +To host a [Django](https://www.djangoproject.com/) 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](/container-registry/) and deploy it via [Scaleway Serverless Containers](/serverless-containers/). |
| 33 | + |
| 34 | +### Create your app and test it locally |
| 35 | + |
| 36 | +1. In a terminal, create a new directory and navigate into it: |
| 37 | + ```bash |
| 38 | + mkdir my-django-app |
| 39 | + cd my-django-app |
| 40 | + ``` |
| 41 | + |
| 42 | +2. Create a virtual environment and install Django: |
| 43 | + |
| 44 | + <Tabs id="python-venv"> |
| 45 | + <TabsTab label="Linux/macOS"> |
| 46 | + ```bash |
| 47 | + python3 -m venv venv |
| 48 | + source venv/bin/activate |
| 49 | + pip install django gunicorn |
| 50 | + ``` |
| 51 | + </TabsTab> |
| 52 | + <TabsTab label="Windows"> |
| 53 | + ```bash |
| 54 | + python -m venv venv |
| 55 | + venv\Scripts\activate |
| 56 | + pip install django gunicorn |
| 57 | + ``` |
| 58 | + </TabsTab> |
| 59 | + </Tabs> |
| 60 | + |
| 61 | +3. Start a new Django project: |
| 62 | + ```bash |
| 63 | + django-admin startproject myproject . |
| 64 | + ``` |
| 65 | + |
| 66 | +4. Verify the project structure: |
| 67 | + ``` |
| 68 | + my-django-app/ |
| 69 | + ├── myproject/ |
| 70 | + │ ├── __init__.py |
| 71 | + │ ├── settings.py # Configuration |
| 72 | + │ ├── urls.py # URL routing |
| 73 | + │ └── wsgi.py # WSGI application entry point |
| 74 | + ├── manage.py # CLI for Django commands |
| 75 | + ├── venv/ # Virtual environment (excluded from Docker) |
| 76 | + └── requirements.txt # To be created (lists dependencies) |
| 77 | + ``` |
| 78 | + |
| 79 | +5. Create a `requirements.txt` file: |
| 80 | + ```txt |
| 81 | + Django==5.1.* |
| 82 | + gunicorn==21.2.* |
| 83 | + ``` |
| 84 | + |
| 85 | +6. Test the app locally: |
| 86 | + ```bash |
| 87 | + python manage.py runserver |
| 88 | + ``` |
| 89 | + Visit [http://localhost:8000](http://localhost:8000) to see the default Django welcome page. |
| 90 | + |
| 91 | +### Build the app image and push it to Scaleway Container Registry |
| 92 | + |
| 93 | +Before building and pushing your image, ensure you have [created a Scaleway Container Registry namespace](/container-registry/how-to/create-namespace/), and [logged into it with the Docker CLI](/container-registry/how-to/connect-docker-cli/) |
| 94 | + |
| 95 | +1. Create a `Dockerfile` at the root of your project: |
| 96 | + ```dockerfile |
| 97 | + # Use a lightweight Python image |
| 98 | + FROM python:3.11-slim |
| 99 | + |
| 100 | + # Set working directory |
| 101 | + WORKDIR /app |
| 102 | + |
| 103 | + # Install system dependencies |
| 104 | + RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 105 | + && rm -rf /var/lib/apt/lists/* |
| 106 | + |
| 107 | + # Copy and install Python dependencies |
| 108 | + COPY requirements.txt . |
| 109 | + RUN pip install --no-cache-dir -r requirements.txt |
| 110 | + |
| 111 | + # Copy the rest of the application |
| 112 | + COPY . . |
| 113 | + |
| 114 | + # Expose port |
| 115 | + EXPOSE 8000 |
| 116 | + |
| 117 | + # Run Gunicorn as the entrypoint |
| 118 | + CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi:application"] |
| 119 | + ``` |
| 120 | + |
| 121 | +2. Build, tag, and push the image to Scaleway Container Registry: |
| 122 | + ```bash |
| 123 | + docker build \ |
| 124 | + --platform linux/amd64 \ |
| 125 | + --push \ |
| 126 | + -t <CONTAINER_REGISTRY_ENDPOINT>/my-django-app:latest . |
| 127 | + ``` |
| 128 | + <Message type="note"> |
| 129 | + You can find your Container Registry endpoint in the **Overview** tab of your Container Registry namespace in the [Scaleway console](https://console.scaleway.com/registry/namespaces). |
| 130 | + </Message> |
| 131 | + |
| 132 | +### Deploy your app using Serverless Containers |
| 133 | + |
| 134 | +1. [Deploy a Serverless Container](/serverless-containers/how-to/deploy-container/) with the following settings: |
| 135 | + - **Registry**: Scaleway Container Registry |
| 136 | + - **Registry namespace**: The namespace where you pushed your image |
| 137 | + - **Container port**: `8000` as it is the [port](/serverless-containers/reference-content/port-parameter-variable/) exposed in the dockerfile. |
| 138 | + - **Resources**: `512 mVCPU` and `512 MB` memory |
| 139 | + - **Autoscaling**: Set minimum scale to `1` to avoid [cold starts](/serverless-containers/concepts/#cold-start) (optional) |
| 140 | + |
| 141 | + Deployment may take up to a minute. |
| 142 | + |
| 143 | +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. |
| 144 | + |
| 145 | +## Going further |
| 146 | + |
| 147 | +- You can deploy an existing Django project by building and pushing its container image as shown above. |
| 148 | +- [Add a custom domain](/serverless-containers/how-to/add-a-custom-domain-to-a-container/) to your deployed app. |
| 149 | +- Secure your app by using environment variables for `SECRET_KEY` and `DEBUG` via the container’s environment settings in the console. |
| 150 | +- Integrate with a database (e.g., PostgreSQL) by connecting to a managed Scaleway Database instance or using an external provider. |
| 151 | + |
| 152 | +``` |
| 153 | +
|
| 154 | +> ✅ **Tip**: For production, always set `DEBUG=False` and use proper allowed hosts and secret key management. |
0 commit comments