Last active
August 24, 2018 16:52
-
-
Save mnapoli/05cfcbbf7607af91675ac8fea653b496 to your computer and use it in GitHub Desktop.
Docker Compose shortcut
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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