Skip to content

Instantly share code, notes, and snippets.

@izznogooood
Last active November 7, 2019 16:35
Show Gist options
  • Save izznogooood/a97e68178ac3b46b73fedfb1e9a906cf to your computer and use it in GitHub Desktop.
Save izznogooood/a97e68178ac3b46b73fedfb1e9a906cf to your computer and use it in GitHub Desktop.
Django Deployment

Production

Relative path Django project dir.

Dockerfile (Django app)

FROM python:3.7.4-alpine3.9

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

RUN apk update && apk upgrade
RUN apk add --no-cache linux-headers bash gcc \
    musl-dev libjpeg-turbo-dev libpng libpq \
    postgresql-dev git zlib-dev libmagic \
    python-dev libffi-dev openssl-dev py2-pip


COPY ./requirements.txt /app

RUN pip install -U pip
RUN pip install -U -r /app/requirements.txt

COPY ./ /app

nginx/Dockerfile (Nginx)

FROM nginx:1.17.1-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d

nginx/default.conf

server {
    listen 5000;
    
    client_max_body_size 10M;

    location / {
        proxy_pass http://app:5000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /app/staticfiles/;
    }

    location /media/ {
        alias /srv/media/;
    }

}

docker-compose.stage.yml

version: '3'

services:
  app:
    restart: always
    build: ./
    command: ./docker-entrypoint.stage.sh
    env_file:
      - env-stage.env
    volumes:
      - static_volume:/app/staticfiles
      - /srv/media:/app/media:rw
    expose:
      - 5000
  nginx:
    restart: always
    build: ./nginx
    volumes:
      - static_volume:/app/staticfiles
      - /srv/media:/srv/media:ro
    ports:
      - 5000:5000
    depends_on:
      - app

volumes:
  static_volume:

docker-entrypoint.stage.sh

#!/usr/bin/env bash

# clean static
rm -fr staticfiles

python3 manage.py collectstatic --noinput
python3 manage.py migrate --noinput

# Probably gunicorn for you
daphne -b 0.0.0.0 -p 5000 secpim.asgi:application # Python asgi/wsgi path

env-stage.env

Depends on what your django app needs...

DJANGO_SETTINGS_MODULE=django_project_name.settings.stage <-- python path
SECRET_KEY=
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=blah
SQL_USER=blah
SQL_PASSWORD=blah.
SQL_HOST=
SQL_PORT=

Development

docker-compoose

version: '3'

services:
  db:
    restart: always
    image: postgres:11.3-alpine
    environment:
      POSTGRES_DB: blah
      POSTGRES_USER: blah
      POSTGRES_PASSWORD: blah

    volumes:
      - postgres:/var/lib/postgresql/data

  app:
    restart: always
    build: ./
    command: ./docker-entrypoint.dev.sh
    volumes:
      - ./:/app:rw
    depends_on:
      - db
    env_file:
      - env-dev.env
    ports:
      - "5000:5000"

volumes:
  postgres:

docker-entrypoint.dev.sh

#!/usr/bin/env bash

echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
  sleep 0.1
done
echo "PostgreSQL started"

python3 manage.py makemigrations
python3 manage.py migrate

python3 manage.py runserver 0.0.0.0:5000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment