简体   繁体   中英

gunicorn: command not found Flask

I have created a flask app and it is running fine locally using this command gunicorn --workers 2 -b:5000 wsgi:app
Now I'm trying to run the same application in docker and I'm getting ./gunicorn_starter.sh: line 3: gunicorn: command not found .

my dockerfile:

FROM python:3.5

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt --user

COPY . .
RUN chmod +x gunicorn_starter.sh
CMD ["./gunicorn_starter.sh" ]

gunicorn_starter.sh:

#!/bin/sh
source venv/bin/activate
gunicorn --workers 2 -b :5000 wsgi:app

wsgi.py:

from app import app

if __name__ == "__main__":
    app.run(debug=True)

File structure:

flask_app
    >venv
    dockerfile
    gunicorn_starter.sh
    app.py
    wsgi.py
    requirements.txt

I do have gunicorn in require.nets.txt file.
I have tried following links:
gunicorn not found when running a docker container with venv
Installed gunicorn but it is not in venv/bin folder
Gunicorn throwing error - cannot find executable on $PATH (Docker/Nginx)

I couldn't figure out why it is not working, please help Thanks.

Probably the most significant issue in what you show is that you're trying to COPY your host's virtual environment in the venv directory into the Docker image, but virtual environments aren't portable across installations. So if the non-standard source command succeeds (if /bin/sh is actually GNU bash) you could be trying to run ./venv/bin/gunicorn , but that's connected to a Python installation that doesn't exist inside the container.

The first thing you should do is write a .dockerignore file that includes the line

venv

That will keep your host-based virtual environment out of the image build. This avoids problems where the Python doesn't match (maybe it's not even the same OS) and also will make the docker build sequence run significantly faster.

The Docker image is isolated from other Python installations by virtue of being in Docker, so you don't need a virtual environment here. One standard approach is to install packages into the "system" Python, but inside the isolated image. This means you don't need to "activate" anything and you can dispense with the shell-script wrapper as well.

A simplified Dockerfile could look like:

FROM python:3.5

WORKDIR /app

COPY requirements.txt ./              # can skip second filename
RUN pip3 install -r requirements.txt  # without --user

COPY . .

CMD gunicorn --workers 2 -b :5000 wsgi:app  # without shell-script wrapper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM