Home > Software design >  Running nginx and gunicorn in the same docker file
Running nginx and gunicorn in the same docker file

Time:12-09

I have a Dockerfile which at the end runs run.sh.

I want to run gunicorn on port 8000 and proxy requests on 80 to 8000 with nginx.

The problem is running server is a blocking command and it never executes nginx -g 'daemon off;'.

What can I do to handle this situation?

Here is the run.sh file:

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

And this the 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"]

And here is the 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;
        }
    }
}

CodePudding user response:

Either start nginx first in daemon mode :

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

Or have gunicorn running in nohup :

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