macos:用docker安装redis
一,创建需要用到的目录:
% mkdir -p conf
% mkdir -p data
% ls
conf data
二,创建配置文件
命令:
% vi conf/redis.conf
内容:
# 允许外网/宿主机连接
bind 0.0.0.0
# 关闭保护模式(如果设置了 requirepass,开启 yes 亦可)
protected-mode no
# 设置 Redis 访问密码(请将 mypassword 修改为您设置的实际密码)
requirepass mypassword
# 开启 AOF 持久化
appendonly yes
三,下载image
% docker pull redis:latest
查看效果:
% docker image ls
i Info → U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
hello-world:latest 5e2309035332 25.9kB 9.49kB U
mysql:8.0 7dcddc01f13b 1.1GB 249MB U
nginx:latest cb29e33d254e 253MB 69.2MB U
redis:latest 718f745deb7d 213MB 57.6MB
四,创建配置文件:
命令:
% vi docker-compose.yml
内容:
services:
redis:
image: redis:latest
container_name: my-redis
restart: always
ports:
- "6379:6379"
volumes:
# 挂载配置文件
- ./conf/redis.conf:/etc/redis/redis.conf
# 挂载持久化数据目录
- ./data:/data
# 启动时加载指定的配置文件
command: redis-server /etc/redis/redis.conf
五,启动redis服务
% docker compose up -d
[+] up 2/2
✔ Network redis_default Created 0.0s
✔ Container my-redis Started
查看效果:
% docker ps | grep redis
970fe864f864 redis:latest "docker-entrypoint.s…" 52 seconds ago Up 51 seconds 0.0.0.0:6379->6379/tcp, [::]:6379->6379/tcp my-redis
六,连接redis服务测试效果:
% docker exec -it my-redis redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth mypassword
OK
127.0.0.1:6379> keys *
(empty array)
浙公网安备 33010602011771号