Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gunicorn not found when running a docker container with venv

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
like image 687
Weblurk Avatar asked Dec 13 '18 13:12

Weblurk


People also ask

Can Gunicorn run on Docker?

Last updated 04 Jun 2019, originally created 20 May 2019 Gunicorn is a common WSGI server for Python applications, but most Docker images that use it are badly configured. Running in a container isn’t the same as running on a virtual machine or physical server, and there are also Linux-environment differences to take into account.

Why can’t I run Docker images inside my virtualenv?

When you run the resulting Docker image it will run the CMD —which also isn’t going to be run inside the virtualenv, since it too is unaffected by the RUN processes. One solution is to explicitly use the path to the binaries in the virtualenv.

How to build and run an app from a docker container?

At this point you can use docker build to build your app image and docker run to run the container on your machine. By default, the docker build command looks for a Dockerfile in the current directory to find its build instructions.

How to run Gunicorn on localhost port 8000?

CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "mysite.wsgi:application"] By default, containers using this image will execute gunicorn bound to localhost port 8000 with 3 workers, and run the application function in the wsgi.py file found in the mysite directory.


1 Answers

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.

like image 180
Siyu Avatar answered Oct 22 '22 22:10

Siyu