How to Deploy Django Using Docker: Step-by-Step Guide
To deploy a Django app using
Docker, create a Dockerfile that sets up Python, installs dependencies, and runs the app. Use docker-compose.yml to define services like the Django app and database, then run docker-compose up to start everything together.Syntax
A typical Docker deployment for Django involves two main files:
- Dockerfile: Defines the environment, installs Python, copies your code, installs dependencies, and runs the server.
- docker-compose.yml: Defines services like the Django app and database, networking, and volumes.
These files work together to build and run your Django app inside containers.
dockerfile
FROM python:3.11-slim WORKDIR /app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Example
This example shows a simple Docker setup for Django with a PostgreSQL database using docker-compose. It demonstrates building the Django image, installing dependencies, and running the app with the database service.
yaml
version: '3.9' services: db: image: postgres:15 environment: POSTGRES_DB: mydb POSTGRES_USER: user POSTGRES_PASSWORD: password volumes: - postgres_data:/var/lib/postgresql/data web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/app ports: - "8000:8000" depends_on: - db volumes: postgres_data:
Output
Starting db ... done
Starting web ... done
web_1 | Watching for file changes with StatReloader
web_1 | Performing system checks...
web_1 | System check identified no issues (0 silenced).
web_1 | April 27, 2024 - 12:00:00
web_1 | Django version 4.x, using settings 'myproject.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
Common Pitfalls
Common mistakes when deploying Django with Docker include:
- Not exposing the correct port in
Dockerfileordocker-compose.yml, so the app is unreachable. - Forgetting to add
ALLOWED_HOSTSin Django settings to allow connections. - Not using volumes properly, causing code changes not to reflect inside the container.
- Missing database environment variables or not waiting for the database to be ready before Django starts.
dockerfile
## Wrong: Missing port exposure and ALLOWED_HOSTS CMD ["python", "manage.py", "runserver"] ## Right: Expose port and set ALLOWED_HOSTS in settings.py CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] # settings.py ALLOWED_HOSTS = ['*']
Quick Reference
Tips for smooth Django Docker deployment:
- Use
python:3.11-slimbase image for smaller size. - Keep dependencies in
requirements.txtand install in Dockerfile. - Use
docker-composeto manage multi-container apps (web + db). - Map ports and volumes for development convenience.
- Set
ALLOWED_HOSTSproperly in Django settings.
Key Takeaways
Create a Dockerfile to set up Python, install dependencies, and run Django on 0.0.0.0:8000.
Use docker-compose.yml to define Django and database services for easy multi-container deployment.
Expose ports and set ALLOWED_HOSTS in Django settings to allow external access.
Use volumes in docker-compose for live code updates during development.
Ensure the database service is ready before Django starts to avoid connection errors.