I have set up my nginx folder, Dockerfile, .env, docker-compose.yml and entrypoint.sh files, and everything is running okay and I’m able to see pages as it should be. But the only problem is I can’t load staticfiles. The gunicorn
container logs are showing
"Not Found" and the nginx container is showing failed (2: No such file
or directory).
These are my configuration files:
docker-compose.yml
version: '3'
services:
marathon_gunicorn:
volumes:
- static:/static
env_file:
- .env
build:
context: .
ports:
- "8010:8000"
nginx:
build: ./nginx
volumes:
- static:/static
ports:
- "8012:80"
depends_on:
- marathon_gunicorn
db:
image: postgres:15
container_name: postgres_db
restart: always
environment:
POSTGRES_DB: xxx
POSTGRES_USER: xxx
POSTGRES_PASSWORD: xxx
volumes:
- pg_data:/var/lib/postgresql/data
volumes:
static:
pg_data:`
entrypoint.sh
#!/bin/sh
# Apply database migrations
python manage.py migrate --no-input
# Collect static files
python manage.py collectstatic --no-input
# Start Gunicorn server
gunicorn absamarathon.wsgi:application --bind 0.0.0.0:8000
Django Dockerfile
FROM python:3.8
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./absamarathon /app
WORKDIR /app
COPY ./entrypoint.sh /
ENTRYPOINT [ "sh", "/entrypoint.sh" ]
nginx/default.conf
upstream django {
server marathon_gunicorn:8000;
}
server {
listen 80;
location / {
proxy_pass http://django;
}
location /static/ {
alias /static/;
}
# location /media/ {
# alias /app/media/;
# }
}
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
2
Answers
Your ngix configuration isn’t right as it doesn’t handle static right, because you didn’t tell nginx where is the folder to server /static. You shall put
/static/
above/
so it matches first.}
Create and attach volume to your container,
docker-compose.yml