Skip to main content

objective

  • have a CLI to start a process which runs abitrary code on docker events

  • be able to provide a way to change behavior of and provide data to event subscribers

  • load subscribers by module or file

  • events may be extended

install

# pip install docker-events

usage

 # docker-events --help
 Usage: docker-events [OPTIONS]

 Options:
   -s, --sock TEXT        the docker socket
   -c, --config FILENAME  a config yaml
   -m, --module TEXT      a python module to load
   -f, --file PATH        a python file to load
   -l, --log PATH         logging config
   --debug                enable debug log level
   --help                 Show this message and exit.

 # create a config for skydns
 # cat > skydns-config.yaml << EOF
 ---

 skydns:
   domain: foo

 skydns.containers:
   redis:
     domain: myredis.{domain}
 EOF

# run skydns subscriber on start events
# docker-events -c skydns-config.yaml -m docker_events.tools.skydns

API

There are some predefined events: start, stop, create, die, destroy, pull

You may write your own events like this:

@event
def pull(client, event_data):
    return event_data.get('status') == 'pull'

Events may be extended by other events:

@docker_events.pull.extend
def pull_something(client, event_data):
    return event_data['id'].startswith("foobar")

For example if you want to use skydns, you may want to put the following code into a file or module and load it via -m or -f option:

"""
setup skydns records for containers
"""

import docker_events
import etcd
import simplejson as json


etcd_client = etcd.Client()


@docker_events.start.subscribe
def set_skydns_record(client, docker_event, config):
    # get ip of container
    container = client.inspect_container(docker_event['id'])

    container_name = container['Name'].strip("/")
    container_ip = container['NetworkSettings']['IPAddress']

    skydns_config = config.get('skydns', {})
    skydns_containers = config.get('skydns.containers', {
        'domain': 'docker.local'
    })

    # find domain name for this container
    if skydns_config and container_name in skydns_containers:
        # use template
        domain = skydns_containers[container_name].get('domain').format(**skydns_config)

    else:
        # join container_name with domain
        domain = '.'.join((container_name, skydns_config.get('domain')))

    domain_path = '/'.join(reversed(domain.split('.')))


    etcd_client.write('/skydns/local/skydns/{}'.format(domain_path), json.dumps({
        'host': container_ip
    }))

Metadata

Release files for docker-events 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for docker-events 0.0.10
File Size Uploaded
docker-events-0.0.10.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for docker-events 0.0.10
File Interpreter ABI Platform
docker_events-0.0.10-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 13.8 kB

Release files / docker-events-0.0.10.tar.gz

Download URL docker-events-0.0.10.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cfb66daa5e7e9d9affac05edb6464657798ad73d9acd92d3e1a894c6aa6aac31
BLAKE2b-256 checksum
How to use checksums
be7a1f9a01569c56dd33c2574c99a4b7fcdbc665d1d951f35e7f82bc99853b2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / docker_events-0.0.10-py2.py3-none-any.whl

Download URL docker_events-0.0.10-py2.py3-none-any.whl
Size 8.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
c4b8b2cc6c602404f6d8c16d0f2a77145b9e108fe7b65448c2e593f86f2251e4
BLAKE2b-256 checksum
How to use checksums
c4a06ed0d8020fdf57ce02f2077a13e8d5c0fd2b3b1cbeaed5184a7f8fdb8305
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page