【问题标题】:Running nginx and gunicorn in the same docker file在同一个 docker 文件中运行 nginx 和 gunicorn
【发布时间】:2022-01-14 05:19:03
【问题描述】:

我有一个Dockerfile,最后运行run.sh

我想使用 nginx 在 8000 端口上运行 gunicorn 并在 80 到 8000 上代理请求。

问题是运行服务器是一个阻塞命令,它永远不会执行nginx -g 'daemon off;'

我能做些什么来处理这种情况?

这是run.sh 文件:

python manage.py migrate --noinput
gunicorn --bind=0.0.0.0:8000 bonit.wsgi:application &
nginx -g 'daemon off;'

这是Dockerfile

FROM python:3.8

# set work directory
WORKDIR /usr/src/app

# set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update &&  apt-get install -y nginx supervisor build-essential gcc libc-dev libffi-dev default-libmysqlclient-dev libpq-dev
RUN apt update && apt install -y python3-pip python3-cffi python3-brotli libpango-1.0-0 libpangoft2-1.0-0

RUN pip install --upgrade pip
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .

RUN adduser --disabled-password --gecos '' nginx
COPY nginx.conf /etc/nginx/nginx.conf

RUN python manage.py collectstatic --noinput

ENTRYPOINT ["sh", "/usr/src/app/run.sh"]

这里是nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;

    upstream app {
        server app:8000;
    }

    server {
        listen 80;
        server_name 127.0.0.1;
        charset utf-8;

        location /static/ {
            alias /static/;
        }

        location / {
            proxy_pass http://app;
        }
    }
}

【问题讨论】:

  • 标准方法是在两个单独的容器中运行它们。看起来 Nginx 代理实际上并没有做任何事情,您可能在此处运行 GUnicorn(让 Python 应用程序提供自己的静态资产)。
  • 是的,但我在 paas Web 服务上部署项目,我不能有多个容器我只能有一个 Dockerfile 并且 gunicorn 不能提供静态文件。

标签: python django docker nginx gunicorn


【解决方案1】:

要么在守护模式下先启动 nginx:

python manage.py migrate --noinput
nginx -g 'daemon on;'
gunicorn --bind=0.0.0.0:8000 bonit.wsgi:application

或者让 gunicorn 在 nohup 中运行:

python manage.py migrate --noinput
nohup gunicorn --bind=0.0.0.0:8000 bonit.wsgi:application &
nginx -g 'daemon off;'

【讨论】:

  • 谢谢我尝试了第一个,它成功了。
猜你喜欢
  • 2021-10-06
  • 2017-08-21
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2021-06-07
  • 2019-06-19
相关资源
最近更新 更多