1. 拉取镜像
docker pull redis
2. 配置文件
2.1 自定义配置文件
- 创建
mydata/redis/redis.conf
,写入内容:
# 数据持久化 appendonly yes
2.2 运行命令
- 使用自定义配置文件启动
docker run -v /mydata/redis/redis.conf:/usr/local/etc/redis/redis.conf \ -v /mydata/redis/data:/data \ -d --name myredis \ -p 6379:6379 \ redis redis-server /usr/local/etc/redis/redis.conf
2.3 查看
root@docker01:/mydata/redis# docker run -v /mydata/redis/redis.conf:/usr/local/etc/redis/redis.conf \ > -v /mydata/redis/data:/data \ > -d --name myredis \ > -p 6379:6379 \ > redis redis-server /usr/local/etc/redis/redis.conf f2698d4f67035f97221d9ef7a9b8248303e9815ac8da485e89259d0714624131 root@docker01:/mydata/redis# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2698d4f6703 redis "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp myredis a6f48d56297a nginx "/docker-entrypoint.…" About an hour ago Up 2 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp mynginx root@docker01:/mydata/redis#
通过 redis-cli 连接测试使用 redis 服务
root@docker01:/mydata/redis# docker exec -it myredis /bin/bash root@f2698d4f6703:/data# redis-cli 127.0.0.1:6379> set test 1 OK 127.0.0.1:6379>
3. 设置密码
mydata/redis/redis.conf
,写入内容:
# 数据持久化 appendonly yes # 设置密码 requirepass redisroot
重启redis
root@docker01:/mydata/redis# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2698d4f6703 redis "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp myredis a6f48d56297a nginx "/docker-entrypoint.…" About an hour ago Up 9 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp mynginx # 重启myredis root@docker01:/mydata/redis# docker restart f2698d4f6703