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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With