NGINX is not serving static files correctly
I'm having some trouble while trying to use nginx to serve my static files. What I'm trying to do is using nginx + gunicorn to deploy my django app, and I'm using docker compose to try to ease all needed conf.
Here are my files:
docker-compose.yml
django_gunicorn:
build:
context: .
volumes:
- static:/app/static
- media:/app/media
env_file:
- .env
expose:
- 8000
depends_on:
db:
condition: service_healthy
networks:
- app_network
command: >
gunicorn core.wsgi:application --bind 0.0.0.0:8000 --workers 3 --threads 2
# Serviço Nginx
nginx:
build:
context: ./nginx
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- static:/app/static
django - Dockerfile
FROM python:3.12-slim
WORKDIR /app
# Instala dependências do sistema
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
default-libmysqlclient-dev \
&& apt-get clean
# Instala dependências Python
COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# Copia os arquivos do projeto
COPY . .
# Permissões para o entrypoint
RUN chmod +x entrypoint.sh
EXPOSE 8000
ENTRYPOINT ["sh", "./entrypoint.sh"]
entrypoint.sh
#!/bin/sh
until python -c "import MySQLdb; MySQLdb.connect(host='${DATABASE_HOST}', port=int('${DATABASE_PORT}'), user='${MYSQL_USER}', passwd='${MYSQL_PASSWORD}', db='${MYSQL_DATABASE}')"; do
sleep 1
done
echo "Banco de dados disponível!"
python manage.py migrate --noinput
python manage.py collectstatic --noinput
exec gunicorn --workers 4 --bind 0.0.0.0:8000 core.wsgi:application
/nginx/default.conf
location /static/ {
alias /app/static/;
}
/nginx/Dockerfile
# Usar a imagem oficial do Nginx
FROM nginx:latest
# Remover configuração padrão do Nginx
RUN rm /etc/nginx/conf.d/default.conf
# Copiar a configuração personalizada do Nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
# (Opcional) Copiar certificados SSL
# COPY ./nginx/ssl /etc/nginx/ssl
# Expor a porta 80
EXPOSE 80
# Iniciar o Nginx
CMD ["nginx", "-g", "daemon off;"]
I've been digging up a bit on my containers and I realized that both django_gunicorn and nginx containers don't have anything on their /app/static dir. And here are my settings.py regarding static files:
settings.py
STATIC_ROOT = '/app/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'core/static')]
Any clue? I guess it's something related to these settings + docker volumes, I've tried changing things a little bit but no success.