Sign inSign up

davidlor/python-autoclonable-app

By davidlor

•Updated almost 7 years ago

One single Docker image to automatically deploy and run any Python app on Docker

Image
0

1.9K

davidlor/python-autoclonable-app repository overview

⁠Docker-Python-Autoclonable-App

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

DockerHub⁠

⁠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 means that one single image let you run any(1) Python project, 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.

(1) The project must follow a structure in order for the entrypoint to run it (see Python Project structure⁠).

⁠Changelog

  • 1.0.1 - Rewrite Dockerfile (NOT DEPLOYED YET to DockerHub!):
    • App setup script written in Python
    • Change Python base image tag through ARG
  • 0.2.2 - Option to set the GIT Branch to clone
  • 0.2.1 - Option to provide a SSH private key through ENV (encoded in base64).
  • 0.1.1 - Check Exit code after each command executed on the entrypoint. If some part fails, the container will stop.
  • 0.0.1 - Initial release.

⁠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 script will clone the repository in your container, install all the Python requirements through pip, and start it. After this first startup, the next time/s the container starts, the setup process is skipped.

App runs with a new user created on Dockerbuild (appuser by default), so the app won't run with root privileges.

⁠How to deploy

docker run -d -e GIT_REPOSITORY=<url to a Git repository> --name <containerName> davidlor/python-autoclonable-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: MyApp)
  • (ARG) USERNAME: name of the user that is created on Dockerbuild to run the app with (optional, default: appuser)
  • (ARG) IMAGE_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⁠.

⁠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/directories

Some examples of projects compliant with this structure are:

⁠Tags & Builds

  • The images are built and uploaded automatically, based on the tags latest, slim and alpine of the base Python image, on each push on the master branch.
  • Each push on the develop branch triggers a new image with the sufix -dev

⁠How to build

If you want to build this image (required in order to change default username or base image tag), 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 IMAGE_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 DockerPythonApp
docker build DockerPythonApp --build-arg USERNAME=<desiredUser> --build-arg IMAGE_TAG=<baseTag> -t yourname/yourtag:yourversion
docker run [...] yourname/yourtag:yourversion

⁠TODO

  • Run as root with a env variable - or another image tag
  • Load SSH key from directory for cloning SSH git repositories
  • Push version tags to DockerHub from Github Actions workflow

Tag summary

Content type

Image

Digest

Size

333.1 MB

Last updated

almost 7 years ago

docker pull davidlor/python-autoclonable-app