Event Hooks in Compose

It would be really nice to easily trigger commands to be run when services are created, started, stopped, scaled, and removed.

There is an ‘events’ stream we can watch that requires a lot of work by users and moves our configuration outside of the docker-compose.yml file, as well it requires I open another terminal and have a constantly running app to handle my compose environment events. The event implementation is simply not fit to implement lifecycle event handlers.

To lay down some concrete use cases.

  • I have a django environment, when ever I run docker-compose up and the container is first created I want to ensure the database exists and a superuser is created. When I restart or update the container, migrations need to be ran.

  • I have a clustered service, when I scale up the service I want to run a command on new instances and possibly existing instances to handle the scaling (add / removing members from the cluster, pre-warming caches, etc)

  • I’m shutting down a database service and as a pre-caution I want to do an explicit database export.

I’d like all of this to be in container ala docker exec or docker-compose run…

example of possible compose format…

services:
   django:
       events:
             create:
                 target: django  # service to run the command in.
                 single: true      # run in only a single container.
                 commands:     # series of commands to run (could be scripts in the container)
                     - ./code/venv/python ./code/manage.py migrate
              start:
                 commands:
                     - ./code/venv/python ./code/manage.py migrate
              stop:
              remove:
              add:      

obviously there would need to be a clearer syntax around the types of events (scaling up vs scaling down) creating first instance vs last instance. these could be passed as environment variables to commands or could be some sort of filter property or distinct events.

I’m not certain about the implementation, but this is definitely a feature that would make my life and the lives of my teams easier. I’m the primary person setting up our development and production environments across six projects and I work with compose as a user daily. If I could save developers the trouble of running the same commands on spinning up fresh environments, upon updating containers, or blowing away and rebuilding environments it would be good for our overall productivity.

I’m also the person who has the call on when we put docker in production. I’d really like these types of lifecycle hooks to help speed up deployments and minimize human error.

This has been requested numerous times in the issue queue, but the compose maintainers seem to disregard what their users are asking for, or make arguments like it’s too hard, they don’t want to support it, it’s not a real need, work around it, or managing container lifecycle isn’t a problem for compose.

It’s really disheartening to see such an disinterest in the needs of users, especially on issues that come up repeatedly.

2 Likes