diff --git a/Dockerfile b/Dockerfile index c26785f63ed4cd1b4b233dcf763ac577b4c0e8c4..9fe0799bef73b264c4bfb1726662db23b38df2e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ ARG REDIS_DOWNLOAD_URL="http://download.redis.io/" ARG REDIS_VERSION="stable" -RUN addgroup -S -g 1000 redis && adduser -S -G redis -u 999 redis && \ +RUN addgroup -S -g 1001 redis && adduser -S -G redis -u 1001 redis && \ apk add --no-cache su-exec tzdata make curl build-base linux-headers bash RUN curl -fL -Lo /tmp/redis-${REDIS_VERSION}.tar.gz ${REDIS_DOWNLOAD_URL}/redis-${REDIS_VERSION}.tar.gz && \ @@ -21,14 +21,21 @@ RUN curl -fL -Lo /tmp/redis-${REDIS_VERSION}.tar.gz ${REDIS_DOWNLOAD_URL}/redis- make install && \ mkdir -p /etc/redis && \ cp -f *.conf /etc/redis && \ - rm -rf /tmp/redis-${REDIS_VERSION}* + rm -rf /tmp/redis-${REDIS_VERSION}* && \ + apk del curl make + +COPY redis.conf /etc/redis/redis.conf COPY entrypoint.sh /usr/bin/entrypoint.sh +COPY run.sh /usr/bin/run.sh + VOLUME ["/data"] WORKDIR /data +USER redis + EXPOSE 6379 ENTRYPOINT ["/usr/bin/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh index 47668cc587a194529e622a5b2015facbc5efad2f..8f4dbb0d33d1ed27a6e958fcb4b3ded8cac5929e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,22 +1,9 @@ #!/bin/bash -set -eu - -generate_common_config() { - { - echo "bind 0.0.0.0" - echo protected-mode yes - echo tcp-backlog 511 - echo timeout 0 - echo tcp-keepalive 300 - echo daemonize no - echo supervised no - echo pidfile /var/run/redis.pid - } > /etc/redis/redis.conf -} +set -ex set_redis_password() { - if [ -z "${REDIS_PASSWORD}" ]; then + if [[ -z "${REDIS_PASSWORD}" ]]; then echo "Redis is running without password which is not recommended" else { @@ -27,7 +14,7 @@ set_redis_password() { } redis_mode_setup() { - if [ "${SETUP_MODE}" = "cluster" ]; then + if [[ "${SETUP_MODE}" == "cluster" ]]; then { echo cluster-enabled yes echo cluster-config-file nodes.conf @@ -39,15 +26,15 @@ redis_mode_setup() { } start_redis() { - echo "Starting redis service " + echo "Starting redis service....." redis-server /etc/redis/redis.conf } main_function() { - generate_common_config set_redis_password redis_mode_setup start_redis + redis_server_mode } main_function diff --git a/redis.conf b/redis.conf new file mode 100644 index 0000000000000000000000000000000000000000..ddfa5405bc1754322afb018c58f967cddc639cd5 --- /dev/null +++ b/redis.conf @@ -0,0 +1,8 @@ +bind 0.0.0.0 +protected-mode yes +tcp-backlog 511 +timeout 0 +tcp-keepalive 300 +daemonize no +supervised no +pidfile /var/run/redis.pid diff --git a/run.sh b/run.sh new file mode 100755 index 0000000000000000000000000000000000000000..03bb7948c89fe4d194793bdb95b263c8de4bf66c --- /dev/null +++ b/run.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +redis_server_mode() { + if [[ "${SERVER_MODE}" == "master" ]]; then + echo "Redis server mode is master" + elif [[ "${SERVER_MODE}" == "slave" ]]; then + echo "Redis server mode is slave" + if [[ -z "${REDIS_PASSWORD}" ]]; then + redis-cli --add-node "${SLAVE_IP}" "${MASTER_IP}" --cluster-slave + else + redis-cli --add-node "${SLAVE_IP}" "${MASTER_IP}" --cluster-slave -a "${REDIS_PASSWORD}" + fi + else + echo "Redis server mode is standalone" + fi +} + +redis_server_mode