FG
☁️ Cloud & DevOpsDocker

Hooks/ Plugins support to run arbitrary scripts

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

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

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Custom Script Hooks for Docker Compose

Medium Risk

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. 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.

    bash
    mkdir .docker-compose-hooks
  2. 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.

    bash
    echo '#!/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. 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.

    yaml
    version: '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. 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.

    bash
    echo 'Running post-up script...'
    ./.docker-compose-hooks/post-up.sh
  5. 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.

    bash
    docker-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

AC

Alex Chen

2450 rep

Tags

dockerdocker-composecontainers