Skip to content

Instantly share code, notes, and snippets.

@smitpatelx
Forked from ricog/Dockerfile
Created March 3, 2021 21:02
Show Gist options
  • Save smitpatelx/8f7f6524984fbbc4634f430c4bbe45e6 to your computer and use it in GitHub Desktop.
Save smitpatelx/8f7f6524984fbbc4634f430c4bbe45e6 to your computer and use it in GitHub Desktop.
Docker wait for mysql to be ready and then load sql

Run the container with links to mysql and the database file available through volumes or volumes_from.

FROM mysql:5.6
MAINTAINER Your Name <you@yourplace.com>
COPY wait-for-mysql.sh /
CMD /wait-for-mysql.sh
HOST="$MYSQL_PORT_3306_TCP_ADDR"
PORT="$MYSQL_PORT_3306_TCP_PORT"
USER="root"
PASSWORD="$MYSQL_ENV_MYSQL_ROOT_PASSWORD"
DATABASE="$MYSQL_ENV_MYSQL_DATABASE"
SQL_PATH="${SQL_PATH:-/var/www/Config/sql}"
echo $SQL_FILE;
SQL_FILE="${SQL_FILE:-database.sql}"
echo $SQL_FILE;
until echo '\q' | mysql -h"$HOST" -P"$PORT" -u"$USER" -p"$PASSWORD" $DATABASE; do
>&2 echo "MySQL is unavailable - sleeping"
sleep 1
done
while [ ! -d $SQL_PATH ]; do
>&2 echo "Data is unavailable - sleeping"
sleep 1;
done;
>&2 echo "MySQL and Data are up - executing command"
cat $SQL_PATH/$SQL_FILE | mysql -h"$HOST" -P"$PORT" -u"$USER" -p"$PASSWORD" $DATABASE
@alanhortz
Copy link

alanhortz commented Feb 28, 2022

Is there any issue with logging credentials in the command line ending in logs ? I mean is it possible to have the stdin and out recorded somewhere ?

MySql cli usually complaints if password is given in the command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment