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.
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.
I got stuck in the port settings. .. .. Everyone should be careful ;;
Recommended Posts