Home

Awesome

Docker Python Git App

Docker Hub Build status

A Docker image to deploy a Python app from a Git repository, to avoid building a Docker image for each app. The container will handle the git clone and requirements installing before the app starts for the first time.

Features

Important! Required Python project structure

The entrypoint script expects the cloned repository to have the following structure:

ProjectRoot (cloned through Git)
ā”‚-  __main__.py (app entrypoint that will run)
|-  requirements.txt (if required)
ā”‚-  ...and all the other project files/directories

Some examples of projects compliant with this structure are:

Getting started

docker run -it --rm -e GIT_REPOSITORY="https://github.com/David-Lor/Python-HelloWorld.git" davidlor/python-git-app

ENV Variables & ARGs

Only required variable is (ENV) GIT_REPOSITORY. The variables marked with (ARG) are build-args, used on image build.

If your Python script/app is CLI-based and requires to be called with arguments (for example python . worker --instances=10), you can provide them as Docker command arguments, like following:

docker run -it --rm -e GIT_REPOSITORY="https://github.com/David-Lor/Python-HelloWorld.git" -e GIT_BRANCH="args" davidlor/python-git-app worker --instances=10
# This command will clone an existing branch on the example repository, and print out the custom commands provided

Available tags

Building

If you want to build this image (required in order to change default username, base image tag or building for unsupported architedtures), you must do on host machine:

git clone https://github.com/David-Lor/Docker-Python-Autoclonable-App.git DockerPythonClonable
docker build DockerPythonClonable --build-arg USERNAME=user --build-arg FROM_IMAGE=python:slim -t yourname/yourtag:yourversion
docker run [...] yourname/yourtag:yourversion

Entrypoint pipeline

The steps that run when the container starts are:

Caching requirements

The startup process of new containers may be lighten up by persisting the local cache (and even the installed libraries) in volumes. This way, multiple containers, or the same container when being recreated for upgrading or reinstalling the running application, can skip the download and/or installing process. The two directories that can be bind to volumes are:

It is important that the mounted directories are owned by UID:GID 1000:1000, since the container runs as a non-root user.

Example:

# Create the volumes, unless using binds
docker volume create pythongitapp-cache
docker volume create pythongitapp-local

# Change ownership
docker run -it --rm -v pythongitapp-local:/mnt/local -v pythongitapp-cache:/mnt/cache alpine sh -c "chown 1000:1000 /mnt/*"

# Run
docker run -it --rm -e GIT_REPOSITORY="https://github.com/David-Lor/Python-HelloWorld.git" -e GIT_BRANCH="fastapi" -v pythongitapp-local:/home/user/.local -v pythongitapp-cache:/home/user/.cache davidlor/python-git-app

# Ctrl+C to stop it (container will be removed)
# "docker run" again for verifying that requirements are not downloaded/installed again

Useful Make utils

Changelog

TODO