Skip to content

Instantly share code, notes, and snippets.

@iAnanich
Last active April 2, 2023 08:30
Show Gist options
  • Save iAnanich/2b94fdd9de8dc34f1eb6bf9def06e3d5 to your computer and use it in GitHub Desktop.
Save iAnanich/2b94fdd9de8dc34f1eb6bf9def06e3d5 to your computer and use it in GitHub Desktop.
Shell script that helps to simplify Docker Compose usage
#!/bin/bash
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
### Generated with the help of ChatGPT ###
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
# Usage:
# ./do.sh dc ps
# ./do.sh dc down
# ./do.sh dc up -d service
# ./do.sh dc logs -f service
# ./do.sh update
# ./do.sh update service
# ./do.sh restart
# ./do.sh
### Set environment variables from .env file
### ----------------------------------------
set -o allexport
source .env
set +o allexport
### Defaults
### --------
_DOCKER_COMPOSE_PROJECT_NAME=${_DOCKER_COMPOSE_PROJECT_NAME:-$(basename "$(pwd)")}
_DOCKER_COMPOSE_FILE=${_DOCKER_COMPOSE_FILE:-docker-compose.yml}
# Docker Context is not yet widely supported. It is used with `-c` option for Docker Compose commands.
# _DOCKER_CONTEXT=${_DOCKER_CONTEXT:-default}
### Get the Docker Compose binary name
### ----------------------------------
# Docker is now switching to making Compose a plugin for Docker Engine,
# there for it's call signature is no longer using dash
if docker compose version > /dev/null 2>&1; then
# Use the plugin for the Docker CLI if available
_DOCKER_COMPOSE_COMMAND="docker compose"
elif command -v docker-compose > /dev/null 2>&1; then
# Use the standalone binary if available
_DOCKER_COMPOSE_COMMAND="docker-compose"
else
echo "Error: docker-compose or docker CLI with compose plugin is not installed"
exit 1
fi
### Utility functions
### -----------------
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
### ==================
### Define functions
### ------------------
function dc {
echo "Executing Docker Compose command: ${_DOCKER_COMPOSE_COMMAND} -p ${_DOCKER_COMPOSE_PROJECT_NAME} -f ${_DOCKER_COMPOSE_FILE}" "$@"
${_DOCKER_COMPOSE_COMMAND} -p ${_DOCKER_COMPOSE_PROJECT_NAME} -f ${_DOCKER_COMPOSE_FILE} "$@"
}
function restart {
echo "Restart:" "$@"
try dc restart "$@"
dc logs -f
}
function update {
echo "Update:" "$@"
try dc build "$@"
try dc up -d --force-recreate "$@"
dc logs -f
}
# Add new functions here:
### -----------------------------
### end of functions definition
### =============================
### Handle command (function) execution
### -----------------------------------
# Generate the command map dynamically based on defined functions
function generate_command_map {
declare -a map
for func in $(declare -F | awk '{print $3}'); do
if [[ "$func" != "generate_command_map" ]]; then
map+=("$func")
fi
done
echo "${map[*]}"
}
# Define the command mapping
command_map=($(generate_command_map))
# Parse command line arguments and call the appropriate function
if [[ " ${command_map[*]} " == *" $1 "* ]]; then
# Call the corresponding function
"${1}" "${@:2}"
else
echo "Invalid command. Usage: ./do.sh [${command_map[*]}]. Got" "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment