我在django开发环境中使用Docker,Gunicorn,Nginx,但只能看到nginx欢迎页面。
我的码头文件:
django的:
FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN groupadd -r django \
&& useradd -r -g django django
COPY ./requirements.txt /requirements.txt
RUN pip install --no-cache-dir -r /requirements.txt \
&& rm -rf /requirements.txt
COPY ./compose/django/gunicorn.sh /
RUN sed -i 's/\r//' /gunicorn.sh \
&& chmod +x /gunicorn.sh \
&& chown django /gunicorn.sh
COPY . /app
RUN chown -R django /app
RUN mkdir /static
RUN chown -R django /static
USER django
WORKDIR /app
nginx的:
FROM nginx:latest
ADD nginx.conf /etc/nginx/sites-enabled/django_blog.conf
gunicorn.sh:
#!/bin/sh
python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn blogproject.wsgi -w 4 -b 127.0.0.1:8000 --chdir=/app
nginx.conf:
server {
charset utf-8;
listen 80 default_server;
location /static {
alias /app/static;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8000;
}
}
搬运工-compose.yml:
version: '3'
services:
django:
build:
context: .
dockerfile: ./compose/django/Dockerfile
command: /gunicorn.sh
nginx:
build: ./compose/nginx
depends_on:
- django
ports:
- "80:80"
我跑的命令:
docker-compose build
docker-compose up
似乎Nginx没有将请求传递给gunicorn?
更新
根据@Robert的建议,事情有效。但是,由于静态文件在django容器中,所以我不知道如何让nginx托管它们?我尝试按照Volume configuration reference设置卷,如下所示:
version: '3'
services:
django:
build:
context: .
dockerfile: ./compose/django/Dockerfile
volumes:
- static-volume:/app/static
command: /gunicorn.sh
nginx:
build: ./compose/nginx
volumes:
- static-volume:/app/static
depends_on:
- django
ports:
- "0.0.0.0:80:80"
volumes:
static-volume:
但似乎不起作用?我该如何让nginx访问django容器中的静态文件?谢谢!
答案 0 :(得分:2)
在nginx.conf中正确指向django:
proxy_pass http://django:8000;
感谢docker网络。
而且,我建议你覆盖默认的nginx conf。所以改变这个:
ADD nginx.conf /etc/nginx/sites-enabled/django_blog.conf
对此:
ADD nginx.conf /etc/nginx/conf.d/default.conf