我的 django、docker、nginx 和 Gunicorn 設定遇到問題。即使在運行伺服器時,我也無法顯示靜態檔案collectstatic
。當我有nginx 時,我要么收到一條錯誤消息,說我正在使用的端口已分配,要么,如果我更改端口,我會得到“壞網關”,但仍然能夠使用gunicorn正在使用的端口- 在我的情況下,連接埠 8000。
我的 docker-compose.yml 檔案:
version: '3'
services:
db:
image: postgres:latest
container_name: rapid_goat_postgres_server_1
nginx:
image: nginx:latest
container_name: rapid_goat_nginx_1
ports:
- "8000:8000"
volumes:
- .:/code
- ./nginx:/etc/nginx/conf.d
- /static:/static
depends_on:
- web
web:
build: .
container_name: rapid_goat_django
command: bash -c "chmod -R 755 . && cd rapidgoat_scraper_product && python3 manage.py collectstatic --noinput && python3 manage.py makemigrations && python3 manage.py migrate && gunicorn rapidgoat_scraper.wsgi -b 0.0.0.0:8000 --workers 6"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
我的 nginx 設定檔:
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
root /code/static/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
以原樣使用上述兩個文件,運行時出現以下錯誤docker-compose up --build
:
ERROR: for 693800faea42_693800faea42_693800faea42_my_nginx_docker_container Cannot start service nginx: driver failed programming external connectivity on endpoint my_nginx_docker_container (48eab4028fad0c54d8bbf5669fe746c24f293cfeb11dc57c8566d4010a995a83): Bind for 0.0.0.0:8000 failed: port is already allocated
ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on endpoint my_nginx_docker_container (48eab4028fad0c54d8bbf5669fe746c24f293cfeb11dc57c8566d4010a995a83): Bind for 0.0.0.0:8000 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
然後,當我從 docker-compose.yml 檔案中刪除 nginx 服務時,我可以啟動 Gunicorn 伺服器,但它不提供靜態檔案。我也在應用程式模板中使用,所以我的 django 專案結構如下所示:
- project
-- __init__.py
-- settings.py
-- urls.py
-- wsgi.py
- appname
-- migrations
-- static
--- appname
... then rest of static files like js, css, images... etc
-- templates
--- appname
... then all of my html files. I am using django templating.
-- admin.py
-- apps.py
-- forms.py
-- models.py
-- tests.py
-- views.py
我也在使用 django 2.0.3
編輯:
當我刪除 nginx 時,這裡是相同的檔案 - 與我更改 nginx 的連接埠時相同 - 並且gunicorn不提供靜態服務:
我的 docker-compose.yml 檔案:
version: '3'
services:
db:
image: postgres:latest
container_name: rapid_goat_postgres_server_1
web:
build: .
container_name: rapid_goat_django
command: bash -c "chmod -R 755 . && cd rapidgoat_scraper_product && python3 manage.py collectstatic --noinput && python3 manage.py makemigrations && python3 manage.py migrate && gunicorn rapidgoat_scraper.wsgi -b 0.0.0.0:8000 --workers 6"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
哦,我的 Dockerfile 是:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
RUN mkdir /static
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/