Last active
December 27, 2018 00:40
-
-
Save kendru/f45fcd13a8acee80ca7c1ada0829af3f to your computer and use it in GitHub Desktop.
Set up a throw-away mysql server in docker and a client to connect to it
This file contains hidden or 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
#!/bin/bash | |
cleanup_db() { | |
docker stop mysql_sandbox &>/dev/null | |
docker rm mysql_sandbox &>/dev/null | |
docker network inspect mysql_sandbox > /dev/null 2>&1 | |
if [[ "$?" == "0" ]]; then | |
docker network rm mysql_sandbox | |
fi | |
} | |
db_port="$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')" | |
echo "Spinning up database container on port ${db_port}" | |
cleanup_db | |
docker network create --driver bridge mysql_sandbox | |
docker run -d \ | |
--name=mysql_sandbox \ | |
--network mysql_sandbox \ | |
--health-cmd='mysqladmin ping --silent' \ | |
-e 'MYSQL_USER=test' \ | |
-e 'MYSQL_PASSWORD=s3cr3t' \ | |
-e 'MYSQL_DATABASE=test' \ | |
-e 'MYSQL_ALLOW_EMPTY_PASSWORD=true' \ | |
-p "${db_port}:3306" \ | |
mariadb:10.3 &>/dev/null | |
printf "Waiting for MySQL to be ready." | |
while [[ "$(docker inspect --format "{{json .State.Health.Status }}" mysql_sandbox)" != "\"healthy\"" ]]; do | |
printf "." | |
sleep 1 | |
done | |
printf "\n" | |
echo "MySQL server is running (mapped to host port ${db_port}). Starting client." | |
docker run -it \ | |
--rm \ | |
--name mysql_client \ | |
--network mysql_sandbox \ | |
mariadb:10.3 \ | |
mysql -h mysql_sandbox -u test -ps3cr3t test | |
cleanup_db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment