Deploy Django in 3 minutes using docker-compose

I have created a docker-compose that can be used even for projects that have already been created, so I will summarize the specifications in the article.

** How to Use is written on GitHub, so please → GitHub **

(If you change the DB from the default sqlite, you will need another setting ... sorry)

The deployment environment is Django + Nginx + Gunicorn.

Overview

Please refer to the README on GitHub, which has a link above, for how to use it.

To make it easy to deploy, I created a docker-compose for Django deployment that works with the following directory structure.

django-nginx-gunicorn-docker/
      ├ nginx/
      │ └ project.conf
      ├ django/
      │  ├ Dockerfile
      │  ├ requirements.txt
      │  └ [DJANGOPROJECT]
      │     ├ manage.py
      │     ├ …
      │     └ [PROJECTNAME]
      └ docker-compose.yml

Mount the entire project in a container and run it. Now let's look at the configuration file.

docker-compose The containers used are those for Django applications (Gunicorn also works in this) and Nginx containers that act as reverse proxies.

docker-compose.yml


version: '3'
services:
    django:
        build: ./django
        expose:
            - "8000"
        networks:
            - nginx_network
        volumes:
            - ./django:/code
        hostname: django-server
        restart: always
    nginx:
        image: nginx
        ports:
            - "80:80"
        networks:
            - nginx_network
        depends_on:
            - django
        volumes:
            - ./nginx/project.conf:/etc/nginx/conf.d/default.conf
        restart: always
networks:
    nginx_network:
        driver: bridge

I'm mounting the project environment under volumes in django. Even if you edit after starting the container, it will be reflected by restarting the container.

Also, since the nginx container is port forwarding at 80:80, when access comes to port 80 of the local host, it will be handed over to the nginx container.

I haven't done anything particularly tricky, so the explanation of compose is over.

Dockerfile A description of the Dockerfile in the django directory.

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
WORKDIR /code/MYPROJECT
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "MYPROJECT.wsgi:application"]

Use the image of python3 series. I'm downloading Django and Gunicorn in RUN on line 6. (I also downloaded the library for Postgres, but I'm not using it. I'm sorry.)

requirements.txt


Django==2.2.7
gunicorn==19.9.0
psycopg2

By the way, it's a version of Django, but when I initially specified 2.0 and gave it to Github, I got angry. (A lot of security warnings came. I also got an angry pull request from the bot on GitHub.)

project.conf This is the nginx configuration file.

project.conf


upstream django {
  server django:8000;
}

server {
    listen  80;
    server_name :localhost;

    location / {
        proxy_pass http://django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Specify the destination of the request upstream. The destination is the container's localhost. Also, the one named here is used in the proxy path setting.

On the server side, the listening port and proxy are set.

at the end

I got stuck in the port settings. .. .. Everyone should be careful ;;

Recommended Posts

Deploy Django in 3 minutes using docker-compose
CSS environment created in 10 minutes using Django
How to deploy a Django app on heroku in just 5 minutes
Information recording memo using session in Django
Django Foreign Key Tutorial Ends in 10 Minutes
Django Heroku Deploy 1
Django Heroku Deploy 2
Forms in Django
Build a Django environment with Vagrant in 5 minutes
Easy 3 minutes TensorBoard in Google Colab (using TensorFlow 2.x)
Learn Pandas in 10 minutes
Model changes in Django
Understand in 10 minutes Selenium
CSS environment created in 10 minutes using Django
Try using Django templates.html
Selenium running in 15 minutes
[Python] Create an infrastructure diagram in 3 minutes using diagrams
Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
Getting started and using sample class-based generic views in Django
How to generate a query using the IN operator in Django
Create an easy-to-use follow model in Django using ManyToManyField through
Performance optimization in Django 3.xx
PHP var_dump in Django templates
Handle constants in Django templates
Implement follow functionality in Django
Rename table columns in Django3
Deploy Django serverless with Lambda
Using verticalenv in shell scripts
[Django] Notes on using django-debug-toolbar
Output table structure in Django
DEBUG settings when using Django
(Note) Django in Vagrant environment
Deploy django project to heroku
Show Django ManyToManyField in Template
Translate using googletrans in Python
Using Python mode in Processing
Start in 5 minutes GIMP Python-Fu
[Tutorial] Make a named entity extractor in 30 minutes using machine learning
Implementation of JWT authentication functionality in Django REST Framework using djoser
Publish your web app in 4 minutes 33 seconds using Heroku x bottle
Create and deploy a Django (PTVS) app using Azure Table storage
Build and try an OpenCV & Python environment in minutes using Docker