Giter Site home page Giter Site logo

david-lor / docker-python-autoclonable-app Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 48 KB

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

Dockerfile 16.57% Shell 83.43%
python-docker docker-image dockerbuild docker python python3 entrypoint

docker-python-autoclonable-app's Introduction

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

  • 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: none - use Git default branch)
  • SSH_KEY: SSH private key, base64 encoded. Required if a SSH git repository is provided. The SSH Key must have no passphrase (optional, default: none)
  • 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)

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:

  • Clone this repository
  • Build a new Docker image using the repository directory (optionally set a custom username using the USERNAME ARG)
  • 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> -t yourname/yourtag:yourversion
docker run [...] yourname/yourtag:yourversion

Changelog

  • 0.0.1: Initial release.
  • 0.1.1: Check Exit code after each command executed on the entrypoint. If some part fails, the container will stop.
  • 0.2.1: Option to provide a SSH private key through ENV (encoded in base64).
  • 0.2.2: Option to set the GIT Branch to clone

TODO

  • Allow to have the Python app on any other directory than root
  • Check if git clone and Python requirements install were successful
  • Allow to change the command to execute after the entrypoint

docker-python-autoclonable-app's People

Contributors

david-lor avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.