Ich stehe vor einem Problem, das bereits einen großen Teil meiner Zeit in Anspruch genommen hat. Ich versuche, eine Anwendung mit Docker und Django bereitzustellen, kann die statischen Dateien jedoch nicht lesen, da die Meldung angezeigt wird, dass sie nicht gefunden werden können. Ich bin in den Container gelangt und die Dateien sind dort, wo sie sein sollten.
Ich werde unten etwas Code hinzufügen:
Docker-Datei
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /www
COPY Pipfile Pipfile.lock /www/
WORKDIR /www
RUN pip install pipenv && pipenv install --system
COPY . /www/
RUN pip install -r requirements.txt
RUN cd myapp-root && python manage.py collectstatic --no-input
EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "myapp-root", "myapp.wsgi:application"]
Einstellungen.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
docker-compose.yml
version: '3'
services:
# database containers, one for each db
postgres:
restart: always
image: postgres:latest
ports:
- "5432:5432"
volumes:
- db_volume:/var/lib/postgresql/data/
# web container, with django + gunicorn
djangoapp:
build: .
environment:
- DJANGO_SETTINGS_MODULE
volumes:
- .:/www
links:
- postgres:postgres
- redis:redis
env_file: .env
# reverse proxy container (nginx)
nginx:
image: nginx:1.13
ports:
- 80:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- ./myapp-root/static:/static
- ./myapp-root/media:/media
depends_on:
- djangoapp
redis:
restart: always
image: redis:latest
ports:
- "6379:6379"
volumes:
- redisdata:/data
volumes:
db_volume:
redisdata:
local.conf (nginx)
server {
listen 80;
server_name localhost;
location /static/ {
alias /www/myapp-root/static/;
}
location /media/ {
alias /www/myapp-root/media/;
}
location / {
proxy_pass http://djangoapp;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://djangoapp;
break;
}
}
}
Ich habe die Lokalisierung der Dateien viele Male geändert, werde sie aber immer noch nicht im Docker-Container finden.
nginx_1 | 2019/06/29 00:11:28 [error] 6#6: *3 open() "/www/myapp-root/static/css/style.css" failed (2: No such file or directory),
Danke schön,