前言
机器:centos7
安装了docker,如果没有安装可点击这篇安装教程
开始安装
可以先上docker hub上查看一下redis的最新版本信息
输入redis
点击后进去可以查看支持的tags,即版本号
或者直接命令行先查一下redis的版本,不过这里看不到什么版本,官方的那个是最新的版本
[root@192 ~]# docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 8603 [OK]
bitnami/redis Bitnami Redis Docker Image 162 [OK]
sameersbn/redis 81 [OK]
grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0 71
rediscommander/redis-commander Alpine image for redis-commander - Redis man… 47 [OK]
kubeguide/redis-master redis-master with "Hello World!" 33
redislabs/redis Clustered in-memory database engine compatib… 27
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 24
oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp… 22
arm32v7/redis Redis is an open source key-value store that… 21
redislabs/rejson RedisJSON - Enhanced JSON data type processi… 18
bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 16 [OK]
redislabs/redisinsight RedisInsight - The GUI for Redis 13
redislabs/redisgraph A graph database module for Redis 12 [OK]
webhippie/redis Docker images for Redis 12 [OK]
s7anley/redis-sentinel-docker Redis Sentinel 10 [OK]
insready/redis-stat Docker image for the real-time Redis monitor… 9 [OK]
arm64v8/redis Redis is an open source key-value store that… 9
redislabs/redismod An automated build of redismod - latest Redi… 7 [OK]
centos/redis-32-centos7 Redis in-memory data structure store, used a… 5
circleci/redis CircleCI images for Redis 5 [OK]
clearlinux/redis Redis key-value data structure server with t… 2
tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK]
wodby/redis Redis container image with orchestration 1 [OK]
xetamus/redis-resource forked redis-resource 0 [OK]
- 拉取镜像
我这里直接拉取最新的版本
[root@mq0 ~]# docker pull redis:latest
#也可以直接不带版本号,即最新版本
[root@mq0 ~]# docker pull redis
#查看镜像
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 84c5f6e03bf0 2 weeks ago 104MB
- 运行镜像
未使用自定义配置文件启动
[root@192 ~]# docker run --name redis -p 6379:6379 -d redis (暴露外部宿主机端口为6379进行连接)
使用自定义配置文件启动
[root@192 ~]# 默认情况下redis官方镜像中没有redis.conf配置文件 需要去官网下载指定版本的配置文件
#1. wget http://download.redis.io/releases/redis-5.0.8.tar.gz 下载官方安装包
#2. 将官方安装包中配置文件进行复制到宿主机指定目录中如 /root/redis/redis.conf文件
#3. 修改需要自定义的配置
# 切忌将daemon no 改为yes ,会与docker -d 参数冲突
# bind 0.0.0.0 开启远程权限
# appenonly yes 开启aof持久化
#4. 加载配置启动
docker run --name redis -v /root/redis:/usr/local/etc/redis -p 6379:6379 -d redis redis-server /usr/local/etc/redis/redis.conf
如果还要将数据目录挂在到本地保证数据安全,最终的如下
[root@192 ~]# docker run --name redis -v /root/redis/data:/data -v /root/redis/:/usr/local/etc/redis/ -p 6379:6379 -d redis redis-server /usr/local/etc/redis/redis.conf
参数解释:
–name: 别名
-v /root/redis/data:/data 将数据目录挂载到宿主机目录上
-v /root/redis/redis.conf:/usr/local/etc/redis/redis.conf 将配置文件挂载到宿主机上
-p 将容器内的端口映射出去
-d: 守护线程(后台)运行,daemon的缩写
查看启动日志
[root@192 ~]# docker logs -t -f 容器id|容器名称
特别强调
我将redis.conf中daemon 改为了yes,用上述的怎么也启动不了
[root@192 usr]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
873205525bce redis "docker-entrypoint.s…" 14 seconds ago Exited (0) 13 seconds ago
发现redis怎么都启动不了,原因在于docker中的-d参数和redis.conf中daemon yes是矛盾的
,这才导致启动不起来,可能是由于我们不用docker方式安装redis的时候都会把配置文件改成daemon yes,但在docker这里却是个大忌!!!
- 界面访问