diff --git a/Dockerfile b/Dockerfile index d2f7312..8f9ba8d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,97 +1,108 @@ ################################################################################ # # Copyright (c) 2017-2018 # Data Intensive Applications and Systems Labaratory (DIAS) # Ecole Polytechnique Federale de Lausanne # # All Rights Reserved. # # Permission to use, copy, modify and distribute this software and its # documentation is hereby granted, provided that both the copyright notice # and this permission notice appear in all copies of the software, derivative # works or modified versions, and any portions thereof, and that both notices # appear in supporting documentation. # # This code is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. THE AUTHORS AND ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE # DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM # THE USE OF THIS SOFTWARE. # ################################################################################ FROM openjdk:jre-alpine MAINTAINER Darius Sidlauskas RUN apk update && \ apk add tar bash ENV LANG=C.UTF-8 ENV SOLR_USER="solr" \ SOLR_UID="8983" \ SOLR_GROUP="solr" \ SOLR_GID="8983" \ SOLR_VERSION="8.0.0" \ PATH="/opt/hbp-lucene-solr/bin:/opt/hbp-lucene-solr-docker/scripts:$PATH" RUN addgroup -S -g $SOLR_GID $SOLR_GROUP && \ adduser -S -u $SOLR_UID -G $SOLR_GROUP $SOLR_USER COPY src/hbp-lucene-solr/solr/package/solr-8.0.0-SNAPSHOT.tgz /opt/hbp-lucene-solr.tgz RUN mkdir -p /opt/hbp-lucene-solr && \ tar -C /opt/hbp-lucene-solr --extract --file /opt/hbp-lucene-solr.tgz --strip-components=1 && \ rm /opt/hbp-lucene-solr.tgz* && \ rm -Rf /opt/hbp-lucene-solr/docs/ RUN chown -R $SOLR_USER:$SOLR_GROUP /opt/hbp-lucene-solr COPY scripts /opt/hbp-lucene-solr-docker/scripts RUN chown -R $SOLR_USER:$SOLR_GROUP /opt/hbp-lucene-solr-docker #RUN cd /opt/hbp-lucene-solr && ant ivy-bootstrap && ant compile #RUN cd solr && ant server && ant dist ################################################################################ # FROM openjdk:jre-alpine # Note: All environment variables are resetted by the FROM: -ENV SOLR_HOME=/opt/hbp-lucene-solr +ENV SOLR_HOME="/opt/hbp-lucene-solr/server/solr" ARG BUILD_DATE ARG VCS_REF LABEL org.label-schema.build-date=$BUILD_DATE \ org.label-schema.name="lucene-solr" \ org.label-schema.description="Docker image for running hbp-lucene-solr" \ org.label-schema.url="https://bitbucket.org/sakurad/hbp-lucene-solr-docker" \ org.label-schema.vcs-type="git" \ org.label-schema.vcs-ref=$VCS_REF \ org.label-schema.vcs-url="https://bitbucket.org/sakurad/hbp-lucene-solr-docker" \ org.label-schema.vendor="DIAS EPFL" \ org.label-schema.docker.dockerfile="Dockerfile" \ org.label-schema.schema-version="0.8" ################################################################################ -WORKDIR $SOLR_HOME +WORKDIR /opt/hbp-lucene-solr USER $SOLR_USER EXPOSE 8983 # Both the ENTRYPOINT and CMD instructions support two different forms: the # shell form and the exec form. When using the shell form, the specified binary # is executed with an invocation of the shell using `/bin/sh -c`. # When the exec form of the ENTRYPOINT/CMD instruction is used the command will # be executed without a shell. # # Therefore, it is always recommended to use the *exec* form of the # ENTRYPOINT/CMD instructions which looks like this: # CMD ["executable","param1","param2"] # instead of *shell* form, which looks like this: # CMD executable param1 param2 -CMD ["solr", "start", "-p", "8983", "-f", "-s", "server/solr" ] - +# +# The next CMD alone worked fine for running a single Solr instance within a +# docker: +#CMD ["solr", "start", "-p", "8983", "-f", "-s", "server/solr" ] +# +# The docker image can be run then using: +# `docker run --rm --name my-hbp-solr -d -p 8983:8983 -t hbp-lucene-solr` +# +# +# However, to have more flexibility and following existing conventions, it was +# replaced by the bellow combination of ENTRYPOINT and CMD instructions. +# # When both an ENTRYPOINT and CMD are specified, the CMD string(s) will be # appended to the ENTRYPOINT in order to generate the container's command # string. Remember that the CMD value can be easily overridden by supplying one # or more arguments to `docker run` after the name of the image. -# ENTRYPOINT ["docker-entrypoint.sh"] -# CMD ["solr-foreground"] + +ENTRYPOINT ["docker-entrypoint.sh"] +CMD ["hbp-solr-foreground"] diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh index 6fd0ec2..0efb866 100755 --- a/scripts/docker-entrypoint.sh +++ b/scripts/docker-entrypoint.sh @@ -1,24 +1,33 @@ #!/bin/bash # # docker-entrypoint for hbp-solr-lucene-docker set -e +# From bash set manual: +# -e Exit immediately if a simple command exits with a non-zero status, unless +# the command that fails is part of an until or while loop, part of an if +# statement, part of a && or || list, or if the command's return status is +# being inverted using !. -o errexit if [[ "$VERBOSE" = "yes" ]]; then set -x fi -# when invoked with e.g.: docker run solr -help +# when invoked with e.g.: docker run hbp-lucene-solr -help if [ "${1:0:1}" = '-' ]; then - set -- solr-foreground "$@" + set -- hbp-solr-foreground "$@" +# From bash set manual: +# -- If no arguments follow this option, then the positional parameters are unset. +# Otherwise, the positional parameters are set to the arguments, even if +# some of them begin with a `-'. fi # execute command passed in as arguments. # # The Dockerfile has specified the PATH to include # /opt/hbp-lucene-solr/bin (for Solr) and # /opt/hbp-solr-lucene-docker/scripts (for our scripts like # solr-foreground, solr-create, solr-precreate, solr-demo). Note: if # you specify "solr", you'll typically want to add -f to run it in the # foreground. exec "$@" diff --git a/scripts/hbp-solr-foreground b/scripts/hbp-solr-foreground new file mode 100755 index 0000000..ddd12b5 --- /dev/null +++ b/scripts/hbp-solr-foreground @@ -0,0 +1,14 @@ +#!/bin/bash +# +# Run the initdb, then start solr in the foreground +set -e + +if [[ "$VERBOSE" = "yes" ]]; then + set -x + fi + +. /opt/hbp-lucene-solr-docker/scripts/run-initdb + +echo "Starting HBP Spatial Search API (using Solr-$SOLR_VERSION)" + +exec solr -f "$@" diff --git a/scripts/init-hbp-solr-home b/scripts/init-hbp-solr-home new file mode 100755 index 0000000..22c0803 --- /dev/null +++ b/scripts/init-hbp-solr-home @@ -0,0 +1,50 @@ +#!/bin/bash +# +# A helper script to initialise a custom SOLR_HOME. +# For example: +# +# mkdir mysolrhome +# sudo chown 8983:8983 mysolrhome +# docker run -it -v $PWD/mysolrhome:/mysolrhome -e SOLR_HOME=/mysolrhome -e INIT_SOLR_HOME=yes hbp-solr +# + +set -e + +if [[ "$VERBOSE" = "yes" ]]; then + set -x +fi + +# Normally SOLR_HOME is not set, and Solr will use /opt/solr/server/solr/ +# If it's not set, then this script has no relevance. +if [[ -z $SOLR_HOME ]]; then + exit +fi + +# require an explicit opt-in. Without this, you can use the SOLR_HOME and +# volumes, and configure it from the command-line or a docker-entrypoint-initdb.d +# script +if [[ "$INIT_SOLR_HOME" != "yes" ]]; then + exit +fi + +# check the directory exists. +if [ ! -d "$SOLR_HOME" ]; then + echo "SOLR_HOME $SOLR_HOME does not exist" + exit 1 +fi + +# check for existing Solr +if [ -f "$SOLR_HOME/solr.xml" ]; then + exit +fi + +# refuse to use non-empty directories, which are likely a misconfiguration +if [ "$(find "$SOLR_HOME" -mindepth 1 ! -name lost+found | wc -l)" != '0' ]; then + echo "SOLR_HOME directory $SOLR_HOME is not empty; refusing to create a solr home." + exit 1 +fi + +# populate with default solr home contents +echo "copying solr home contents to $SOLR_HOME" +cp -R /opt/solr/server/solr/* "$SOLR_HOME" + diff --git a/scripts/run-initdb b/scripts/run-initdb new file mode 100755 index 0000000..072fd12 --- /dev/null +++ b/scripts/run-initdb @@ -0,0 +1,36 @@ +#!/bin/bash +# +# Run the init-hbp-solr-home script and source any '.sh' scripts that are found +# in `/docker-entrypoint-initdb.d`. Files there will be executed in alphabetical +# order. +# +# This script (run-initdb) is sourced by some of the solr-* commands, so that +# I can run eg: +# +# mkdir initdb; echo "echo hi" > initdb/hi.sh +# docker run -v $PWD/initdb:/docker-entrypoint-initdb.d hbp-lucene-solr +# +# and have your script execute before hbp-lucene-solr instance starts. +# +# Note: scripts can modify the environment, which will affect subsequent scripts +# and ultimately Solr. That allows you to set environment variables from your +# scripts (though you usually just use "docker run -e"). If this is undesirable +# in your use-case, have your scripts execute a sub-shell. + +set -e + +# init script for handling a custom SOLR_HOME +/opt/hbp-lucene-solr-docker/scripts/init-hbp-solr-home + +# execute files in /docker-entrypoint-initdb.d before starting hbp-lucene-solr +SCRIPTS_TO_RUN=/tmp/init-scripts-to-run +find /docker-entrypoint-initdb.d/ -mindepth 1 -type f | sort -n > "$SCRIPTS_TO_RUN" +readarray -t scripts_to_run < "$SCRIPTS_TO_RUN" +rm "$SCRIPTS_TO_RUN" +for f in "${scripts_to_run[@]}"; do + case "$f" in + *.sh) echo "$0: running $f"; . "$f" ;; + *) echo "$0: ignoring $f" ;; + esac + echo +done diff --git a/scripts/solr-foreground b/scripts/solr-foreground deleted file mode 100755 index 27c99bd..0000000 --- a/scripts/solr-foreground +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -# -# Run the initdb, then start solr in the foreground -set -e - -if [[ "$VERBOSE" = "yes" ]]; then - set -x - fi - -# . /opt/docker-solr/scripts/run-initdb - -echo "Starting Solr $SOLR_VERSION" - -exec solr -f "$@" \ No newline at end of file