Hooks/ Plugins support to run arbitrary scripts
Problem
It would be nice if `docker-compose up|down|..` would look for custom scripts in the current directory, like: [code block] I guess this is mostly helpful when docker-compose is used to run the development environment. These scripts should not be meant to configure containers. My current use-cases include: - set up hostnames in `/etc/hosts` after container startup - run docker-rsync after container started Eventually docker-compose should allow these scripts to run in the background and stream stdout/stderr to `docker-compose logs`. Related https://github.com/docker/compose/pull/74 https://github.com/docker/compose/issues/57
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Custom Script Hooks for Docker Compose
Docker Compose does not currently support executing arbitrary scripts during lifecycle events (up/down) which limits its flexibility in development environments. This lack of support prevents users from automating tasks such as modifying host configurations or syncing files post container startup.
Awaiting Verification
Be the first to verify this fix
- 1
Create Hook Script Directory
Create a directory named '.docker-compose-hooks' in your project root. This directory will contain your custom scripts for various Docker Compose lifecycle events.
bashmkdir .docker-compose-hooks - 2
Add Custom Scripts
Add your custom scripts to the '.docker-compose-hooks' directory. For example, create a script named 'post-up.sh' to run after containers are started.
bashecho '#!/bin/bash echo "Setting up hostnames..." sudo bash -c "echo '127.0.0.1 myapp.local' >> /etc/hosts"' > .docker-compose-hooks/post-up.sh chmod +x .docker-compose-hooks/post-up.sh - 3
Modify Docker Compose Configuration
Update your Docker Compose file to include a command that looks for and executes scripts from the '.docker-compose-hooks' directory. This can be done by using a wrapper script or modifying the entrypoint.
yamlversion: '3' services: app: image: your-image entrypoint: ["/bin/bash", "-c", "if [ -d .docker-compose-hooks ]; then for script in .docker-compose-hooks/*; do $script; done; fi; exec your-original-command"] - 4
Stream Logs from Custom Scripts
Ensure that your scripts output logs to stdout/stderr so that they can be captured by 'docker-compose logs'. Modify your scripts to include 'echo' statements or redirect output appropriately.
bashecho 'Running post-up script...' ./.docker-compose-hooks/post-up.sh - 5
Test the Implementation
Run 'docker-compose up' and verify that your custom scripts execute as expected. Check the logs to confirm that the output from your scripts is visible.
bashdocker-compose up
Validation
To confirm that the fix worked, run 'docker-compose up' and check the console output for messages from your custom scripts. Additionally, verify that the intended changes (like hostname updates) have been applied successfully.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep