From de9cf95c598bf225765cb798d9325cc47d2a1354 Mon Sep 17 00:00:00 2001 From: sundayenglish Date: Fri, 10 Oct 2025 09:54:50 +0700 Subject: [PATCH] new redis --- .env | 7 +++++++ docker-compose.yml | 37 ++++++++++++++++++++----------------- scripts/redis-entrypoint.sh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 .env create mode 100644 scripts/redis-entrypoint.sh diff --git a/.env b/.env new file mode 100644 index 0000000..b6e4dd8 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +REDIS_VERSION=7.4-alpine +REDIS_PASSWORD="1qaz2wsxA@" +REDIS_MAXMEMORY=512mb +REDIS_EVICTION_POLICY=allkeys-lru +NETWORK_NAME=gk.internal +ENABLE_PERSIST=NO +EXPOSE_PORT=6379 diff --git a/docker-compose.yml b/docker-compose.yml index 3d20f8d..5db1999 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,21 +1,24 @@ -version: '3.3' +version: "3.9" services: - redis-service: - image: redis:latest - container_name: redis-service - restart: always - ports: - - "6379:6379" - volumes: - - ./:/root/redis - - ./redis.conf:/usr/local/etc/redis/redis.conf + redis: + image: redis:${REDIS_VERSION} + container_name: redis + restart: unless-stopped + entrypoint: ["/usr/local/bin/redis-entrypoint.sh"] environment: - - REDIS_PASSWORD=A@1qaz2wsxA@ - - REDIS_PORT=6379 - - REDIS_DATABASES=16 -networks: - app-network: - name: gk.sunday.redis - driver: bridge + REDIS_PASSWORD: ${REDIS_PASSWORD} + REDIS_MAXMEMORY: ${REDIS_MAXMEMORY} + REDIS_EVICTION_POLICY: ${REDIS_EVICTION_POLICY} + ENABLE_PERSIST: ${ENABLE_PERSIST} + volumes: + - ./scripts/redis-entrypoint.sh:/usr/local/bin/redis-entrypoint.sh:ro + - ./data:/data + ports: + - "${EXPOSE_PORT}:6379" + networks: [default] +networks: + default: + external: true + name: ${NETWORK_NAME} diff --git a/scripts/redis-entrypoint.sh b/scripts/redis-entrypoint.sh new file mode 100644 index 0000000..5ae46d0 --- /dev/null +++ b/scripts/redis-entrypoint.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env sh +set -eu + +# Đọc biến môi trường (có default hợp lý) +REDIS_PASSWORD="${REDIS_PASSWORD:?REDIS_PASSWORD is required}" +REDIS_MAXMEMORY="${REDIS_MAXMEMORY:-512mb}" +REDIS_EVICTION_POLICY="${REDIS_EVICTION_POLICY:-allkeys-lru}" +ENABLE_PERSIST="${ENABLE_PERSIST:-NO}" + +# Thư mục dữ liệu +mkdir -p /data + +CMD="redis-server \ + --requirepass '${REDIS_PASSWORD}' \ + --maxmemory ${REDIS_MAXMEMORY} \ + --maxmemory-policy ${REDIS_EVICTION_POLICY} \ + --bind 0.0.0.0 \ + --port 6379 \ + --loglevel notice \ + --dir /data \ + --protected-mode yes" + +if [ "${ENABLE_PERSIST}" = "YES" ]; then + # Snapshot nhẹ (RDB): 900s nếu >=1 key đổi; 300s nếu >=10 key + CMD="$CMD --save 900 1 --save 300 10 --appendonly no" +else + # Cache thuần: tắt persist để giảm I/O + CMD="$CMD --save '' --appendonly no" +fi + +echo "[entrypoint] Starting: $CMD" +exec sh -c "$CMD"