Skip to content

Django REST API Deployment

Overview

This guide provides detailed instructions on deploying the Django REST API using Docker in a Docker Swarm environment. The provided docker-compose.yml file includes services for PgBouncer, Memcached, Nginx, and the Django API itself.

Docker Compose File

version: "3.8"

services:
  pgbouncersomapi:
    image: edoburu/pgbouncer
    networks:
      - ovencrypt
    environment:
      DB_USER: database_user
      DB_PASSWORD: database_password
      DB_NAME: database_name
      DB_HOST: 10.10.10.10
      AUTH_TYPE: plain
      POOL_MODE: session
      MAX_CLIENT_CONN: 100
      DEFAULT_POOL_SIZE: 20

  memcached:
    image: memcached:latest
    ports:
      - "11211:11211"

  sommedia:
    image: nginx:1.15
    networks:
      - ovencrypt
    volumes:
      - media:/usr/share/nginx/media
      - static:/usr/share/nginx/static
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    deploy:
      replicas: 1
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.sommedia.rule=Host(`files.domain.com`)"
        - "traefik.http.services.sommedia.loadbalancer.server.port=80"
        - "traefik.docker.network=ovencrypt"

  somapi:
    image: michameiu/somapi:v1.0.6
    restart: always
    depends_on:
      - pgbouncersomapi
    networks:
      - ovencrypt
    volumes:
      - media:/media
      - static:/static
      - tmp:/tmp
    environment:
      - DB_NAME=database_name
      - DB_USER=database_user
      - SECRET_KEY=secret_key
      - DB_PASSWORD=database_password
      - DB_HOST=pgbouncersomapi
      - DB_PORT=5432 
      - STATIC_URL=https://files.domain.com/static/
      - MEDIA_URL=https://files.domain.com/media/
    deploy:
      replicas: 1
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.somapi.rule=Host(`api.domain.com`)"
        - "traefik.http.services.somapi.loadbalancer.server.port=8000"
        - "traefik.docker.network=ovencrypt"

  process_tasks:
    image: michameiu/somapi:v1.0.6
    restart: always
    depends_on:
      - pgbouncersomapi
      - somapi
    command: python manage.py process_tasks
    networks:
      - ovencrypt
    volumes:
      - media:/media
      - static:/static
      - tmp:/tmp
    environment:
      - DB_NAME=database_name
      - DB_USER=database_user
      - SECRET_KEY=secret_key
      - DB_PASSWORD=database_password
      - DB_HOST=pgbouncersomapi
      - DB_PORT=5432 
      - STATIC_URL=https://files.domain.com/static/
      - MEDIA_URL=https://files.domain.com/media/

networks:
  ovencrypt:
    external: true
    attachable: true

volumes:
  media:
  cache:
  mainapp:
  seluser:
  static:
  tmp:
  pycache:

Nginx Configuration

server {
  listen 80;
  location /static {
    alias /usr/share/nginx/static;
  }
  location /media {
    alias /usr/share/nginx/media;
  }
}

Instructions

  1. Prerequisites

    • Ensure Docker and Docker Compose are installed.
    • Configure Domains
    • Initialize Docker Swarm if not initialized: sh docker swarm init
    • Ensure the ovencrypt network is created and attachable: sh docker network create --driver=overlay --attachable ovencrypt
  2. Setup Environment Variables

    • Modify the environment variables in the docker-compose.yml file as necessary, especially sensitive information like DB_USER, DB_PASSWORD,DB_NAME,DB_PORT, and SECRET_KEY.
    • Ensure the Traefik labels and network configurations are correct.
  3. Deploy the Stack

    • Navigate to the directory containing the docker-compose.yml file.
    • Run the following command to deploy the stack: sh docker stack deploy -c docker-compose.yml <stack_name> Replace <stack_name> with a suitable name for your stack
  4. Verify Deployment

    • Check the status of the services using: sh docker stack services <stack_name>
    • Verify that all services are running and properly configured.
  5. Access the Services

    • The API should be accessible at http://api.domain.com.
    • Static and media files should be accessible at http://files.domain.com.
  6. Logs and Debugging

    • To view logs for a specific service, use: sh docker service logs <stack_name>_<service_name> Replace <stack_name> with the name of your stack and <service_name> with the name of the service you want to inspect (e.g., somapi).