使用Docker部署Redis时使用的是默认配置文件不带鉴权
需要鉴权可以把设置写到配置文件后通过Dockerfile生成新的镜像
- redis配置文件
# cat redis.conf
bind 0.0.0.0
port 6379
timeout 0
tcp-keepalive 300
daemonize no
databases 16
save 900 1
save 300 10
save 60 10000
# 是否压缩
rdbcompression yes
# 导入时是否检查
rdbchecksum yes
dir /data
dbfilename dump.rdb
# 密码
requirepass password
- Dockerfile文件
# cat Dockerfile
FROM redis:7.2.4
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
- 通过Dockerfile创建新镜像
tag设置为password
# docker build -t redis:password .
[+] Building 0.4s (7/7) FINISHED docker:default
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 212B 0.0s
=> [internal] load metadata for docker.io/library/redis:7.2.4 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 330B 0.0s
=> [1/2] FROM docker.io/library/redis:7.2.4 0.0s
=> CACHED [2/2] COPY redis.conf /usr/local/etc/redis/redis.conf 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:1e1e4ec7910f5e789e966c149f447335cfec87296e18adb12f0606dee2ac7696 0.0s
=> => naming to docker.io/library/redis:password 0.0s
- 使用新镜像启动
# docker run -d -p 6379:6379 -v /opt/redis-data/:/data redis:password
- 验证鉴权和持久化
使用密码连接redis
如果密码错误会出现认证失败提示
AUTH failed: WRONGPASS invalid username-password pair or user is disabled.
# redis-cli -h 127.0.0.1 -a 'password'
# 设置a为2并保存
# 127.0.0.1:6379> set a 2
OK
127.0.0.1:6379> save
OK
挂载目录有持久化数据
# ls -lah /opt/redis-data/dump.rdb
-rw-------. 1 systemd-coredump input 98 3月 14 09:38 /opt/redis-data/dump.rdb
标签:验证,0.0,redis,conf,Docker,password,Dockerfile,docker,Redis
From: https://www.cnblogs.com/minseo/p/18072138