4

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!

2
  • You don't seem to have done anything to connect the nginx container with the Django one. Commented Jun 15, 2017 at 12:51
  • @yangxg I've updated my answer Commented Jun 15, 2017 at 15:36

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank a lot! It works. But the next problem is I can not serve static files using nginx. Please see the detail in my question.
Let me check it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.