From fd935b2215fcce0f4013889ee2accc0ce06d9ee9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 11 Jul 2021 22:19:22 +0800 Subject: [PATCH] feat: redis support in docker --- Dockerfile | 3 ++- backend/redis.php | 8 ++++++-- conf/docker/init.sh | 1 + docs/setup-docker.md | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6884b8c..fad5a7d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,10 +6,11 @@ FROM alpine LABEL maintainer="dnomd343" COPY . /var/www/echoIP COPY --from=iconv /tmp/iconv/ /usr/local/lib/ -RUN apk --update add --no-cache nginx curl nodejs php7 php7-fpm php7-json php7-iconv php7-sqlite3 php7-openssl php7-mbstring && \ +RUN apk --update add --no-cache nginx curl nodejs redis php7 php7-fpm php7-json php7-iconv php7-redis php7-sqlite3 php7-openssl php7-mbstring && \ rm /usr/lib/php7/modules/iconv.so && ln -s /usr/local/lib/iconv.so /usr/lib/php7/modules/ && \ mv /usr/local/lib/libiconv.so /usr/local/lib/libiconv.so.2 && \ mkdir -p /run/nginx && touch /run/nginx/nginx.pid && \ + echo "daemonize yes" >> /etc/redis.conf && \ cp /var/www/echoIP/conf/docker/init.sh / && \ cp /var/www/echoIP/conf/docker/ip.conf /etc/nginx/echoip.conf && \ cp -f /var/www/echoIP/conf/docker/nginx.conf /etc/nginx/nginx.conf && \ diff --git a/backend/redis.php b/backend/redis.php index 9b98310..be20bca 100644 --- a/backend/redis.php +++ b/backend/redis.php @@ -13,7 +13,9 @@ function getRedisData($ip) { // 查询Redis,不存在返回NULL $redis = new Redis(); global $redisSetting; $redis->connect($redisSetting['host'], $redisSetting['port']); - $redis->auth($redisSetting['passwd']); + if ($redisSetting['passwd'] != '') { + $redis->auth($redisSetting['passwd']); + } $redisKey = $redisSetting['prefix'] . $ip; $redisValue = $redis->exists($redisKey) ? $redis->get($redisKey) : NULL; return $redisValue; @@ -23,7 +25,9 @@ function setRedisData($ip, $data) { // 写入信息到Redis $redis = new Redis(); global $redisSetting; $redis->connect($redisSetting['host'], $redisSetting['port']); - $redis->auth($redisSetting['passwd']); + if ($redisSetting['passwd'] != '') { + $redis->auth($redisSetting['passwd']); + } $redisKey = $redisSetting['prefix'] . $ip; $redis->set($redisKey, $data); // 写入数据库 $redis->pexpire($redisKey, $redisSetting['cache_time']); // 设置过期时间 diff --git a/conf/docker/init.sh b/conf/docker/init.sh index c2ebff3..76c3056 100644 --- a/conf/docker/init.sh +++ b/conf/docker/init.sh @@ -1,3 +1,4 @@ +/usr/bin/redis-server /etc/redis.conf /usr/sbin/php-fpm7 /usr/sbin/nginx sh /var/www/echoIP/backend/qqwryUpdate.sh diff --git a/docs/setup-docker.md b/docs/setup-docker.md index 5b50951..9e62975 100644 --- a/docs/setup-docker.md +++ b/docs/setup-docker.md @@ -42,7 +42,7 @@ CONTAINER ID IMAGE COMMAND CREATED STATU 48d4b7a644e8 dnomd343/echoip "sh init.sh" 12 seconds ago Created echoip ``` -容器开启后会自动拉取 `qqwry.dat` 离线数据库,如果网络较慢可能需要数分钟时间,期间查询信息不完整。可以在htop等进程管理工具中查看docker内部进程,在拉取期间将存在以下进程 +容器开启后会自动拉取 `qqwry.dat` 离线数据库,如果网络较慢可能需要数分钟时间,期间查询信息不完整,且由于容器内部配置了Redis缓存,缓存过期前(默认为一个小时)将一直保持不完整信息,因此务必在确认数据库拉取完成后再执行查询。可以在htop等进程管理工具中查看docker内部进程,在拉取期间将存在以下进程 ``` sh init.sh