1. solr官网:http://www.apache.org/dyn/closer.lua/lucene/solr/8.0.0
    DockerHub地址:https://hub.docker.com/_/solr/
  2. 下载alpine的java镜像做为基础镜像
    docker pull openjdk:8-jre-alpine
    docker安装solr
  3. 编写Dockerfile文件
FROM openjdk:8-jre-alpine

MAINTAINER menard 2019-03-25


ARG SOLR_DOWNLOAD_SERVER

RUN apk add --no-cache \
        lsof \
        gnupg \
        procps \
        tar \
        bash
RUN apk add --no-cache ca-certificates wget && \
        update-ca-certificates

ENV SOLR_USER="solr" \
    SOLR_UID="1001" \
    SOLR_GROUP="solr" \
    SOLR_GID="1001" \
    SOLR_VERSION="7.5.0" \
    SOLR_URL="${SOLR_DOWNLOAD_SERVER:-https://archive.apache.org/dist/lucene/solr}/7.5.0/solr-7.5.0.tgz" \
    SOLR_SHA256="eac2daffc376dd8057ee831fbfc4a1b8ee236b8ad94122e11d67fd2b242acebc" \
    SOLR_KEYS="052C5B48A480B9CEA9E218A5F98C13CFA5A135D8" \
    PATH="/opt/solr/bin:/opt/docker-solr/scripts:$PATH"

ENV GOSU_VERSION 1.11
ENV GOSU_KEY B42F6819007F00F88E364FD4036A9C25BF357DD4

RUN addgroup -S -g $SOLR_GID $SOLR_GROUP && \
    adduser -S -u $SOLR_UID -G $SOLR_GROUP $SOLR_USER

RUN set -e; \
  export GNUPGHOME="/tmp/gnupg_home" && \
  mkdir -p "$GNUPGHOME" && \
  chmod 700 "$GNUPGHOME" && \
  echo "disable-ipv6" >> "$GNUPGHOME/dirmngr.conf" && \
  for key in $SOLR_KEYS $GOSU_KEY; do \
    found=''; \
    for server in \
      ha.pool.sks-keyservers.net \
      hkp://keyserver.ubuntu.com:80 \
      hkp://p80.pool.sks-keyservers.net:80 \
      pgp.mit.edu \
    ; do \
      echo "  trying $server for $key"; \
      gpg --batch --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$key" && found=yes && break; \
      gpg --batch --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$key" && found=yes && break; \
    done; \
    test -z "$found" && echo >&2 "error: failed to fetch $key from several disparate servers -- network issues?" && exit 1; \
  done; \
  exit 0

RUN set -e; \
  export GNUPGHOME="/tmp/gnupg_home" && \
  apkArch="$(apk --print-arch | sed 's/x86_64/amd64/' | sed 's/aarch64/arm64/')"; \
  wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$apkArch"; \
  wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$apkArch.asc"; \
  gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu && \
  rm /usr/local/bin/gosu.asc && \
  chmod +x /usr/local/bin/gosu && \
  gosu nobody true && \
  mkdir -p /opt/solr && \
  echo "downloading $SOLR_URL" && \
  wget -q $SOLR_URL -O /opt/solr.tgz && \
  echo "downloading $SOLR_URL.asc" && \
  wget -q $SOLR_URL.asc -O /opt/solr.tgz.asc && \
  echo "$SOLR_SHA256 */opt/solr.tgz" | sha256sum -c - && \
  (>&2 ls -l /opt/solr.tgz /opt/solr.tgz.asc) && \
  gpg --batch --verify /opt/solr.tgz.asc /opt/solr.tgz && \
  tar -C /opt/solr --extract --file /opt/solr.tgz --strip-components=1 && \
  rm /opt/solr.tgz* && \
  rm -Rf /opt/solr/docs/ && \
  mkdir -p /opt/solr/server/solr/lib /opt/solr/server/solr/mycores /opt/solr/server/logs /docker-entrypoint-initdb.d /opt/docker-solr /opt/mysolrhome && \
  sed -i -e 's/"\$(whoami)" == "root"/$(id -u) == 0/' /opt/solr/bin/solr && \
  sed -i -e 's/lsof -PniTCP:/lsof -t -PniTCP:/' /opt/solr/bin/solr && \
  sed -i -e '/-Dsolr.clustering.enabled=true/ a SOLR_OPTS="$SOLR_OPTS -Dsun.net.inetaddr.ttl=60 -Dsun.net.inetaddr.negative.ttl=60"' /opt/solr/bin/solr.in.sh && \
  chown -R $SOLR_USER:$SOLR_GROUP /opt/solr /opt/mysolrhome /docker-entrypoint-initdb.d /opt/docker-solr

COPY --chown=solr:solr scripts /opt/docker-solr/scripts

EXPOSE 8983
WORKDIR /opt/solr
USER $SOLR_USER

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["solr-foreground"]

  1. 创建scripts目录,并进入这个目录。
    mkdir scripts
    cd scripts
  2. 编写docker-entrypoint.sh文件。
#!/bin/bash
#
# docker-entrypoint for docker-solr

set -e

if [[ "$VERBOSE" = "yes" ]]; then
    set -x
fi

if [[ -v SOLR_PORT ]] && ! grep -E -q '^[0-9]+$' <<<"${SOLR_PORT:-}"; then
  SOLR_PORT=8983
  export SOLR_PORT
fi

# when invoked with e.g.: docker run solr -help
if [ "${1:0:1}" = '-' ]; then
    set -- solr-foreground "[email protected]"
fi

# execute command passed in as arguments.
# The Dockerfile has specified the PATH to include
# /opt/solr/bin (for Solr) and /opt/docker-solr/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 "[email protected]"

  1. 编写init-solr-home文件
#!/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 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

  1. 编写precreate-core文件
#!/bin/bash
#
# Create a core on disk
# arguments are: corename configdir

set -e

echo "Executing $0" "[email protected]"

if [[ "${VERBOSE:-}" = "yes" ]]; then
    set -x
fi

CORE=${1:-gettingstarted}
CONFIG_SOURCE="${2:-}"
if [[ -z "$CONFIG_SOURCE" ]]; then
    DEFAULT_CONFIGS=(_default data_driven_schema_configs)
    for config_dir in "${DEFAULT_CONFIGS[@]}"; do
        config_dir="/opt/solr/server/solr/configsets/$config_dir"
        if [ -d "$config_dir" ]; then
           CONFIG_SOURCE="$config_dir"
           break
        fi
    done
    if [[ -z $CONFIG_SOURCE ]]; then
        echo "Cannot find default config"
        exit 1
    fi
fi

if [[ -z $SOLR_HOME ]]; then
    coresdir="/opt/solr/server/solr/mycores"
    mkdir -p $coresdir
else
    coresdir=$SOLR_HOME
fi
coredir="$coresdir/$CORE"
if [[ ! -d $coredir ]]; then
    cp -r "$CONFIG_SOURCE/" "$coredir"
    touch "$coredir/core.properties"
    echo created "$CORE"
else
    echo "core $CORE already exists"
fi

  1. 编写run-initdb文件
#!/bin/bash
#
# Run the init-solr-home script and source any '.sh' scripts in
# /docker-entrypoint-initdb.d.
# This script is sourced by some of the solr-* commands, so that
# you can run eg:
#
#   mkdir initdb; echo "echo hi" > initdb/hi.sh
#   docker run -v $PWD/initdb:/docker-entrypoint-initdb.d solr
#
# and have your script execute before Solr 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/docker-solr/scripts/init-solr-home

# execute files in /docker-entrypoint-initdb.d before starting solr
while read -r f; do
    case "$f" in
        *.sh)     echo "$0: running $f"; . "$f" ;;
        *)        echo "$0: ignoring $f" ;;
    esac
    echo
done < <(find /docker-entrypoint-initdb.d/ -mindepth 1 -type f | sort -n)

  1. 编写solr-create文件
#!/bin/bash
#
# This script starts Solr on localhost, creates a core with "solr create",
# stops Solr, and then starts Solr as normal.
# Any arguments are passed to the "solr create".
# To simply create a core:
#      docker run -P -d solr solr-create -c mycore
# To create a core from mounted config:
#      docker run -P -d -v $PWD/myconfig:/myconfig solr solr-create -c mycore -d /myconfig

set -euo pipefail
echo "Executing $0" "[email protected]"

if [[ "${VERBOSE:-}" = "yes" ]]; then
    set -x
fi

. /opt/docker-solr/scripts/run-initdb

# solr uses "-c corename". Parse the arguments to determine the core name.
CORE_NAME="$(
  while (( $# > 0 )); do
    if [[ "$1" == '-c' ]]; then
      shift
      echo "$1"
    fi
    shift
  done
)"
if [[ -z "${CORE_NAME:-}" ]]; then
  echo "could not determine core name"
  exit 1
fi

if [[ -v SOLR_PORT ]] && ! grep -E -q '^[0-9]+$' <<<"$SOLR_PORT"; then
  echo "Invalid SOLR_PORT=$SOLR_PORT environment variable specified"
  exit 1
fi

# cores get created in SOLR_HOME
CORE_DIR="${SOLR_HOME:-/opt/solr/server/solr}/$CORE_NAME"

if [[ -d $CORE_DIR ]]; then
    echo "$CORE_DIR exists; skipping core creation"
else
    start-local-solr

    echo "Creating core with:" "${@:1}"
    /opt/solr/bin/solr create "${@:1}"

    # See https://github.com/docker-solr/docker-solr/issues/27
    echo "Checking core"
    if ! wget -O - "http://localhost:${SOLR_PORT:-8983}/solr/admin/cores?action=STATUS" | grep -q instanceDir; then
      echo "Could not find any cores"
      exit 1
    fi

    echo "Created core with:" "${@:1}"
    stop-local-solr

    # check the core_dir exists; otherwise the detecting above will fail after stop/start
    if [ ! -d "$CORE_DIR" ]; then
        echo "Missing $CORE_DIR"
        exit 1
    fi
fi
exec solr -f

  1. 编写solr-demo文件
#!/bin/bash
#
# Configure a Solr demo and then run solr in the foreground

set -euo pipefail

if [[ "${VERBOSE:-}" = "yes" ]]; then
    set -x
fi

. /opt/docker-solr/scripts/run-initdb

CORE=demo
CORE_DIR="${SOLR_HOME:-/opt/solr/server/solr}/demo"
if [ -d "$CORE_DIR" ]; then
  echo "$CORE_DIR exists; skipping demo creation"
else
  start-local-solr
  echo "Creating $CORE"
  /opt/solr/bin/solr create -c "$CORE"
  echo "Created $CORE"
  echo "Loading example data"
  /opt/solr/bin/post -c $CORE -commit no example/exampledocs/*.xml
  /opt/solr/bin/post -c $CORE -commit no example/exampledocs/books.json
  /opt/solr/bin/post -c $CORE -commit yes example/exampledocs/books.csv
  echo "Loaded example data"
  stop-local-solr

    # check the core_dir exists; otherwise the detecting above will fail after stop/start
    if [ ! -d "$CORE_DIR" ]; then
        echo "Missing $CORE_DIR"
        exit 1
    fi
fi

exec solr -f

  1. 编写solr-foreground文件
#!/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

if [[ -v SOLR_PORT ]] && ! grep -E -q '^[0-9]+$' <<<"${SOLR_PORT}"; then
  echo "Invalid SOLR_PORT=$SOLR_PORT environment variable specified"
  exit 1
fi

echo "Starting Solr $SOLR_VERSION"

exec solr -f "[email protected]"

  1. 编写solr-precreate文件
#!/bin/bash
#
# Create a core on disk and then run solr in the foreground
# arguments are: corename configdir
# To simply create a core:
#      docker run -P -d solr solr-precreate mycore
# To create a core from mounted config:
#      docker run -P -d -v $PWD/myconfig:/myconfig solr solr-precreate mycore /myconfig
# To create a core in a mounted directory:
#      mkdir mycores; chown 8983:8983 mycores
#      docker run -it --rm -P -v $PWD/mycores:/opt/solr/server/solr/mycores solr solr-precreate mycore
set -e

echo "Executing $0" "[email protected]"

if [[ "${VERBOSE:-}" = "yes" ]]; then
    set -x
fi

. /opt/docker-solr/scripts/run-initdb

/opt/docker-solr/scripts/precreate-core "[email protected]"

exec solr -f

  1. 编写start-local-solr文件
#!/bin/bash
# configure Solr to run on the local interface, and start it running in the background

set -euo pipefail

if [[ "${VERBOSE:-}" = "yes" ]]; then
    set -x
fi

# determine the solr logs dir, set by SOLR_LOGS_DIR in the environment or in solr.in.sh.
# let bash parse it, then prefix MY_ so we don't polute the environment used by Solr.
MY_SOLR_INCLUDE="${SOLR_INCLUDE:-/opt/solr/bin/solr.in.sh}"
eval "$( set +e +u +o pipefail; . "$MY_SOLR_INCLUDE"; set -o posix; set | grep -E '^SOLR_(LOGS_DIR|OPTS)=' | sed 's/^SOLR_/MY_SOLR_/')"

MY_SOLR_LOGS_DIR="${MY_SOLR_LOGS_DIR:-/opt/solr/server/logs}"
MY_SOLR_OPTS="${MY_SOLR_OPTS:-} -Djetty.host=${SOLR_LOCAL_HOST:-localhost}"

if [ ! -w "$MY_SOLR_LOGS_DIR" ]; then
    echo "Log directory $MY_SOLR_LOGS_DIR is not writable by $(id -u):$(id -g)"
    exit 1
fi

# If the SOLR_PID_DIR is specified by the user, let Solr use that
if [ -z "${MY_SOLR_PID_DIR:-}" ]; then
    # if not, and the default location is not writable, put it in the log dir
    if [ ! -w "${MY_SOLR_PID_DIR:-/opt/solr/bin}" ]; then
        export SOLR_PID_DIR="$MY_SOLR_LOGS_DIR"
    fi
fi

if [[ -v SOLR_PORT ]] && ! grep -E -q '^[0-9]+$' <<<"${SOLR_PORT:-}"; then
  echo "Invalid SOLR_PORT=$SOLR_PORT environment variable specified"
  exit 1
fi

echo "Running solr in the background. Logs are in $MY_SOLR_LOGS_DIR"
SOLR_OPTS=$MY_SOLR_OPTS solr start
max_try=${MAX_TRY:-12}
wait_seconds=${WAIT_SECONDS:-5}
if ! /opt/docker-solr/scripts/wait-for-solr.sh --max-attempts "$max_try" --wait-seconds "$wait_seconds"; then
    echo "Could not start Solr."
    if [ -f "$MY_SOLR_LOGS_DIR/solr.log" ]; then
        echo "Here is the log:"
        cat "$MY_SOLR_LOGS_DIR/solr.log"
    fi
    exit 1
fi

  1. 编写stop-local-solr文件
#!/bin/bash
# stop the background Solr, and restore the normal configuration

set -e

if [[ "$VERBOSE" = "yes" ]]; then
    set -x
fi

echo "Shutting down the background Solr"
solr stop

  1. 编写wait-for-solr.sh文件
#!/bin/bash
#
# A helper script to wait for solr
#
# Usage: wait-for-solr.sh [--max-attempts count] [--wait-seconds seconds] [--solr-url url]
# Deprecated usage: wait-for-solr.sh [ max_attempts [ wait_seconds ] ]

set -euo pipefail

SCRIPT="$0"

if [[ "${VERBOSE:-}" = "yes" ]]; then
    set -x
fi

function usage {
  echo "$1"
  echo "Usage: $SCRIPT [--max-attempts count] [--wait-seconds seconds ] [--solr-url url]"
  exit 1
}

max_attempts=12
wait_seconds=5

if [[ -v SOLR_PORT ]] && ! grep -E -q '^[0-9]+$' <<<"$SOLR_PORT"; then
  echo "Invalid SOLR_PORT=$SOLR_PORT environment variable specified"
  exit 1
fi

solr_url="http://localhost:${SOLR_PORT:-8983}"

while (( $# > 0 )); do
  case "$1" in
   --help)
     cat <<EOM
Usage: $SCRIPT [options]
Options:
  --max-attempts count: number of attempts to check Solr is up. Default: $max_attempts
  --wait-seconds seconds: number of seconds to wait between attempts. Default: $wait_seconds
  --solr-url url: URL for Solr server to check. Default: $solr_url
EOM
     exit 0
     ;;
   --solr-url)
     solr_url="$2";
     shift 2
     ;;

   --max-attempts)
     max_attempts="$2";
     shift 2;
     ;;

   --wait-seconds)
     wait_seconds="$2";
     shift 2;
     ;;

  * )
    # deprecated invocation, kept for backwards compatibility
    max_attempts=$1;
    wait_seconds=$2;
    echo "WARNING: deprecated invocation. Use $SCRIPT [--max-attempts count] [--wait-seconds seconds]"
    shift 2;
    break;
    ;;

  esac
done

grep -q -E '^[0-9]+$' <<<"$max_attempts" || usage "--max-attempts $max_attempts: not a number"
if (( max_attempts == 0 )); then
  echo "--max-attempts should be >0"
  exit 1
fi
grep -q -E '^[0-9]+$' <<<"$wait_seconds" || usage "--wait-seconds $wait_seconds: not a number"
grep -q -E '^https?://' <<<"$solr_url" || usage "--solr-url $solr_url: not a URL"

((attempts_left=max_attempts))
while (( attempts_left > 0 )); do
  if wget -q -O - "$solr_url" | grep -q -i solr; then
    break
  fi
  (( attempts_left-- ))
  if (( attempts_left == 0 )); then
    echo "solr is still not running; giving up"
    exit 1
  fi
  if (( attempts_left == 1 )); then
    attempts=attempt
  else
    attempts=attempts
  fi
  echo "solr is not running yet on $solr_url. $attempts_left $attempts left"
  sleep "$wait_seconds"
done
echo "solr is running on $solr_url"

  1. 增加可执行权限chmod +x *
  2. 构建建成镜像文件
    docker build -t menard/alpine-solr:7.5.0 .
    docker安装solr

相关文章: