环境说明:

  • 系统镜像: Redis:5.x (示例容器为: redis:5.0.4 )
  • 操作系统: Centos
  • Docker 版本: 19.03.8

具体实现步骤

操作系统简单优化

1
2
3
4
5
6
echo 'vm.overcommit_memory=1
net.core.somaxconn=65535' >> /etc/sysctl.conf

sysctl -p

echo never > /sys/kernel/mm/transparent_hugepage/enable

下载容器

1
docker pull redis:5.0.4 

创建依赖文件夹

1
mkdir -p /application/redis/{conf,data}

初始化配置文件

1
2
3
4
5
6
wget https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf -O /application/redis/conf/redis.conf
 
sed -i 's/logfile ""/logfile "access.log"/' /application/redis/conf/redis.conf  # 开启日志
sed -i 's/# requirepass foobared/requirepass aabb/' /application/redis/conf/redis.conf  # 设置密码
sed -i 's/appendonly no/appendonly yes/' /application/redis/conf/redis.conf  # 开启持久化
sed -i  's/bind 127.0.0.1/bind 0.0.0.0/' /application/redis/conf/redis.conf # 修改端口监听

启动容器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 docker run \
 -v /application/redis/conf/redis.conf:/etc/redis/redis.conf \
 -v /application/redis/data:/data \
 -p 6379:6379 \
 --restart=always \
 -m 8192M \
 --memory-swap 0 \
 --oom-kill-disable \
 --privileged=true \
 --name redis-server  \
 -d redis:5.0.4 redis-server  /etc/redis/redis.conf