From 554f50111394899cefcd080e4e03e85728d189ae Mon Sep 17 00:00:00 2001 From: KennyLee Date: Tue, 4 Jun 2019 13:51:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96springboot=E7=9A=84wait-for-m?= =?UTF-8?q?ysql=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dockerfile} | 5 ++- .../wait-for-mysql/wait-for-mysql.sh | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) rename springboot/image-build/{Dockerfile-alpine-mysql_client => wait-for-mysql/Dockerfile} (67%) create mode 100644 springboot/image-build/wait-for-mysql/wait-for-mysql.sh diff --git a/springboot/image-build/Dockerfile-alpine-mysql_client b/springboot/image-build/wait-for-mysql/Dockerfile similarity index 67% rename from springboot/image-build/Dockerfile-alpine-mysql_client rename to springboot/image-build/wait-for-mysql/Dockerfile index 050c29c..d42679f 100755 --- a/springboot/image-build/Dockerfile-alpine-mysql_client +++ b/springboot/image-build/wait-for-mysql/Dockerfile @@ -6,4 +6,7 @@ RUN apk --no-cache update && \ apk --no-cache add mysql-client && \ rm -fr /tmp/* /var/cache/apk/* -ENTRYPOINT "/entrypoint.sh" +COPY wait-for-mysql.sh /wait-for-mysql.sh +RUN chmod +x /wait-for-mysql.sh + +ENTRYPOINT ["/wait-for-mysql.sh"] diff --git a/springboot/image-build/wait-for-mysql/wait-for-mysql.sh b/springboot/image-build/wait-for-mysql/wait-for-mysql.sh new file mode 100644 index 0000000..b03c6c9 --- /dev/null +++ b/springboot/image-build/wait-for-mysql/wait-for-mysql.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# wait until MySQL is really available +max_counter=45 + +port=3306 +host=localhost +username=root +password= + +cmd="$@" + +if [[ -n "${MYSQL_PORT}" ]]; then + port="${MYSQL_PORT}" +fi +if [[ -n "${MYSQL_HOST}" ]]; then + host="${MYSQL_HOST}" +fi +if [[ -n "${MYSQL_USER}" ]]; then + username="${MYSQL_USER}" +fi +if [[ -n "${MYSQL_PASSWORD}" ]]; then + password="${MYSQL_PASSWORD}" +fi + +password_arg= +if [[ -n ${password} ]]; then + password_arg="-p${password}" +fi + +counter=1 +waiting_dot='.' +while ! mysql --protocol TCP -h"${host}" -P"${port}" -u"${username}" ${password_arg} -e "show databases;" > /dev/null 2>&1; do + sleep 1 + ehco "${waiting_dot}" + waiting_dot+='.' + counter=`expr ${counter} + 1` + if [[ ${counter} -gt ${max_counter} ]]; then + >&2 echo "We have been waiting for MySQL too long already; failing." + exit 1 + fi; +done + +echo "MySQL is up - executing command" +exec ${cmd} +