Docker启动运行Redis


Docker安装Redis服务并以配置文件方式启动

1)镜像获取

#搜索镜像
docker search redis
#拉取镜像 docker pull redis
#查看镜像 docker images

2)启动Redis

一、从Redis.io官网获取redis.conf配置文件

二、在/usr/local/创建redis文件夹,并把redis.conf文件拷贝进来

三、修改redis.conf配置文件

#注释掉这部分,这是限制redis只能本地访问
bind 127.0.0.1
#默认yes,开启保护模式,限制为本地访问 protected-mode no
#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方#式启动redis失败 daemonize no
#redis持久化(可选) appendonly yes
#设置密码 requirepass
123456

四、启动命令

docker run -p 6379:6379 --name redis
-v /usr/local/redis.conf:/etc/redis/redis.conf 
-v /usr/local/data:/data 
-d redis redis-server /etc/redis/redis.conf 
--appendonly yes 

命令注释:

  • -p 6379:6379 端口映射:前表示主机部分,:后表示容器部分。
  • --name redis 指定该容器名称,查看和进行操作都比较方便。
  • -v 挂载目录,规则与端口映射相同。
  • -d redis 表示后台启动redis。
  • redis-server /etc/redis/redis.conf 以配置文件启动redis,加载容器内的conf文件,最终找到的是挂载的目录/usr/local/redis.conf。
  • --appendonly yes 开启redis 持久化

3)总结

  1. 在服务器部署redis服务,一定要设置密码,不然很容易受到攻击!
  2. 使用外部服务连接redis,需要开放相应的端口,同时根据上文所说redis.conf配置文件注释掉bind行,否则访问不了


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM