pyentrypoint manages entrypoints in Docker containers.
Project description
pyentrypoint
pyentrypoint is a tool written in Python to manage Docker containers ENTRYPOINT.
This tool avoids writing shell scripts to:
- Handle commands and sub commands
- Identify linked containers
- Generate configuration using
jinja2templates - Run commands before starting service
- Reload service when configuration has changed
Changelog
v0.7.0 (2020-05-17)
- Add command matching setup
V0.6.0 (2020-05-10)
- Drop python 2 support
- Deprecation of
commandandsubcommandssettings forcommands(see bellow)
Usages
Install in container
All you need to do is to setup a yaml file called entrypoint-config.yml and to install pyentrypoint in your Dockerfile using pip.
FROM debian
# Installing git for example
RUN apt-get update && apt-get install git python-pip -y
# Install pyentrypoint
RUN pip install pyentrypoint
# Copy config file in the current WORKDIR
COPY entrypoint-config.yml .
# Set ENTRYPOINT
ENTRYPOINT ['pyentrypoint']
# git will be the default command
CMD ['git']
FROM alpine
# Installing git for example
RUN apk add --update py-pip git
# Install pyentrypoint
RUN pip install pyentrypoint
# Copy config file in the current WORKDIR
COPY entrypoint-config.yml .
# Set ENTRYPOINT
ENTRYPOINT ['pyentrypoint']
# git will be the default command
CMD ['git']
Using docker-image
FROM goldy/pyentrypoint:python3
# ONBUILD statement add entrypoint-config.yml in current directories
Available with many flavours:
goldy/pyentrypoint:python3goldy/pyentrypoint:python3.6goldy/pyentrypoint:python3.7goldy/pyentrypoint:python3.8goldy/pyentrypoint:python3-alpinegoldy/pyentrypoint:python3.6-alpinegoldy/pyentrypoint:python3.7-alpinegoldy/pyentrypoint:python3.8-alpine
Working examples
Setup entrypoint
This is an example of entrypoint-config.yml file.
# Entrypoint configuration example
# This entry list commands handled by entrypoint.
# If you run the container with a command not in this list,
# pyentrypoint will run the command directly without any action
# If this setting and `command` are not set, all commands will be handled.
# Support wildcard
commands:
- git
- sl*
# DEPRECATED: This option is remplaced by `commands`
# This entry should reflect CMD in Dockerfile
# If `commands` is present, this option will be ignored.
# DEPRECATED: This option is remplaced by `commands`
command: git
# DEPRECATED: This option will be dropped
# This is a list with some subcommands to handle
# when CMD is not `git` here.
# By default, all args started with hyphen are handled.
# DEPRECATED: This option will be dropped
subcommands:
- "-*"
- clone
- init
- ls-files
# etc...
# User and group to run the cmd.
# Can be name or uid/gid.
# Affect only command handled.
# Dockerfile USER value by default.
user: 1000
group: 1000
# These files should exist (ADD, COPY or mounted)
# and should be jinja templated.
# Note: if config files end with ".tpl", the extension will be removed.
config_files:
- /etc/gitconfig
- .ssh/config.tpl # Will apply to ".ssh/config"
- /tmp/id_rsa: .ssh/id_rsa # Will apply "/tmp/id_rsa" template to ".ssh/id_rsa"
# These environment variables will be wiped before
# exec command to keep them secret
# CAUTION: if the container is linked to another one,
# theses variables will passed to it anyway
secret_env:
- SSHKEY
- '*' # Support globbing, all environment will be wiped
# Links are handled here
# Port, name, protocol or env variable can be used to identify the links
# Raise an error if the link could not be identified
# This is not supported when using docker network or docker-compose v2.
links:
'ssh':
port: 22
name: 'ssh*'
protocol: tcp
# env can be list, dictionary or string
env:
FOO: bar
# Single doesn't allow multiple links for this ID
# false by default
single: true
# Set to false to get optional link
# true by default
required: true
# Commands to run before applying configuration
pre_conf_commands:
- echo something > to_this_file
# commands to run after applying configuration
post_conf_commands:
- echo "something else" > to_this_another_file
post_run_commands:
- echo run commands after started service
# Reload service when configuration change by sending a signal to process
reload:
signal: SIGHUP # Optional, signal to send, default is SIGHUP
pid: 1 # Optional, pid to send signal, default is 1
watch_config_files: true # Optional, watch defined config files, default True
files: # Optional, list of files to watch
- /etc/conf/to/watch
- /file/support/*.matching
# can also be enabled like this:
reload: true
# Cleanup environment from variables created by linked containers
# before running command (True by default)
clean_env: true
# Enable debug to debug
debug: true
# Do not output anything except error
quiet: false
Handled command matching
All settings can be mapped to an handled command.
For instance:
# This config will handle command `abc` and `xyz`
commands:
- abc
- xyz
# you can map commands to handled commands bellow
pre_conf_commands:
- abc:
- echo "will run for command abc"
- xyz:
- echo "will run for command xyz"
- echo "Can be multiple"
- echo "Will run for both commands"
user:
- abc: 1000
- xyz: 1001
# Mapping can also be a dictionnary
group:
abc: 1000
xyz: 1001
# Etc
Not supported for deprecated settings command, subcommands and links.
Config templates
You can generate configuration for your service with jinja2 template.
links and containers are not supported with docker network and docker-compose v2.
Here is an example for an hypothetical ssh config file:
host server:
hostname {{links.ssh.ip}}
port {{links.ssh.port}}
Templates will be replaced with ip address and port of the identified link. All links can be accessed from links.all, this is a tuple of links you can iterate on it.
{% for link in links.all %}
host {{link.names[0]}}
hostname {{link.ip}}
port {{links.port}}
{% endfor %}
If you change the option single to false in the entrypoint-config.yml, the identified link ssh will become a tuple of links. You must iterate on it in the jinja template.
{% for link in links.ssh %}
host {{link.names[0]}}
hostname {{link.ip}}
port {{links.port}}
{% endfor %}
Accessing environment in template.
{% if 'SSHKEY in env' %}
{{env['SSHKEY']}}
{% endfor %}
Accessible objects
You have 4 available objects in your templates.
configlinkscontainersenvironyamljson
config
Config reflect the config file. You can retrieve any setup in this object.
(see config.py)
links
Not supported with docker network and docker-compose v2
Links handles Link objects. You can identify links using wildcard patterns in the configuration file.
link is related to one physical link (one ip and one port).
link handles the following attributes:
ip- link ip
port- link port (integer)
environ- related container environment
protocol- link protocol (
tcporudp)
- link protocol (
uri- link URI (example:
tcp://10.0.0.3:80)
- link URI (example:
names- tuple of related container names
containers
Not supported with docker network and docker-compose v2
containers handles a tuple of container object.
container handles the following attributes:
ip- container ip
environ- container environment
names- List of containers names
- Names are sorted by length, but container ID will be the last element.
- List of containers names
id- Hexadecimal container ID (if available, empty string else)
links- Tuple of
linkobjects related to this container
- Tuple of
environ
environ is the environment of the container (os.environ).
env is an alias to environ.
yaml and json
yaml and json objects are respectively an import of PyYAML and json modules.
They are useful to load and dump serialized data from environment.
# Here yaml is present in SETUP_YAML environment variable
{% set data = yaml.load(env['SETUP_YAML'])%}
{{data['param']}}
# Here json is present in SETUP_JSON environment variable
{% set data = json.loads(env['SETUP_JSON'])%}
{{data['param']}}
Setup
Some setups can be overridden using environment variables.
ENTRYPOINT_CONFIGoverrides path ofentrypoint-config.ymlfile.ENTRYPOINT_FORCEis applying configuration and runs pre and post conf commands even if thecommandprovided is not handled.ENTRYPOINT_PRECONF_COMMANDrun an extra pre conf shell command after all pre conf commands.ENTRYPOINT_POSTCONF_COMMANDrun an extra post conf shell command after all post conf commands.ENTRYPOINT_DEBUGenables debug logs.ENTRYPOINT_RAWdoes not use logging to display pre and post conf commands. This can be useful if output is serialized.ENTRYPOINT_DISABLE_RELOADdisable reload system even if it is enabled inentrypoint-config.yml.ENTRYPOINT_USERoverridesuserin config.ENTRYPOINT_GROUPoverridesgroupin config.ENTRYPOINT_DISABLE_SERVICEexits container with 0 before doing anything. Useful to disable container using environement.
Running Tests
To run tests, ensure that docker-compose and make are installed and run
$ make test
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyentrypoint-0.7.0.tar.gz.
File metadata
- Download URL: pyentrypoint-0.7.0.tar.gz
- Upload date:
- Size: 18.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/2.7.16 Darwin/19.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d15b7ce1642159bd9aa05196232e6abb7e8800a0608af7b00204bd672b51e4c4
|
|
| MD5 |
86cf04355f7735624fbe46b0adfb466a
|
|
| BLAKE2b-256 |
7b8318c4f8a8c3d191b482eaefa9702fe2bdff8330ec7d06b82582138faa419b
|
File details
Details for the file pyentrypoint-0.7.0-py3-none-any.whl.
File metadata
- Download URL: pyentrypoint-0.7.0-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/2.7.16 Darwin/19.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a957667300c62c68480ed33c1cf0ed91a537d79fa09b80e0761d9780dc19b2d4
|
|
| MD5 |
218878f58d36ef553b1cb11044a17574
|
|
| BLAKE2b-256 |
720256c6c9f9335b13de0b46f9ab4f1dd4ccd42512c8e6de1520e460bad1f643
|