在docker中安装redis

Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 的 NoSQL 数据库,并提供多种语言的 API。
1、查看可用的Redis版本
访问Redis镜像库地址: https://hub.docker.com/_/redis?tab=tags
可以通过 Sort by 查看其他版本的 Redis,默认是最新版本 redis:latest。

也用 docker search redis 命令来查看可用版本:

[root@localhost ~]# docker search redis
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis                            Redis is an open source key-value store that…   8792                [OK]                
bitnami/redis                    Bitnami Redis Docker Image                      168                                     [OK]
sameersbn/redis                                                                  82                                      [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0           72                                      
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   47                                      [OK]
kubeguide/redis-master           redis-master with "Hello World!"                33                                      
redislabs/redisearch             Redis With the RedisSearch module pre-loaded…   29                                      
redislabs/redis                  Clustered in-memory database engine compatib…   27                                      
oliver006/redis_exporter          Prometheus Exporter for Redis Metrics. Supp…   22                                      
redislabs/rejson                 RedisJSON - Enhanced JSON data type processi…   22                                      
arm32v7/redis                    Redis is an open source key-value store that…   22                                      
bitnami/redis-sentinel           Bitnami Docker Image for Redis Sentinel         17                                      [OK]
redislabs/redisinsight           RedisInsight - The GUI for Redis                14                                      
redislabs/redisgraph             A graph database module for Redis               12                                      [OK]
webhippie/redis                  Docker images for Redis                         11                                      [OK]
s7anley/redis-sentinel-docker    Redis Sentinel                                  10                                      [OK]
arm64v8/redis                    Redis is an open source key-value store that…   10                                      
insready/redis-stat              Docker image for the real-time Redis monitor…   9                                       [OK]
redislabs/redismod               An automated build of redismod - latest Redi…   7                                       [OK]
circleci/redis                   CircleCI images for Redis                       5                                       [OK]
centos/redis-32-centos7          Redis in-memory data structure store, used a…   5                                       
clearlinux/redis                 Redis key-value data structure server with t…   3                                       
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]

2、拉取官方的最新版本镜像
docker pull redis:latest

[root@localhost ~]# docker pull redis:latest
latest: Pulling from library/redis
852e50cd189d: Pull complete
76190fa64fb8: Pull complete
9cbb1b61e01b: Pull complete
d048021f2aae: Pull complete
6f4b2af24926: Pull complete
1cf1d6922fba: Pull complete
Digest: sha256:5b98e32b58cdbf9f6b6f77072c4915d5ebec43912114031f37fa5fa25b032489
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

3、查看本地镜像
docker images

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              74d107221092        2 weeks ago         104MB
mysql               5.7                 1b12f2e9257b        5 weeks ago         448MB

4、创建配置文件目录
mkdir -p /data/docker/redis/conf

[root@localhost ~]# mkdir -p /data/docker/redis/conf

下载官方配置文件并修改 http://download.redis.io/redis-stable/redis.conf
修改启动默认配置

bind 127.0.0.1 #注释掉这部分,这是限制redis只能本地访问
protected-mode no #默认yes,开启保护模式,限制为本地访问
daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败
并将修改后redis.conf上传到配置文件夹下

[root@localhost conf]# pwd
/data/docker/redis/conf
[root@localhost conf]# ll
total 84
-rw-r--r-- 1 root root 84842 Dec  2 21:47 redis.conf

5、docker启动redis
docker run -p 6379:6379 --name redis -v /data/docker/redis/conf/redis.conf:/etc/redis/redis.conf -v /data/docker/redis/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文件,最终找到的是挂载的目录/data/docker/redis/conf/redis.conf
--appendonly yes 开启redis 持久化

[root@localhost redis]# docker run -p 6379:6379 --name redis -v /data/docker/redis/conf/redis.conf:/etc/redis/redis.conf -v /data/docker/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
83bd0ed4cd24a8120279715929547c859085d81e6938bfae458430ab5acb08fa

docker ps 查看容器


[root@localhost conf]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
34db9947c8a7        redis:latest        "docker-entrypoint.s…"   7 seconds ago       Up 5 seconds        0.0.0.0:6379->6379/tcp              redis
e1d7217e6ecb        mysql:5.7           "docker-entrypoint.s…"   3 weeks ago         Up 3 weeks          0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

进入redis
方式一:
docker exec -it redis /bin/bash
docker-cli

[root@localhost conf]# docker exec -it redis /bin/bash
root@34db9947c8a7:/data# redis-cli
127.0.0.1:6379> set ke 1
OK
127.0.0.1:6379>

方式二:docker exec -it redis redis-cl

[root@localhost conf]# docker exec -it redis redis-cli
127.0.0.1:6379>

附录:dockers基本命令


查看所有镜像 docker images
删除镜像(会提示先停止使用中的容器) docker rmi  镜像name/镜像id
查看所有容器 docker ps -a
查看容器运行日志 docker logs 容器名称/容器id
停止容器运行 docker stop 容器name/容器id
终止容器后运行 docker start 容器name/容器id
容器重启 docker restart 容器name/容器id
删除容器 docker rm 容器name/容器id
posted @ 2020-12-02 23:05  Deadwjlee  阅读(370)  评论(0)    收藏  举报