优化springboot的wait-for-mysql镜像

This commit is contained in:
KennyLee 2019-06-04 13:51:54 +08:00
parent 56fa85728e
commit 554f501113
2 changed files with 49 additions and 1 deletions

View File

@ -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"]

View File

@ -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}