Is there a way to automatically activate virtualenv as a docker entry point?

I have a flash application inside a docker container. I would like to use the zappa python package to deploy this application to Amazon Web Services.

Unfortunately zappa requires it and all my app dependencies to be installed in a virtual environment ...

So, I rebuilt the docker image and moved everything to a virtual environment.

The problem is that now I cannot run commands like:

docker exec <container> flask <sub command>

      

because the bulb is installed in a virtual environment that has not been activated.

I can still do this:

host$ docker exec -it <container> bash

container$ source venv/bin/activate
container$ flask <sub command>

      

Also, I can no longer run my standard Dockerfile CMD (gunicorn) because this is also my virtual environment.

Does this make sense?

+8


source to share


3 answers


As an alternative to simply finding a script inline with a command, you can create a script that will act like ENTRYPOINT

. An example entrypoint.sh

would look something like this:

#!/bin/sh
source venv/bin/activate
exec "$@"

      

Then in yours Dockerfile

you copy that file and install it like ENTRYPOINT

:



FROM myimage
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

      

Now you can run it like docker run mynewimage flask <sub command>

or docker run mynewimage gunicorn

.

+9


source


You don't need to activate env. Add /path/to/virtualenv/bin

in $PATH

, then python

, pip

etc. Automatically point to commands in virtualenv.

FROM python:3.4-alpine
WORKDIR /deps
ENV PATH=/virtualenv/bin:$PATH
RUN pip install virtualenv && \
    mkdir virtualenv && \
    virtualenv /virtualenv
COPY . /deps

      



Example of work:

#Build dockerfile
docker build . -t="venv_example"
#Run all python commands in virtualenv w/ no hassle
docker run --rm venv_example which python
>/virtualenv/bin/python
docker run --rm venv_example which pip
>/virtualenv/bin/pip

      

+4


source


Try:

docker exec <container> sh -c 'source venv/bin/activate; flask <sub command>'

      

Your team could be:

CMD sh -c 'source venv/bin/activate; gunicorn...'

      

+1


source







All Articles