1

Trying to deploy website with nginx + gunicorn + docker + django. But ngingx isn't serving static files. Following are the configurations:

Django project structure

enter image description here

settings file production.py

STATIC_URL = "/static/"

"""STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)"""


STATIC_ROOT = "/app/forex/static/admin/"

Docker file for nginx

FROM nginx:1.19.0

COPY ./default.conf /etc/nginx/conf.d/default.conf

nginx configurations

upstream django {
    server website:8000;
}

server {
    listen 80;
    
    client_max_body_size 100M;
    proxy_set_header X-Forwarded-Proto $scheme;

    location / {
        proxy_pass http://django;
    }
    
    location /media/ {
        alias /app/media/;
    }

    location /static/ {
        alias /app/forex/static/admin/;
    }
}

Gunicorn docker file

FROM python:3


ADD requirements.txt /app/requirements.txt

ADD . /app
WORKDIR /app
EXPOSE 8000:8000

RUN pip install --upgrade pip && pip install -r /app/requirements.txt
RUN python manage.py collectstatic --no-input --settings=forex.settings.production

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "forex.wsgi:application", "DJANGO_SETTINGS_MODULE=forex.settings.production"]

docker-compose.yml

services:
    website:
        build:
          context: .
          dockerfile: Dockerfile.app
        env_file:
          - env
        container_name: website_container_8
    nginx:
        build: ./nginx
        volumes:
            - static:/app/forex/static/admin/
        ports:
            - "80:80"
        depends_on:
            - website
volumes:
  static:

FROM nginx container, it isn't copying static files. enter image description here

What do I need to change to make it working?

13
  • You are mounting a static named volume, which is empty, where is your static folder on your host? Commented Oct 5, 2022 at 22:36
  • 1
    In your dockerfile you are not copying anything from your local static folder to the image /app/forex/static/admin/ this path. At least RUN python manage.py collectstatic --no-input --settings=forex.settings.production this line does nothing Commented Oct 6, 2022 at 16:14
  • 1
    In your docker compose put volumes: - static:/app/forex/static/admin/ under your app container too Commented Oct 6, 2022 at 17:07
  • 1
    check my answer, it is too long, to post it here :D Commented Oct 6, 2022 at 17:12
  • 1
    You are generating the static contet that nginx has to serve to you with your website (Gunicorn docker file) container, at RUN python manage.py collectstatic --no-input --settings=forex.settings.production this line, you can move it to nginx (by build a multi stage dockerfile) that way you don't have to share folders between conainers Commented Oct 6, 2022 at 17:22

1 Answer 1

1

Your files are located at your website container, you need to share it, with the nginx container:

services:
    website:
        build:
          context: .
          dockerfile: Dockerfile.app
        env_file:
          - env
        container_name: website_container_8
        volumes:
            - static:/app/forex/static/admin/ #<-- you want to share this
    nginx:
        build: ./nginx
        volumes:
            - static:/app/forex/static/admin/ #<-- with this folder
        ports:
            - "80:80"
        depends_on:
            - website
volumes:
  static: #<-- you can do it through this
Sign up to request clarification or add additional context in comments.

Comments

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.