1、拉取redis镜像
docker pull redis
2、创建挂载目录
sudo mkdir /docker-data/redis
3、下载redis.conf文件
cd /docker-data/redis
sudo wget https://raw.githubusercontent.com/redis/redis/6.2.14/redis.conf
4、权限
chmod 777 redis.conf
5、修改默认配置信息
sudo vi /docker-data/redis/redis.conf
bind 127.0.0.1 # 这行要注释掉,解除本地连接限制
protected-mode no # 默认yes,如果设置为yes,则只允许在本机的回环连接,其他机器无法连接。
daemonize no # 默认no 为不守护进程模式,docker部署不需要改为yes,docker run -d本身就是后台启动,不然会冲突
requirepass 123456 # 设置密码
appendonly yes # 持久化
6、docker启动redis
docker run --name redis \
-p 6379:6379 \
-v /docker-data/redis/redis.conf:/etc/redis/redis.conf \
-v /docker-data/redis:/data \
-d redis:6.2.14 redis-server /etc/redis/redis.conf --appendonly yes
说明:
- -p 6379:6379:端口映射,前面是宿主机,后面是容器。
- –name redis:指定该容器名称。
- -v 挂载文件或目录:前面是宿主机,后面是容器。
- -d redis redis-server /etc/redis/redis.conf:表示后台启动redis,以配置文件启动redis,加载容器内的conf文件。
- appendonly yes:开启redis 持久化。
7、查看运行状态
docker logs redis
1:M 25 Mar 2024 09:13:11.873 # User requested shutdown...
1:M 25 Mar 2024 09:13:11.873 * Calling fsync() on the AOF file.
1:M 25 Mar 2024 09:13:11.874 * Saving the final RDB snapshot before exiting.
1:M 25 Mar 2024 09:13:11.877 * DB saved on disk
1:M 25 Mar 2024 09:13:11.877 * Removing the pid file.
1:M 25 Mar 2024 09:13:11.877 # Redis is now ready to exit, bye bye...
1:C 25 Mar 2024 09:13:12.327 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 25 Mar 2024 09:13:12.327 # Redis version=6.2.14, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 25 Mar 2024 09:13:12.327 # Configuration loaded
1:M 25 Mar 2024 09:13:12.328 * monotonic clock: POSIX clock_gettime
1:M 25 Mar 2024 09:13:12.328 * Running mode=standalone, port=6379.
1:M 25 Mar 2024 09:13:12.328 # Server initialized
1:M 25 Mar 2024 09:13:12.329 * Ready to accept connections
运行成功
附上redis.conf配置文件(顶部资源,免费下载)