运行 nginx 和 gunicorn 在同一个 docker 文件中

Running nginx and gunicorn in the same docker file

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

我想 运行 端口 8000 上的 gunicorn 和 80 到 8000 上的代理请求与 nginx。

问题是 运行ning 服务器是一个阻塞命令,它永远不会执行 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 :

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

或者在 nohup 中有 gunicorn 运行 :

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