Skip to content

Instantly share code, notes, and snippets.

@titpetric
Created April 30, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titpetric/c5e4bf5cfe66161f0b68512bb3eda218 to your computer and use it in GitHub Desktop.
Save titpetric/c5e4bf5cfe66161f0b68512bb3eda218 to your computer and use it in GitHub Desktop.
MySQL docker testing bash script to spin up mysql container
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 [start/stop]"
exit 1
fi
function setup_database {
NAME="$1"
DOCKERFILE="titpetric/percona-xtrabackup"
docker run -d -h $NAME --name $NAME --net=party -e MYSQL_ROOT_PASSWORD="testing" $DOCKERFILE
echo -n "Waiting for mysql to become ready: "
while true; do
READY=$(docker logs $NAME 2>&1 | grep "port: 3306" | wc -l)
if [ "$READY" == "2" ]; then
sleep 1
echo " Done."
break
fi
echo -n "."
sleep 1
done
}
ACTION="$1"
CWD=$(dirname $(realpath -s $0))
NAME=""
KEY="$(<$(realpath -s $CWD/../../)/.project)"
if [ -z "$KEY" ]; then
echo "No .project key in $(realpath -s $0)"
exit 1
fi
NAME="$KEY-mysql"
case "$ACTION" in
start)
setup_database $NAME
exit 0
;;
stop)
docker rm -f $NAME
exit 0
;;
*)
echo "Unknown action, allowed start/stop"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment