Is there a way to automatically activate a virtualenv as a docker entrypoint?

how to use virtualenv in docker
docker default entrypoint
docker entrypoint arguments
venv docker container
activate python virtualenv in dockerfile
alpine virtualenv
docker python3 7 virtualenv
docker pyenv virtualenv

I have a flask app inside of a docker container. I would like to use the python package zappa to deploy that app to Amazon Web Services.

Unfortunately zappa requires that it and all of my apps dependencies be installed in a python virtual environment.

So I have rebuilt my docker image and moved everything into a virtual environment in it.

The problem is that now i can't run commands like:

docker exec <container> flask <sub command>

because flask is installed in a virtual environment which 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 default Dockerfile CMD (gunicorn) because that is also is my virtual environment.

Does this make any more sense?

As an alternative to just sourcing the script inline with the command, you could make a script that acts as an ENTRYPOINT. An example entrypoint.sh would look something like:

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

Then in your Dockerfile you would copy this file and set it as the ENTRYPOINT:

FROM myimage
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

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

As an alternative to just sourcing the script inline with the command, you could make a script that acts as an ENTRYPOINT . An example entrypoint.sh would look  Since you’re using a virtualenv, you need to activate it—but if you’re just getting started with Dockerfiles, the naive way doesn’t work. And even if you do know how to do it, the usual method is repetitive and therefore error-prone. There is a simpler way of activating a virtualenv, which I’ll demonstrate in this article.

You don't need to activate the env. Add /path/to/virtualenv/bin to $PATH, then python, pip, etc. automatically point to the commands in the virtualenv.

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

Example working:

#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

There is a simpler way of activating a virtualenv, which I'll demonstrate in this article. When you run the resulting Docker image it will run the CMD —which The virtualenv now automatically works for both RUN and CMD  If your projects have consistent naming, e.g. the virtual environment is always in a sub-directory called "venv", you can automatically activate the venv when you cd to the parent project directory by adding this to the end of your $HOME/.bashrc file (as a bonus, the script also shows name of the project / virtual environment parent in the prompt).

Try:

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

Your command can be:

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

So how do you activate a Conda environment in a Dockerfile? RUN conda env create -f environment.yml # Activate the environment, and make sure it's Unlike the activate script for the Python virtualenv tool, which just sets an environment variable or A failed solution, part #2: Activate automatically. I would recommend using the "system Python" in a Docker container and not using virtualenvs in Docker. You do not need to use venvs in a docker container. It's overly complicated and probably provides no benefit unless you have a very specific use-case. There is no need to activate each command.

A clean way to locally install python dependencies with pip in Docker build a virtualenv virtualenv venv # activate the virtualenv source venv/bin/activate # install docker-compose run --rm vendors How To Automatically Update Your Angular Offline Webapps · Stateful modals with Angular UI router →. So when you’re building a Docker image for a Conda-based application, you’ll need to activate a Conda environment. Unfortunately, activating Conda environments is a bit complex, and interacts badly with the way Dockerfiles works.

EXPOSE 8000 RUN virtualenv venv RUN source venv/bin/activate RUN pip3 install I would recommend using the "system Python" in a Docker container and not post with arduino controlled auto ambient lighting, I creating my own version  If you work with Python, you probably work with virtualenv. If you work with virtualenv, you probably work with virtualenvwrapper. Nevertheless, if you work a lot with many virtualenvs at the same time, you may become tired of al these “. bin/activate” or “workon smthng”.

Docker containers package all the dependencies needed to run a piece of software. Such that one virtual environment may have Tensorflow 1.13 and Keras These dependencies as well as the instruction on how specifically to images” list, docker automatically checks the docker hub to attempt to  Recently I wrote a post on using Docker for Jupyter and Python with the Intel Python2 distribition for data science. With this, I got some feedback to use virtualenv instead of Docker. I started using Docker for the desire to learn how to make portable environments that can be moved from one machine to another with ease.

Comments
  • Your container shouldn't have any attachment to whatever environment you run on your host machine; rather, you should look to run your Python app/env in your container.
  • The virtualenv was created in the container at build. I'd like to have it automatically invoked by docker on entry, so when a CMD is passed, is executed in the context of the virtualenv.
  • Can you list some commands that you would normally use to do this? It sounds like you just need a simple shell script with the commands that you use as an ENTRYPOINT in the Dockerfile. It is something we can help with but you don't provide much information on what needs to happen.
  • Ok, I tried to say exactly what my problem is. Thank you.
  • Awesome! I had to add RUN chmod 700 entrypoint.sh before the ENTRYPOINT as well. Thanks!
  • Yes awesome great idea. Minor nitpick, the shebang line should really be bash not sh because sh doesn't have source it uses the dot .
  • Thanks for pointing this out! I feel dumb for not having thought it myself. In my opinion this is the best way, much better than what the other answers suggest.
  • OOOOHHHH yea. I've spent many hours over the years trying to come up w/ something that was clean, and let me run commands against venv.
  • Really elegant !