|
| 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 | +[Django](https://www.djangoproject.com/) is a robust Python framework designed for rapid development of secure and maintainable web applications. Hosting Django on [Scaleway Serverless Containers](/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. |
| 24 | + |
| 25 | +<Requirements /> |
| 26 | + |
| 27 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 28 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 29 | +- Installed [Docker](https://docs.docker.com/get-started/get-docker/) or [Docker Engine](https://docs.docker.com/engine/install/) |
| 30 | +- [Created a Scaleway Container Registry namespace](/container-registry/how-to/create-namespace/) and [logged into it](/container-registry/how-to/connect-docker-cli/) |
| 31 | + |
| 32 | +## Create and host a basic Django web application |
| 33 | + |
| 34 | +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/). |
| 35 | + |
| 36 | +### Create your app and test it locally |
| 37 | + |
| 38 | +1. In a terminal, create a new directory and navigate into it: |
| 39 | + ```bash |
| 40 | + mkdir my-django-app |
| 41 | + cd my-django-app |
| 42 | + ``` |
| 43 | + |
| 44 | +2. Create a virtual environment and install Django: |
| 45 | + |
| 46 | + <Tabs id="python-venv"> |
| 47 | + <TabsTab label="Linux/macOS"> |
| 48 | + ```bash |
| 49 | + python3 -m venv venv |
| 50 | + source venv/bin/activate |
| 51 | + pip install django gunicorn |
| 52 | + ``` |
| 53 | + </TabsTab> |
| 54 | + <TabsTab label="Windows"> |
| 55 | + ```bash |
| 56 | + python -m venv venv |
| 57 | + venv\Scripts\activate |
| 58 | + pip install django gunicorn |
| 59 | + ``` |
| 60 | + </TabsTab> |
| 61 | + </Tabs> |
| 62 | + |
| 63 | +3. Start a new Django project: |
| 64 | + ```bash |
| 65 | + django-admin startproject myproject . |
| 66 | + ``` |
| 67 | + |
| 68 | +4. Verify the project structure: |
| 69 | + ``` |
| 70 | + my-django-app/ |
| 71 | + ├── myproject/ |
| 72 | + │ ├── __init__.py |
| 73 | + │ ├── settings.py # Configuration |
| 74 | + │ ├── urls.py # URL routing |
| 75 | + │ └── wsgi.py # WSGI application entry point |
| 76 | + ├── manage.py # CLI for Django commands |
| 77 | + └── venv/ # Virtual environment (excluded from Docker) |
| 78 | + ``` |
| 79 | + |
| 80 | +5. Create a `requirements.txt` file and add the following code to it: |
| 81 | + ```txt |
| 82 | + Django==5.1.* |
| 83 | + gunicorn==21.2.* |
| 84 | + ``` |
| 85 | + |
| 86 | +6. Run the following command to test the app locally: |
| 87 | + ```bash |
| 88 | + python manage.py runserver |
| 89 | + ``` |
| 90 | + Visit [http://localhost:8000](http://localhost:8000) to see the default Django welcome page. |
| 91 | + |
| 92 | +### Build the app image and push it to Scaleway Container Registry |
| 93 | + |
| 94 | +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/). |
| 95 | + |
| 96 | +1. Create a `Dockerfile` at the root of your Django app: |
| 97 | + ```dockerfile |
| 98 | + # Use a lightweight Python image |
| 99 | + FROM python:3.11-slim |
| 100 | + |
| 101 | + # Set working directory |
| 102 | + WORKDIR /app |
| 103 | + |
| 104 | + # Install system dependencies |
| 105 | + RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 106 | + && rm -rf /var/lib/apt/lists/* |
| 107 | + |
| 108 | + # Copy and install Python dependencies |
| 109 | + COPY requirements.txt . |
| 110 | + RUN pip install --no-cache-dir -r requirements.txt |
| 111 | + |
| 112 | + # Copy the rest of the application |
| 113 | + COPY . . |
| 114 | + |
| 115 | + # Expose port |
| 116 | + EXPOSE 8000 |
| 117 | + |
| 118 | + # Run Gunicorn as the entrypoint |
| 119 | + CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi:application"] |
| 120 | + ``` |
| 121 | + |
| 122 | +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: |
| 123 | + |
| 124 | + ```py |
| 125 | + ALLOWED_HOSTS = [ |
| 126 | + '.fr-par.scw.cloud', |
| 127 | + '.nl-ams.scw.cloud', |
| 128 | + '.pl-waw.scw.cloud' |
| 129 | + ] |
| 130 | + ``` |
| 131 | + |
| 132 | +3. Build, tag, and push the image to Scaleway Container Registry: |
| 133 | + ```bash |
| 134 | + docker build \ |
| 135 | + --platform linux/amd64 \ |
| 136 | + --push \ |
| 137 | + -t <CONTAINER_REGISTRY_ENDPOINT>/my-django-app:latest . |
| 138 | + ``` |
| 139 | + <Message type="note"> |
| 140 | + 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). |
| 141 | + </Message> |
| 142 | + |
| 143 | +### Deploy your app using Serverless Containers |
| 144 | + |
| 145 | +1. [Deploy a Serverless Container](/serverless-containers/how-to/deploy-container/) with the following settings: |
| 146 | + - **Registry**: Scaleway Container Registry |
| 147 | + - **Registry namespace**: The namespace where you pushed your image |
| 148 | + - **Container port**: `8000` as it is the [port](/serverless-containers/reference-content/port-parameter-variable/) exposed in the dockerfile. |
| 149 | + - **Resources**: `500 mVCPU` and `512 MB` memory |
| 150 | + - **Autoscaling**: Set minimum scale to `1` to avoid [cold starts](/serverless-containers/concepts/#cold-start) (optional) |
| 151 | + |
| 152 | + Deployment may take up to a minute. |
| 153 | + |
| 154 | +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. |
| 155 | + |
| 156 | +## Going further |
| 157 | + |
| 158 | +- You can deploy an existing Django project by building and pushing its container image as shown above. |
| 159 | +- [Add a custom domain](/serverless-containers/how-to/add-a-custom-domain-to-a-container/) to your deployed app. |
0 commit comments