Skip to content

Instantly share code, notes, and snippets.

@rafael-neri
Last active December 29, 2022 18:21
Show Gist options
  • Save rafael-neri/ba566e9c981779cce0b09ac694299bb7 to your computer and use it in GitHub Desktop.
Save rafael-neri/ba566e9c981779cce0b09ac694299bb7 to your computer and use it in GitHub Desktop.
Atomate run docker-composer in specific folder
# if using bash
echo 'export WORKRDIR="$HOME/my/workdir"' >> ~/.bashrc
# if using zsh
echo 'export WORKRDIR="$HOME/my/workdir"' >> ~/.zshrc
# download script
sudo curl https://bit.ly/3G9oEZk --output /bin/script
# permission
sudo chmod +x /bin/script
# restart terminal or:
# - source ~/.bashrc
# - source ~/.zshrc
# execute
script up
script down
#!/usr/bin/env bash
ARG=$1
if [ -z "$ARG" ] || [ "$ARG" = "--help" ] || [[ "$ARG" =~ [^(up|down)] ]]; then
echo "Missing argument to execute."
echo
echo "Usage: $(echo $0 | rev | cut -d '/' -f1 | rev) [operation]"
echo
echo " Where: "
echo " up: to up docker"
echo " down: to down docker"
exit 0
fi
clear
COLOR_GREEN='\033[0;32m'
COLOR_NONE="\033[0m"
echo "Checking if docker is running... "
if (! docker stats --no-stream > /dev/null 2>&1); then
sleep 1
clear
echo "Starting docker....... "
sudo service docker start > /dev/null 2>&1
sleep 1
fi
clear
echo -e "${COLOR_GREEN}Docker started.${COLOR_NONE}"
sleep 1
clear
declare -A projects
projects[0]="All"
echo "Projects:"
echo
echo "0: All"
key=1
for dir in `find $WORKRDIR/ -mindepth 1 -maxdepth 1 -type d | sort | rev | cut -d "/" -f1 | rev`
do
if [ -f "$WORKRDIR/$dir/docker-compose.yml" ]; then
echo $key: $dir
projects[$key]=$dir
key=`expr $key + 1`
fi
done
echo
echo "Example: 1,3,7"
echo
read -p "Choose project(s): " PROJ
echo
if [ $PROJ = "0" ]; then
unset projects[0]
for dir in ${projects[@]}
do
if [ "$ARG" = "up" ]; then
echo "Starting project $dir...."
docker-compose --log-level ERROR -f $WORKRDIR/$dir/docker-compose.yml up -d
else
echo "Stopping project $dir...."
docker-compose --log-level ERROR -f $WORKRDIR/$dir/docker-compose.yml down
fi
echo
done
else
nums=`echo -n "$PROJ" | sed "s/,/ /g"`
for i in $nums; do
if [[ "$i" == ?(-)+([0-9]) ]]; then
if [ $i -ge 0 ] && [ $i -lt $key ]; then
if [ "$ARG" = "up" ]; then
echo "Starting project ${projects[$i]}...."
docker-compose --log-level ERROR -f $WORKRDIR/${projects[$i]}/docker-compose.yml up -d
else
echo "Stopping project ${projects[$i]}...."
docker-compose --log-level ERROR -f $WORKRDIR/${projects[$i]}/docker-compose.yml down
fi
else
echo "Não é possivel subir: $i"
fi
else
echo "Não é possivel subira: $i"
fi
echo
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment