下载 redis 镜像
docker pull redis
自动下载latest版本。
新增redis的配置目录和数据持久化目录
mkdir /opt/redis/conf
mkdir /opt/redis/data
下载redis.conf
到 /opt/redis/conf目录下
wget http://download.redis.io/redis-stable/redis.conf
运行redis
docker run -p 6379:6379 --name myredis -v /opt/redis/conf/redis.conf:/etc/redis/redis.conf -v /opt/redis/data:/data --network spring-net -d redis redis-server /etc/redis/redis.conf --requirepass "xxxxxx" --appendonly yes
- p 端口转换
- v /opt/redis/conf/redis.conf:/etc/redis/redis.conf:把宿主机配置好的redis.conf放到容器内的这个位置中
- v /opt/redis/data: /data:把redis持久化的数据在宿主机内显示,做数据备份
- redis-server /etc/redis/redis.conf:这个是关键配置,让redis不是无配置启动,而是按照这个redis.conf的配置启动
- appendonly yes:redis启动后数据持久化
- network: 将其放在特定网络中
- requirepass: 设置redis访问密码
进入redis容器,验证是否可用
docker exec -it myredis redis-cli
127.0.0.1:6379> AUTH xxxxxx
OK
127.0.0.1:6379> set "age" 18
OK
127.0.0.1:6379> get age
18
如果要远程访问,需要修改redis.conf文件
- 将“bind 127.0.0.1”注释掉。
- 将“protected-mode yes”改成“protected-mode no”
All done.