使用 docker 创建 Redis 服务

使用docker创建Redis服务

配置docker镜像源

1
2
3
4
5
6
"registry-mirrors": [
"https://4346hkfk.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://reg-mirror.qiniu.com"
]

拉取镜像

1
docker pull redis

创建配置文件

redis并不会创建redis.conf配置文件,而在启动容器时,如果宿主机以及容器内都没有配置文件,docker会自动创建,但是docker创建的redis.conf是目录,并不是文件,因此我们需要提前创建这个文件。

1
2
3
mkdir -p ~/redis/conf

touch ~/redis/conf/redis.conf

配置Redis

编辑redis.conf

1
vim ~/redis/conf/redis.conf

设置以下配置:

1
2
3
4
appendonly yes 						# 数据持久化
protected-mode no # 外部网络访问
bind 0.0.0.0 # 所有ip都可以访问
requirepass 123456 # 设置访问密码

启动容器

1
docker run --name redis -p 6379:6379 -v ~/redis/data:/data -v ~/redis/conf/redis.conf:/etc/redis/redis.conf -d redis:latest redis-server /etc/redis/redis.conf

进入容器

1
2
3
4
5
6
7
docker exec -it redis bash
--------------------------
root@0d0142d45f55:/data# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set ping pong
OK
127.0.0.1:6379>

通过redis桌面管理工具查看是否生效:

说明redis运行成功!


使用 docker 创建 Redis 服务
https://bald3r.wang/2023/07/27/使用-docker-创建-Redis-服务/
作者
Allen
发布于
2023年7月27日
许可协议