🐳Docker image to deploy a 🐍Python app hosted on a Git repository
APACHE-2.0 License
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.
requirements.txt
filelinux/amd64
, linux/arm/v7
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:
docker run -it --rm -e GIT_REPOSITORY="https://github.com/David-Lor/Python-HelloWorld.git" davidlor/python-git-app
GIT_REPOSITORY
: URL of the remote Git repository to get the app from (required)GIT_BRANCH
: set the Branch to clone from the Git repository (optional, default: use default branch)APP_NAME
: name of your app. This name is given to the directory where project is cloned on(optional, default: PythonApp)USERNAME
: name of the user that is created on Dockerbuild to run the app with (optional, default: user)FROM_IMAGE
: base full image name to be used for the build (optional, default: python:latest)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
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:
USERNAME
ARGFROM_IMAGE
ARG (example: python:alpine
or python:slim
)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
The steps that run when the container starts are:
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:
/home/user/.cache
: cached libraries; mounting this volume will avoid downloading requirements/home/user/.local
: installed libraries; mounting this volume will avoid installing requirementsIt 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
make test
- run tests (requires root/sudo & pytest)make test USE_SUDO=0
- run tests without sudo (if current user is root or part of docker group)make test-classic
- run tests sequentially (make test runs in parallel using pytest-xdist)make test-install-requirements
- pip install test requirementssudo make build FROM_IMAGE=python:slim TO_TAG=slim
- build the image with the python:slim
base image, and tag as python-git-app:slim
sudo make build FROM_IMAGE=python:alpine TO_IMAGE=my-python:latest
- build the image with the python:alpine
base image, and tag as my-python:latest
bash
to sh
(Alpine compatibility)test-nobuild*
rules in Makefileimage_tag
param on testsIMAGE_TAG
to IMAGE_NAME
when refering to the full image name (name:tag
)