davidlor/python-git-app

By davidlor

Updated about 23 hours ago

Deploy a Python app from a Git repository, to avoid building a Docker image for each app

Image
2

50K+

davidlor/python-git-app repository overview

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

  • Clone from GIT repository
  • Choose branch to clone
  • Install requirements from requirements.txt file
  • Multiple tags available, with same names as those in the official Python image

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 -e GIT_REPOSITORY="https://github.com/David-Lor/Python-HelloWorld.git" davidlor/python-git-app

ENV Variables & ARGs

  • 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)
  • (ARG) USERNAME: name of the user that is created on Dockerbuild to run the app with (optional, default: user)
  • (ARG) BASE_TAG: tag of the Python base image to be used for the build (optional, default: latest)

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

Available tags

The tags available for the image are a limited selection of tags used in the official Python image. The building and publishing of the images into DockerHub is performed by this Github Actions workflow, where the full list of supported tags is defined.

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:

  • Clone this repository
  • Build a new Docker image using the repository directory - you can optionally set these ARGs:
    • a custom username using the USERNAME ARG
    • a custom Python base image tag using the BASE_TAG ARG (example: alpine or slim)
  • Create a new container, setting up the desired ENV variables
git clone https://github.com/David-Lor/Docker-Python-Autoclonable-App.git DockerPythonClonable
docker build DockerPythonClonable --build-arg USERNAME=user --build-arg BASE_TAG=slim -t yourname/yourtag:yourversion
docker run [...] yourname/yourtag:yourversion

Entrypoint pipeline

The steps that run when the container starts are:

  • If this is the first time the container runs:
    1. clear output directory
    2. git clone
    3. pip install requirements.txt
    4. create a status file to mark the container already ran this setup process
  • Start the cloned app

Useful Make utils

  • 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)
  • test-install-requirements - pip install test requirements
  • sudo build BASE_TAG=slim - build the image with the python:slim base image, and tag as python-git-app:slim
  • sudo build BASE_TAG=alpine IMAGE_TAG=my-python:latest - build the image with the python:alpine base image, and tag as my-python:latest

Changelog

  • 0.1.1
    • Upload to DockerHub from Github Workflow
    • Fix cmd in Dockerfile, change from bash to sh (Alpine compatibility)
    • Fix test-nobuild* rules in Makefile
    • Fix image_tag param on tests
    • Rename IMAGE_TAG to IMAGE_NAME when refering to the full image name (name:tag)
    • Remove Python 2 from tags (not officially supported here)
  • 0.0.1
    • Initial release

TODO

  • Allow setting GIT repository through CMD
  • Load SSH private key for cloning SSH git repositories (from path or secret)
  • Create multi-arch images
  • Run as root with an env variable - or another image tag
  • Tag & upload images based on official Python image tags, plus versions of this repository

Tag summary

Content type

Image

Digest

sha256:d1d6d48a6

Size

78.2 MB

Last updated

about 23 hours ago

Requires Docker Desktop 4.37.1 or later.