Skip to content

Instantly share code, notes, and snippets.

@emmettna
Last active June 27, 2021 20:50
Show Gist options
  • Save emmettna/b78f54a6683b06a2a2da21db7580a8d6 to your computer and use it in GitHub Desktop.
Save emmettna/b78f54a6683b06a2a2da21db7580a8d6 to your computer and use it in GitHub Desktop.
Docker-compose Nginx + Django + Gunicorn
1. Make folder tree like these
mi_project
├── src
│ ├── midjangoapp
│ ├── manage.py
├── config
│ ├── requirements.pip
│ ├── nginx
│ ├── midjangoapp.conf
├── Dockerfile
└── docker-compose.yml
2. write requiretments.pip file by version you need or omit for latest
Django == 1.11.2
gunicorn == 19.7.1
pymysql == 0.7.11
mysqlclient
pyfcm
3.Dockerfile.
FROM python:3.5 # imagename : version
ENV PYTHONUNBUFFERED 1
RUN mkdir /config
ADD /config/requirements.pip /config
RUN pip install -r /config/requirments.pip
RUN mkdir /src;
WORKDIR /src
4. docker-compose.yml
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx1
ports:
- "80:8000" # I like working on 80 since you don't need to specify and looking neat on URL
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
web:
build: .
container_name: django1
command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn mydjango.wsgi -b 0.0.0.0:8000 --workers 3"
volumes:
- ./src:/src
expose:
- "8000"
5. config file for nginx
upstream web {
ip_hash;
server web:8000
}
server {
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
Notice,
if you don't add 'web' to django allowed host, django won't start up
#Nginx
docker exec -ti nginx bash
#Web
docker exec -ti web bash
#Database
docker exec -ti db bash
#Nginx
docker-compose logs nginx
#Web
docker-compose logs web
#DB
docker-compose logs db
# Rebuild and run detached
docker-compose up --force-recreate -d
# Pull from repository and recreate
docker-compose rm -f
docker-compose pull
docker-compose up --build -d
@abbad
Copy link

abbad commented Aug 6, 2018

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

add the semicolon at server web:8000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment