Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #420, entrypoint has a race condition where installation fails if DB is not ready in time. #1435

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Dockerfile-alpine.template
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,9 @@ RUN set -ex; \
COPY *.sh upgrade.exclude /
COPY config/* /usr/src/nextcloud/config/

# wait-for-it.sh is used for waiting on networked DB's to become available during install
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855a4dc21156cfe1694c31d7d1792e/wait-for-it.sh /usr/bin/wait-for-it
RUN chmod +x /usr/bin/wait-for-it

ENTRYPOINT ["/entrypoint.sh"]
CMD ["%%CMD%%"]
4 changes: 4 additions & 0 deletions Dockerfile-debian.template
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,9 @@ RUN set -ex; \
COPY *.sh upgrade.exclude /
COPY config/* /usr/src/nextcloud/config/

# wait-for-it.sh is used for waiting on networked DB's to become available during install
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855a4dc21156cfe1694c31d7d1792e/wait-for-it.sh /usr/bin/wait-for-it
RUN chmod +x /usr/bin/wait-for-it

ENTRYPOINT ["/entrypoint.sh"]
CMD ["%%CMD%%"]
26 changes: 26 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ run_as() {
fi
}

#Test for a port in host string, set default port if none provided
get_host_string() {
local host_string="$1"
local default_port="$2"
local host=""
local port=""

case "$host_string" in
(*:*) host="$host_string";;
(*) host="${host_string}:${default_port}";;
esac

echo "$host"
}

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
Expand Down Expand Up @@ -134,22 +149,33 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP
# shellcheck disable=SC2016
install_options=$install_options' --database-name "$SQLITE_DATABASE"'
install=true
network_db=false
elif [ -n "${MYSQL_DATABASE+x}" ] && [ -n "${MYSQL_USER+x}" ] && [ -n "${MYSQL_PASSWORD+x}" ] && [ -n "${MYSQL_HOST+x}" ]; then
echo "Installing with MySQL database"
# shellcheck disable=SC2016
install_options=$install_options' --database mysql --database-name "$MYSQL_DATABASE" --database-user "$MYSQL_USER" --database-pass "$MYSQL_PASSWORD" --database-host "$MYSQL_HOST"'
install=true
network_db=true
db_host=$(get_host_string $MYSQL_HOST 3306) #Add default port for MySQL to host string if no port is provided
elif [ -n "${POSTGRES_DB+x}" ] && [ -n "${POSTGRES_USER+x}" ] && [ -n "${POSTGRES_PASSWORD+x}" ] && [ -n "${POSTGRES_HOST+x}" ]; then
echo "Installing with PostgreSQL database"
# shellcheck disable=SC2016
install_options=$install_options' --database pgsql --database-name "$POSTGRES_DB" --database-user "$POSTGRES_USER" --database-pass "$POSTGRES_PASSWORD" --database-host "$POSTGRES_HOST"'
install=true
network_db=true
db_host=$(get_host_string $POSTGRES_HOST 5432) #Add default port for Postgres to host string if no port is provided
fi

if [ "$install" = true ]; then
echo "starting nextcloud installation"
max_retries=10
try=0

if [ "$network_db" = true ]; then
#Wait indefinitely for DB to become available by default, otherwise wait seconds defined by $WAIT_TIMEOUT
wait-for-it -t ${WAIT_TIMEOUT:-0} $db_host -- echo "Database service online"
fi

until run_as "php /var/www/html/occ maintenance:install $install_options" || [ "$try" -gt "$max_retries" ]
do
echo "retrying install..."
Expand Down