Skip to content

Instantly share code, notes, and snippets.

@ricog
Created November 8, 2016 15:54
Show Gist options
  • Save ricog/9e745cd79824bc360df14bdf95bb1f27 to your computer and use it in GitHub Desktop.
Save ricog/9e745cd79824bc360df14bdf95bb1f27 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 <[email protected]>
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
@koutsenko
Copy link

What is echo '\q' part doing? Why not just mysql -h ... in until statement?

@mzalazar
Copy link

What is echo '\q' part doing? Why not just mysql -h ... in until statement?

QUIT from MySQL CLI

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