Adding nginx to my dockerized Django application

Viewed 430

I have successfully added gunicorn to my Dockerized Django application. I'm having trouble adding nginx to the mix. The nginx server exits almost immediately. Any help would be appreciated.

Here's my docker-compose.yml

version: "3"

services:
  web:
      build: 
          context: .
          args:
            HTTP_PROXY: http://amec.zscaler.company.com:9480
            HTTPS_PROXY: http://amec.zscaler.company.com:9480
      image: django_web 
      container_name: django_web  
      expose: 
        - "8000"
  nginx:
      build: ./nginx
      ports:
        - 1337:80
      depends_on:
        - web

Here's my Dockerfile:

FROM python:3.6

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

ARG HTTP_PROXY
ARG HTTPS_PROXY

COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN useradd user
USER user

CMD ["gunicorn", "django_web.wsgi:application", "--bind", "0.0.0.0:8000"]

Here's my nginx/Dockerfile:

FROM python:3.6
COPY nginx.conf /etc/nginx/conf.d

Here's nginx.conf:

upstream django_web{
    server web:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://django_web/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

Command to build and run everything:

sudo docker-compose -f docker-compose.cambridge.yml up -d

next command:

sudo docker ps -a
    
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
16f4651047ec        django_nginx       "python3"                20 seconds ago      Exited (0) 18 seconds ago                 django_nginx_1
03a460153a9c        django_web         "gunicorn Django.…"      21 seconds ago      Up 19 seconds               8000/tcp            django_web

When I run sudo docker exec -ti 16f4651047ec ping web I get positive responses.

After a new build, and I run: sudo docker logs 16f4651047ec I get:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration 

/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ 

/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 

10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf 

10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf 

/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh

/docker-entrypoint.sh: Configuration complete; ready for start up

After trying to browse to http://my-ip:1337/ , and I run: sudo docker logs 16f4651047ec I get the same as above plus this one line:

161.88.207.220 - - [23/Jul/2020:13:29:11 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"
p

After trying to browse to http://my-ip:1337/get_my_item/ , and I run: sudo docker logs 16f4651047ec I get the same as above plus this:

161.88.207.220 - - [23/Jul/2020:13:29:11 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"
2020/07/23 13:33:29 [error] 29#29: *3 "/usr/share/nginx/html/get_my_item/index.html" is not found (2: No such file or directory), client: 161.88.207.220, server: localhost, request: "GET /get_my_item/ HTTP/1.1", host: "10.228.91.5:1337"
161.88.207.220 - - [23/Jul/2020:13:33:29 +0000] "GET /get_my_item/ HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"
2 Answers

You are building your nginx image FROM python:3.6

Here's a simple nginx:alpine Dockerfile example:

FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf

I had to change my nginx:alpine Dockerfile to this

FROM nginx:alpine 
RUN rm -f /etc/nginx/conf.d/default.conf COPY
nginx.conf /etc/nginx/conf.d/default.conf
Related