Deploy Django, Gunicorn, NGINX, Postgresql using Docker

This post mainly based on this blog: https://docs.docker.com/compose/django/.

I will be extending this post by serving django+gunicorn using Nginx, also I will using Postgresql docker container to use it as database.

Lets not waste time and go to the following steps.

1. Let's make an empty directory named myproject and add another folder inside name it src. src should contain the django project. For testing purpose lets put a simple django project inside named mydjango.

2. Let's create a subdirectory inside myproject and name it config. Lets put a requirement.pip file inside config and write these line in it:

Django==1.10  
gunicorn==19.6.0  
psycopg2==2.6.2  

3. Now let's make a Dockerfile inside the myproject. This should contain the following lines:

FROM python:3.5  
ENV PYTHONUNBUFFERED 1  
RUN mkdir /config  
ADD /config/requirements.pip /config/  
RUN pip install -r /config/requirements.pip  
RUN mkdir /src;  
WORKDIR /src  

So this Dockerfile starts with a Python 3.5 based image. Then the container is modified by adding the requirement.pip file in /config directory within the container and installing the packages from it.

4. Let's create a file called docker-compose.yml in myproject directory.

The docker-compose.yml file describes the services that make your app. Here we need a web service(Django+Gunicorn), A database(Postgres), and Proxy Server(Nginx). It also describes which Docker images these services will use, how they will link together, any volumes they might need mounted inside the containers. Finally, the docker-compose.yml file describes which ports these services expose. See the docker-compose.yml reference for more information on how this file works. Don't forget to add docker-compose
to your python environment by running pip install docker-compose.

5. Let's add the following configuration to the docker-compose.yml file:

version: '2'  
services:  
  nginx:
    image: nginx:latest
    container_name: ng01
    ports:
      - "8000:8000"
    volumes:
      - ./src:/src
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build: .
    container_name: dg01
    command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn mydjango.wsgi -b 0.0.0.0:8000"
    depends_on:
      - db
    volumes:
      - ./src:/src
    expose:
      - "8000"

  db:
    image: postgres:latest
    container_name: ps01

It says that there are three services for this project: nginx, web, db. nginx depends on web, web depends on db. db container uses postgres's latest image from dockerhub. Default username for db is postgres and password is postgres
web container is build using project's Dockerfile. It mounts src directory into it and exposes port 8000. version is being used for which format to use to compose the docker file.

nginx uses nginx's latest image from dockerhub. This proxy server is accessible from port 8000. It mounts src and config directory.

6. Now let's write a nginx configuration config file named mydjango.conf inside myproject's config folder and put it in a subdirectory named nginx.

upstream web {  
  ip_hash;
  server web:8000;
}

# portal
server {  
  location / {
        proxy_pass http://web/;
    }
  listen 8000;
  server_name localhost;
}

So what it does that, nginx acts as a reverse proxy for any connections going to django server and all connections goes through nginx to reach django server.

Project Directory should look like this:

── myproject
    ├── src
    │   ├── mydjango
    │   ├── manage.py
    ├── config
    │   ├── requirements.pip
    │   ├── nginx
    │      ├── mydjango.conf
    ├── Dockerfile
    └── docker-compose.yml

7. To communicate from django to postgres, we need to put database configuration in django applications settings file. It should look like this:

DATABASES = {  
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
} 

8. All is done. Now lets run docker-compose build in terminal within the project directory. It will build/rebuild(if necessary) all the containers. For first time running the containers, run docker-compose up -d. Lets go to browser and type: localhost:8000. We should see the django application up and running.

9. For stopping the docker, run docker-compose stop. Re-running docker, use docker-compose start.

10. For shell accessing.

#Nginx
docker exec -ti nginx bash

#Web
docker exec -ti web bash

#Database
docker exec -ti db bash  

For logs:

#Nginx
docker-compose logs nginx  
#Web
docker-compose logs web  
#DB
docker-compose logs db  

Thats it. You can see an working example here in my repo: https://github.com/ruddra/docker-django

Also another deployment example for Ruby on rails here: https://github.com/ruddra/deploy-notebook
(Thanks to Akimul Islam for the source)

Cheers!!

Update:

Serving django with gunicorn won't allow you to serve static files with it. You need to serve static files seperately. You can follow this post: http://ruddra.com/2016/11/02/serve-static-files-by-nginx-from-django-using-docker/ for how to do serve static files using Nginx from docker.

comments powered by Disqus