4
votes

I use Docker, Gunicorn, Nginx in django development environment but can only see nginx welcome page.

my docker file:

django:

FROM python:3.5

ENV PYTHONUNBUFFERED 1

RUN groupadd -r django \
    && useradd -r -g django django

COPY ./requirements.txt /requirements.txt
RUN pip install --no-cache-dir -r /requirements.txt \
    && rm -rf /requirements.txt

COPY ./compose/django/gunicorn.sh /
RUN sed -i 's/\r//' /gunicorn.sh \
    && chmod +x /gunicorn.sh \
    && chown django /gunicorn.sh

COPY . /app

RUN chown -R django /app

RUN mkdir /static
RUN chown -R django /static

USER django

WORKDIR /app

nginx:

FROM nginx:latest
ADD nginx.conf /etc/nginx/sites-enabled/django_blog.conf

gunicorn.sh:

#!/bin/sh
python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn blogproject.wsgi -w 4 -b 127.0.0.1:8000 --chdir=/app

nginx.conf:

server {
    charset utf-8;
    listen 80 default_server;

    location /static {
        alias /app/static;
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:8000;
    }
}

docker-compose.yml:

version: '3'

services:
  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    command: /gunicorn.sh

  nginx:
    build: ./compose/nginx
    depends_on:
      - django

    ports:
      - "80:80"

commands I run:

docker-compose build

docker-compose up

It seems Nginx doesn't pass request to gunicorn?


update:

According to @Robert suggestion, things worked. However, since the static files are in django container, so I have no idea how to make nginx host them? I tried to set volumes according to Volume configuration reference like this:

version: '3'

services:
  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    volumes:
      - static-volume:/app/static
    command: /gunicorn.sh

  nginx:
    build: ./compose/nginx
    volumes:
      - static-volume:/app/static
    depends_on:
      - django

    ports:
      - "0.0.0.0:80:80"

volumes:
  static-volume:

but it seems does not work? How should I make nginx to access the static files in django container? Thanks!

1
You don't seem to have done anything to connect the nginx container with the Django one.Daniel Roseman
@yangxg I've updated my answerRobert

1 Answers

2
votes

In nginx.conf point properly to django:

proxy_pass http://django:8000;

Thanks to the docker network.

And, I recommend you to override the default nginx conf. So change this:

ADD nginx.conf /etc/nginx/sites-enabled/django_blog.conf

To this:

ADD nginx.conf /etc/nginx/conf.d/default.conf