How to Dockerize django application for production deployment with Gunicorn and Nginx

How to Dockerize django application for production deployment with Gunicorn and Nginx

Dockerizing a Django application for production deployment involves several key steps: setting up your Django application to work with Gunicorn (a Python WSGI HTTP Server), configuring Nginx as a reverse proxy, and creating Dockerfiles and a Docker Compose file for containerization. Below is a general guide to dockerize a Django application with Gunicorn and Nginx for production.

Step 1: Prepare Django Application

  1. Requirements File: Ensure you have a requirements.txt file in your Django project directory listing all the Python dependencies, including Django and Gunicorn.

    Django>=3.0,<4.0 gunicorn # any other dependencies 
  2. Static and Media Files: In your Django settings, configure static and media files to be served. Gunicorn doesn't serve these files in production, so they will be served by Nginx.

    STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 

Step 2: Create a Dockerfile for Django and Gunicorn

Create a Dockerfile in your Django project directory.

# 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 /code # Install dependencies COPY requirements.txt /code/ RUN pip install --no-cache-dir -r requirements.txt # Copy project COPY . /code/ # Collect static files RUN python manage.py collectstatic --noinput # Command to run gunicorn CMD gunicorn your_project_name.wsgi:application --bind 0.0.0.0:8000 

Replace your_project_name with the name of your Django project.

Step 3: Create a Dockerfile for Nginx

  1. Nginx Configuration: Create an Nginx configuration file (nginx.conf) in your project.

    server { listen 80; server_name yourdomain.com; location / { proxy_pass http://web:8000; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_redirect off; } location /static/ { alias /code/static/; } location /media/ { alias /code/media/; } } 

    Replace yourdomain.com with your domain name.

  2. Dockerfile for Nginx: Create a Dockerfile for Nginx in your project.

    # Use the Nginx image from Docker Hub FROM nginx:1.19-alpine # Copy the Nginx configuration COPY nginx.conf /etc/nginx/conf.d/default.conf 

Step 4: Docker Compose

Create a docker-compose.yml file in your project root.

version: '3.8' services: web: build: . command: gunicorn your_project_name.wsgi:application --bind 0.0.0.0:8000 volumes: - .:/code ports: - 8000:8000 depends_on: - db db: image: postgres:12 volumes: - postgres_data:/var/lib/postgresql/data/ nginx: build: context: . dockerfile: Dockerfile-nginx volumes: - static_volume:/code/static - media_volume:/code/media ports: - "80:80" depends_on: - web volumes: postgres_data: static_volume: media_volume: 

Replace Dockerfile-nginx with the path to your Nginx Dockerfile if it's different.

Step 5: Build and Run with Docker Compose

Run the following commands to build and start your containers:

docker-compose build docker-compose up -d 

Final Notes

  • This setup assumes that you are using PostgreSQL as your database. Change the db service in the docker-compose.yml file accordingly if you use a different database.
  • Make sure to update your Django settings to use environment variables for sensitive data (like database credentials, secret key, etc.) and not to hardcode them in your settings file.
  • Always test your Docker setup in a staging environment before deploying to production.

More Tags

replaykit patindex userform simplebar progress-db mvn-repo testcafe preview blockchain formatting

More Programming Guides

Other Guides

More Programming Examples