I've read some posts about this issue on StackOverflow already but couldn't fix my issue with them, as the title says I'm trying to serve static using django, nginx on docker.
At first I was getting an error saying PermissionError: [Errno 13] Permission denied: '/static/admin'. I've figured out that I had to change STATIC_ROOT
from STATIC_ROOT = '/static'
to STATIC_ROOT = os.path.join(BASE_DIR, 'static')
. Now collectstatic
works on the Docker's Terminal but my webapp's static is still not working, I would like to understand why and fix it.
I've put the project on Github if you need any more infos.
project.conf
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
alias /static/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
docker-compose.yml
version: '2'
services:
nginx:
image: nginx:latest
container_name: nz01
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static
depends_on:
- web
web:
build: .
container_name: dz01
command: bash -c 'python manage.py collectstatic --noinput && python manage.py makemigrations && python manage.py migrate && gunicorn oqtor.wsgi -b 0.0.0.0:8000'
depends_on:
- db
volumes:
- ./src:/src
- /static:/static
expose:
- "8000"
links:
- redis
...
...
Dockerfile :
FROM python:latest
ENV PYTHONUNBUFFERED 1
#ENV C_FORCE_ROOT true
ENV APP_USER myapp
ENV APP_ROOT /src
RUN mkdir /src;
RUN groupadd -r ${APP_USER} \
&& useradd -r -m \
--home-dir ${APP_ROOT} \
-s /usr/sbin/nologin \
-g ${APP_USER} ${APP_USER}
WORKDIR ${APP_ROOT}
RUN mkdir /config
ADD config/requirements.pip /config/
RUN pip install -r /config/requirements.pip
USER ${APP_USER}
ADD . ${APP_ROOT}