8

I'm trying to run a Docker container which complains with the error message: /bin/sh: gunicorn: not found. Firing up the server locally without Docker works fine. And building the image also works fine. I'm new to docker so I don't know if anything looks weird in my Dockerfile..

My Dockerfile:

FROM python:3.7-alpine

RUN adduser -D teamreacher
WORKDIR /home/teamreacher

# copy and install dependencies
COPY ./requirements.txt requirements.txt
RUN python -m venv venv
RUN venv/bin/pip install --upgrade pip
RUN venv/bin/pip install -r requirements.txt

# copy the app
COPY . .
RUN chmod +x boot.sh

RUN chown -R teamreacher:teamreacher ./
USER teamreacher

# expose port and run server
EXPOSE 5000

RUN source venv/bin/activate
CMD gunicorn -b :5000 --access-logfile - --error-logfile - wsgi:app

And my requirements.txt:

Flask==1.0.2
Flask-RESTful==0.3.6
Flask-SQLAlchemy==2.3.2
Flask-JWT==0.3.2
Flask-Cors==3.0.7
gunicorn==19.9.0
Weblurk
  • 6,562
  • 18
  • 64
  • 120
  • 1
    Just try to replace the last two lines of your Dockerfile with the next line: `RUN venv/bin/gunicorn -b :5000 --access-logfile - --error-logfile - wsgi:app` – Nazarii Plebanskii Dec 13 '18 at 13:48

2 Answers2

11

A RUN command creates a layer, it's like running the command in a new shell. When it completes, the 'shell' exits. So any following commands will not be affected.

You can add a shell script (startup.sh) like,

#!/bin/sh
source venv/bin/activate
gunicorn -b :5000 --access-logfile - --error-logfile - wsgi:app

then CMD ["./startup.sh"]

PS:

There is little interest for using virtual env in docker container. A container is already an isolated environment and it's supposed to do only one thing.

Siyu
  • 11,187
  • 4
  • 43
  • 55
  • This solved it, even if I don't understand exactly why. Also, you're absolutely right about virtual env not being necessary in docker. Didn't occur to me since I've been so used to always venv in my flask apps. – Weblurk Dec 13 '18 at 14:14
  • @Weblurk I updated the explanation, does it help you better understand the issue? – Siyu Dec 13 '18 at 14:20
0

My packages installation looked like this:

RUN pip install -r requirements.txt --target=/app/python

I had to remove the --target=/app/python then pod status started showing Running (got fixed).

So the fix for me was:

RUN pip install -r requirements.txt
Libby Lebyane
  • 167
  • 2
  • 14