Home

Awesome

Docker-Python-Autoclonable-App

IMPORTANT: this repository is deprecated. The project is replaced with: https://github.com/David-Lor/Docker-Python-Git-App (same functionality)

šŸ šŸ³ A Docker that downloads a Git repository with a Python app you want to deploy when a container runs for the first time

DockerHub: https://hub.docker.com/r/davidlor/python-autoclonable-app/

Objective

Run a Python app that is hosted on any Git repository, without having to create a specific Docker image for that project and version.

This image lets you create a container, setting during the container creation process the URL of the Git repository the project is hosted on.

Advantages

One single image lets you run any number of Python projects, without having to build one image per project. The latest/wanted version of that project is cloned during the first execution of the container trough Git. Any update on the project can be downloaded by just rebuilding the container.

How does it work?

When you create a new container and properly set the Git URL (using the GIT_REPOSITORY ENV), during the first execution, the entrypoint bash script will clone that repository to your container, install all the Python requirements through pip, and start it.

After this first startup, whenever the container is started, a hidden file will tell the entrypoint script that the container has been executed for the first time before, and won't clone the app through Git, just starting it.

App runs with a new user created on Dockerbuild (appuser by default), so the app won't run with root privileges (if you want to run with root, check out the Run as root. Everything is intended to live within /home directory of this user, so keep this in mind when you want to bind/mount a data volume for persistence.

How to deploy?

docker run -d -p <desired>:<ports> \
-e GIT_REPOSITORY=<url to a Git repository> \
--name <containerName> \
davidlor/python-autoclonable-app

Deploy using SSH

If the repository is private and can be cloned using SSH instead of HTTP/S, then you must provide the SSH Private Key to the container through a ENV Variable, but encoded in Base64. The SSH Key must have no passphrase

You can take advantage of private Github repositories and clone them as a container using this image. Just set a Deploy Key on your repository settings (learn more)

Use the b64encode.sh script to encode your key. This will output a file with the .base64 extension, which content is used as the SSH_KEY ENV variable on your container.

chmod u+x b64encode.sh
./b64encode.sh your_private_key
# A file named 'your_private_key.base64' is generated. You can pass the content directly to docker run:
docker run -d -p <desired>:<ports> \
-e GIT_REPOSITORY=<SSH url to a Git repository> \
-e SSH_KEY="$(cat your_private_key.base64)" \
--name <containerName> \
davidlor/python-autoclonable-app

ENV Variables & ARGs

Only required variable is (ENV) GIT_REPOSITORY.

Python Project structure

The entrypoint script expects the cloned Python app to have the following structure:

ProjectRoot (cloned through Git)
ā”‚   __main__.py
|   requirements.txt (if required)
ā”‚   ...and all the other project files

Run as root

Use the tag root instead of latest:

docker run [...] davidlor/python-autoclonable-app:root

Source files of root tag are available on the root branch.

How to build?

If you want to build this image (required in order to change default username), you must do on host machine:

git clone https://github.com/David-Lor/Docker-Python-Autoclonable-App.git DockerPythonApp
docker build DockerPythonApp --build-arg USERNAME=<desiredUser> -t yourname/yourtag:yourversion
docker run [...] yourname/yourtag:yourversion

Changelog

TODO