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

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.

  • Multi Host : Docker Networking
  • Using sidecar container to retrieve Chef data bag secret value
  • CannotStartContainerError while submitting a AWS Batch Job
  • Submitting a jar to sequenceiq docker-spark container
  • Why does boot2docker ip fail?
  • Docker: how to use container to replace real smtp server?
  • 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?

  • Setting up Nginx Proxy in Docker using Ansible
  • Keep docker-compose container running after ENTRYPOINT
  • Google cloud container engine vs normal vm
  • Pulling docker images
  • Gitlab runner: Use own (exiting) docker container
  • Django in Docker using PyCharm
  • 2 Solutions collect form web for “Is there a way to automatically activate a virtualenv as a docker entrypoint?”

    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/sh
    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
    ENTRYPOINT ["/entrypoint.sh"]
    

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

    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...'
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.