Browse Source

feat: redis support in docker

master v1.4
Dnomd343 3 years ago
parent
commit
fd935b2215
  1. 3
      Dockerfile
  2. 8
      backend/redis.php
  3. 1
      conf/docker/init.sh
  4. 2
      docs/setup-docker.md

3
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 && \

8
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']); // 设置过期时间

1
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

2
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

Loading…
Cancel
Save