Last active
December 14, 2020 06:09
-
-
Save nickistre/ee03d715dd563071f8ae40d854d80bfd to your computer and use it in GitHub Desktop.
A couple of scripts to run Databases in local Docker containers for testing
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
#!/usr/bin/env bash | |
# From: https://stackoverflow.com/a/46625302 | |
function getContainerHealth { | |
docker inspect --format "{{json .State.Health.Status }}" $1 | |
} | |
function waitContainer { | |
while STATUS=$(getContainerHealth $1); [ $STATUS != "\"healthy\"" ]; do | |
if [ $STATUS == "\"unhealthy\"" ]; then | |
echo "Failed!" | |
exit -1 | |
fi | |
printf . | |
lf=$'\n' | |
sleep 1 | |
done | |
printf "$lf" | |
} | |
# SCRIPTPATH line from https://stackoverflow.com/a/4774063 | |
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" | |
DB_USER="user" | |
DB_PASSWORD="password" | |
DB_NAME="database" | |
DB_DOCKER_NAME="${DB_NAME}-mysql" | |
# Stop and remove existing database | |
docker stop $(docker ps -q -f name="${DB_DOCKER_NAME}") | |
docker rm $(docker ps -aq -f name="${DB_DOCKER_NAME}") | |
# start new mysql docker container | |
docker run \ | |
--name "${DB_DOCKER_NAME}" \ | |
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \ | |
-e MYSQL_DATABASE="${DB_NAME}" \ | |
-e MYSQL_USER="${DB_USER}" \ | |
-e MYSQL_PASSWORD="${DB_PASSWORD}" \ | |
-p 0.0.0.0:3306:3306 \ | |
-d \ | |
--health-cmd="mysqladmin ping --silent" \ | |
mysql | |
# Wait for mysql to be ready | |
waitContainer "${DOCKER_NAME}" | |
# Add code to initialize database here | |
# Recommended environment variables for mysql commands: | |
# MYSQL_HOST=127.0.0.1 <command> |
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
#!/usr/bin/env bash | |
# From: https://stackoverflow.com/a/46625302 | |
function getContainerHealth { | |
docker inspect --format "{{json .State.Health.Status }}" $1 | |
} | |
function waitContainer { | |
while STATUS=$(getContainerHealth $1); [ $STATUS != "\"healthy\"" ]; do | |
if [ $STATUS == "\"unhealthy\"" ]; then | |
echo "Failed!" | |
exit -1 | |
fi | |
printf . | |
lf=$'\n' | |
sleep 1 | |
done | |
printf "$lf" | |
} | |
# SCRIPTPATH line from https://stackoverflow.com/a/4774063 | |
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" | |
DB_USER="user" | |
DB_PASSWORD="password" | |
DB_NAME="database" | |
DB_DOCKER_NAME="${DB_NAME}-pg" | |
# Stop and remove existing database | |
docker stop $(docker ps -q -f name="${DB_DOCKER_NAME}") | |
docker rm $(docker ps -aq -f name="${DB_DOCKER_NAME}") | |
# Start new postgres docker container | |
docker run \ | |
--name "${DB_DOCKER_NAME}" \ | |
-e POSTGRES_USER="${DB_USER}" \ | |
-e POSTGRES_PASSWORD="${DB_PASSWORD}" \ | |
-p 0.0.0.0:5432:5432 \ | |
-d \ | |
--health-cmd="pg_isready -U ${DB_USER}"\ | |
postgres | |
# Wait for database to be ready | |
waitContainer "${DB_DOCKER_NAME}" | |
# Add code to initialize database here | |
# Recommended environment variables for postgres commands: | |
# PGHOST=127.0.0.1 PGUSER="${DB_USER}" PGPASSWORD="${DB_PASSWORD}" <command> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment