Skip to content

Instantly share code, notes, and snippets.

@vincepare
Last active January 21, 2018 15:15
Show Gist options
  • Save vincepare/fb6aeb4f99ff19b474c00f4e61699113 to your computer and use it in GitHub Desktop.
Save vincepare/fb6aeb4f99ff19b474c00f4e61699113 to your computer and use it in GitHub Desktop.
Bash helper script to automate docker-compose.yml file detection
#!/usr/bin/env bash
#
# Use as a drop-in replacement for docker-compose that will automatically find your docker-compose.yml file.
# Author : Vincent Paré
function dcp {
local candidates=()
if [ -n "$DCP_COMPOSE" ]; then
candidates+=("$DCP_COMPOSE")
fi
if [ -f ".dcp" ]; then
candidates+=("$(cat .dcp)")
fi
candidates+=(
docker-compose.yml
docker-compose.yml.dist
docker*/docker-compose.yml
docker*/docker-compose.yml.dist
)
local candidate
for candidate in "${candidates[@]}"; do
if [ -f "$candidate" ]; then
local composeFile=$candidate
break
fi
done
if [ -z "$composeFile" ]; then
>&2 echo "Didn't find any docker compose file"
return 1
fi
>&2 echo "Using $(ls -1 "$composeFile")"
docker-compose -f "$composeFile" "$@"
}
dcp "$@"

Use this to avoid to explicitly provide the compose file location to docker-compose if you don't use the default (./docker-compose.yml). This tool search for a docker compose file at predefined locations, or using the DCP_COMPOSE or the .dcp file in the working directory. It can also be used as a lazy/short alias for docker-compose.

You can use it either as a function by putting it into ~/.bashrc, or as a shell script.

Install

curl -Lo /usr/local/bin/dcp https://gist.github.com/vincepare/fb6aeb4f99ff19b474c00f4e61699113/raw/dcp.sh && chmod +x /usr/local/bin/dcp

OR :

wget -O /usr/local/bin/dcp https://gist.github.com/vincepare/fb6aeb4f99ff19b474c00f4e61699113/raw/dcp.sh && chmod +x /usr/local/bin/dcp

Example

Start a docker app :

dcp up -d

Stop a docker app :

dcp stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment