Is there any standard to deploy flask app with gunicorn and nginx on kubernetes cluster
because I am trying to it by on dockerfile like below:
FROM nginx:latest as base-image
RUN apt update
RUN apt -y install python3 python3-pip
RUN apt -y install build-essential
RUN mkdir /app
WORKDIR /app
COPY src src/
COPY src/.nginx/config /etc/nginx/conf.d/default.conf
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
RUN pip3 install -e ./src
RUN pwd
RUN ls -l /app/src
# RUN pytest
EXPOSE 80
WORKDIR /app/src
CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT ["gunicorn" , "--bind","0.0.0.0:8000", "wsgi:app"]
Is the solution is to run two containers for each of gunicorn and nginx inside one pod of kubernetes?
>Solution :
Is the solution is to run two containers for each of gunicorn and nginx inside one pod of kubernetes?
Yes. In Kubernetes or when simply running Docker on your local machine, it is always better to compose multiple containers rather than trying to stuff everything into a single container.
This part of your your Dockerfile:
CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT ["gunicorn" , "--bind","0.0.0.0:8000", "wsgi:app"]
Isn’t doing what you seem to expect. These two directives operate in concert:
- If defined,
ENTRYPOINTis the command run by the container when it starts up. - The value of
CMDis provided as an argument to theENTRYPOINTscript.
You can read more about ENTRYPOINT in the official documentation.
A better design would be to use the official Python image for your Python app:
FROM python:3.10
WORKDIR /app
COPY src src/
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
RUN pip3 install -e ./src
WORKDIR /app/src
CMD ["gunicorn" , "--bind","0.0.0.0:8000", "wsgi:app"]
And then use the official nginx image to run the nginx service.