Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Last active August 24, 2018 16:52
Show Gist options
  • Save mnapoli/05cfcbbf7607af91675ac8fea653b496 to your computer and use it in GitHub Desktop.
Save mnapoli/05cfcbbf7607af91675ac8fea653b496 to your computer and use it in GitHub Desktop.
Docker Compose shortcut
# Docker-Compose shortcut:
# - before: docker-compose exec app <command>
# - after: d <command>
# The alias will auto-start the service if it is stopped.
# Requirements:
# - jq: https://stedolan.github.io/jq/
# - yq: https://github.com/kislyuk/yq
# Add this to your `~/.zshrc` at the end because `d` is an existing (unrelated) alias we want to override
docker_compose_start_if_stopped () {
# Find the first service in `docker-compose.yaml`
export DC_MAIN_SERVICE=`yq '.services|keys[0]' docker-compose.yaml --raw-output`
# If it is stopped: start it
STATE=`docker-compose ps $DC_MAIN_SERVICE | tail -n 1 | awk '{print $5}'`
if [ "$STATE" != "Up" ]
then
docker-compose start $DC_MAIN_SERVICE
fi
}
alias d="docker_compose_start_if_stopped && docker-compose exec $DC_MAIN_SERVICE"
# Reload your terminal:
# source ~/.zshrc
# Usage:
# d <the-command-to-run>
# Example:
# d ls -la
# Commands will be run using `docker-compose exec` in the first service defined in `docker-compose.yaml`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment