我试图将使用django-rest-framework制作的API放入docker容器中。
除了无法访问我的静态文件之外,其他所有内容似乎都可以正常工作。
这是我的settings.py:
MEDIA_URL = '/uploads/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
我的urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
path('api/bookmakers/', include('bookmakers.urls')),
path('api/pronostics/', include('pronostics.urls')),
path('api/', include('authentication.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
我的Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
&& localedef -i fr_FR -c -f UTF-8 -A /usr/share/locale/locale.alias fr_FR.UTF-8
ENV LANG fr_FR.UTF-8
ENV LANGUAGE fr_FR
ENV LC_ALL fr_FR.UTF-8
RUN python --version
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . /code/
COPY docker-entrypoint.sh /code/
ENTRYPOINT ["/code/docker-entrypoint.sh"]
最后是我的docker-compose.yml
version: '3.1'
services:
db:
image: postgres
container_name: nyl2pronos-db
restart: always
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: nyl2pronos
website:
container_name: nyl2pronos-website
image: nyl2pronos-website
build:
context: nyl2pronos_webapp
dockerfile: Dockerfile
ports:
- 3000:80
api:
container_name: nyl2pronos-api
build:
context: nyl2pronos_api
dockerfile: Dockerfile
image: nyl2pronos-api
restart: always
ports:
- 8000:8000
depends_on:
- db
environment:
- DJANGO_PRODUCTION=1
因此,一旦我转到url:http://localhost:8000/admin/,我就可以登录,但是没有CSS,因为不会加载静态文件。
GET http://localhost:8000/static/admin/css/forms.css net::ERR_ABORTED 40
如果您发现其他错误,请不要犹豫,请告诉我。
先感谢您!
最佳答案
如果您使用Gunicorn或任何生产级django服务器,则它们通常不提供静态内容。有其他服务方式。其中之一是使用whitenoise。您可以这样尝试:
pip install whitenoise
并添加一个新的中间件:
MIDDLEWARE = [
# 'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
另一种方法是使用NGINX。我建议你可以这样做
# inside config/nginx/conf.conf in source code
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
alias /src/static/;
}
location /media/ {
autoindex on;
alias /src/media/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
现在更新您的docker compose以添加NGINX和上面给出的配置:
db:
image: postgres
container_name: nyl2pronos-db
restart: always
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: nyl2pronos
website:
container_name: nyl2pronos-website
image: nyl2pronos-website
build:
context: nyl2pronos_webapp
dockerfile: Dockerfile
ports:
- 3000:80
api:
container_name: nyl2pronos-api
build:
context: nyl2pronos_api
dockerfile: Dockerfile
image: nyl2pronos-api
restart: always
ports:
- 8000:8000
depends_on:
- db
environment:
- DJANGO_PRODUCTION=1
nginx:
image: nginx:latest
container_name: nz01
ports:
- "8000:8000"
volumes:
- ./src:/src # for syncing with django source code
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- nyl2pronos-api
有关更多详细信息,您可以在这里检查我的存储库:https://github.com/ruddra/docker-django或检查this post有关如何配置NGINX的信息。
关于python - Django容器-提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54396137/