skip to Main Content

I am having issue with configuring a nginx server using docker and django. I think the issues lies in volume path.
Here is my directory structure

-nginx
--default.conf
--Dockerfile
-portfolio_app (django webpapp)
--main_app
---settings.py
---wsgi.py
--sub_app
---views.py
---static
---media
-docker-compose.yml
-Dockerfile (django related)
-entrypoint.sh (to start django server)

Regarding django it working, but i am unable to serve the static files. I think i am not giving the path correctly.

Here is Dockerfile related to django

FROM python:3.8.13-slim-buster

WORKDIR /app
RUN pip install --upgrade pip
COPY ./requirements.txt ./
RUN pip install -r requirements.txt

COPY ./portfolio_app ./

COPY ./entrypoint.sh ./
ENTRYPOINT ["sh","/app/entrypoint.sh"]

nginx files
default.conf

upstream django {
    server portfolio_application:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://django;
    }

    location /static/ {
        alias /static_volume/;
    }
}

Dockerfile

FROM nginx:1.19.0-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf

Here is docker-compose.yml file

version: '3.3'
services:
  portfolio_application:
    build: 
      context: .
    container_name: portfolio_app
    volumes:
      - static_volume:/app/sub_app/static
      - media_volume:/app/sub_app/media
    ports:
      - "8000:8000"
    env_file:
      - .env

  nginx:
    build: ./nginx
    volumes:
      - static_volume:/app/sub_app/static
      - media_volume:/app/sub_app/media
    ports:
      - "80:80"
    depends_on:
      - portfolio_application


volumes:
  media_volume:
  static_volume:

I am not sure about the path of volumes in yml file and settings of default.conf.
Here are the logs

Attaching to portfolio_app, docker_nginx_1
portfolio_app            | [2022-07-21 09:22:10 +0000] [7] [INFO] Starting gunicorn 20.1.0
nginx_1                  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1                  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
portfolio_app            | [2022-07-21 09:22:10 +0000] [7] [INFO] Listening at: http://0.0.0.0:8000 (7)
portfolio_app            | [2022-07-21 09:22:10 +0000] [7] [INFO] Using worker: sync
nginx_1                  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
portfolio_app            | [2022-07-21 09:22:10 +0000] [9] [INFO] Booting worker with pid: 9
nginx_1                  | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_1                  | 10-listen-on-ipv6-by-default.sh: /etc/nginx/conf.d/default.conf differs from the packages version, exiting
nginx_1                  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1                  | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1                  | 172.19.0.1 - - [21/Jul/2022:09:22:13 +0000] "GET / HTTP/1.1" 200 48080 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" "-"
nginx_1                  | 2022/07/21 09:22:13 [error] 29#29: *1 open() "/static_volume/lib/animate/animate.min.css" failed (2: No such file or directory), client: 172.19.0.1, server: , request: "GET /static/lib/animate/animate.min.css HTTP/1.1", host: "0.0.0.0", referrer: "http://0.0.0.0/"
nginx_1                  | 172.19.0.1 - - [21/Jul/2022:09:22:13 +0000] "GET /static/lib/animate/animate.min.css HTTP/1.1" 404 153 "http://0.0.0.0/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" "-"

Edit
This is how i added static url and file in settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'sub_app','media') 
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'sub_app','static')

2

Answers


  1. default.conf:

    instead of this:

    location /static/ {
            alias sub_app/static/;
        }
    

    try this:

    location /static/ {
            alias ./static/;
        }
    
    Login or Signup to reply.
  2. In your nginx service you have mounted the volume as below. Where static_volume is the host volume and /app/sub_app/static is the container directory where it is mounted to.

    static_volume:/app/sub_app/static
    

    But in your nginx config file you are routing the static requests to /static_volume/. Instead you need to point it to your container directory as shown below

    location /static/ {
            alias /app/sub_app/static/;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search