在CentOS7上用Dokcer容器安装Redis

检查配置

请参考此文安装Docker CE

Redis

搜索镜像

  • STARS用户最欢程度,OFFICIAL是否为官方版

    1
    2
    3
    4
    5
    6
    7
    
    $ sudo docker search redis
    
    NAME              DESCRIPTION                                     STARS   OFFICIAL   AUTOMATED
    redis             Redis is an open source key-value store that…   6799    [OK]                
    bitnami/redis     Bitnami Redis Docker Image                      11                 [OK]
    sameersbn/redis                                                   76                 [OK]
    .....

拉取镜像

  • 拉取STARS最多且OFFICIAL[OK](官方版)的版本即可

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    $ sudo docker pull redis:5.0.4
    
    5.0.4: Pulling from library/redis
    27833a3ba0a5: Already exists 
    cde8019a4b43: Pull complete 
    97a473b37fb2: Pull complete 
    c6fe0dfbb7e3: Pull complete 
    39c8f5ba1240: Pull complete 
    cfbdd870cf75: Pull complete 
    Digest: sha256:000339fb57e0ddf2d48d72f3341e47a8ca3b1beae9bdcb25a96323095b72a79b
    Status: Downloaded newer image for redis:5.0.4
  • 查看拉取到本地的Redis镜像

    1
    2
    3
    4
    5
    
    $ sudo docker images
    
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    redis               5.0.4               a55fbf438dfd        4 weeks ago         95MB
    ......

创建容器

推荐创建容器时,始终指定镜像的版本——Image:Tag

简单模式

1
$ sudo docker run --name iredis -d redis:5.0.4

配置模式

  • 创建本地目录

    1
    2
    3
    4
    5
    6
    7
    
    # 用于存放数据
    $ mkdir -p ~/soft/redis/data
    # 用于存储配置文件 
    $ mkdir -p ~/soft/redis/etc
    
    # 合成一句
    $ mkdir -p ~/soft/redis/{data,etc}
  • 创建配置文件,详细配置项

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    # redis.conf
    # for all options, see:
    # https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf
    
    
    # 操作系统中设置为yes,后台运行
    # 但在docker中如果后台运行,一定时间docker会自动停止,所以在容器中设置为no
    daemonize no
    port 6379
    # 外网可以访问
    # bind 0.0.0.0
    dir "/data/redis"
    logfile "redis-6379.log"
    protected-mode no
    # 打开aop机制
    appendonly yes
  • 创建redis容器

    1
    2
    3
    4
    
    $ sudo docker run --name iredis -p 6379:6379 \
                  -v ~/soft/redis/data:/data/redis \
                  -v ~/soft/redis/etc/redis-6379.conf:/etc/redis-6379.conf \
                  -d redis:5.0.4 redis-server /etc/redis-6379.conf
  • -p 6379:6379端口映射 宿主机:容器

  • -v ~/soft/redis/data:/data/redis映射数据目录 rw 为读写

  • -v ~/soft/redis/etc/redis-6379.conf:/etc/redis-6379.conf:ro挂载配置文件 ro 为readonly

  • --privileged=true给与一些权限

  • --name iredis给容器起个名字

  • -d redis:5.0.4 redis-server /etc/redis-6379.conf deamon运行容器

    • 使用配置文件启动容器内的redis-server
    • /etc/redis-6379.conf是作用在iredis容器内的redis-server上的。
    • /etc/redis-6379.conf是容器iredis内部路径。

进入Shell

  • 进入redis Shell
  • https://redis.io/topics/rediscli

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    # docker exec --help
    # <CONTAINER-NAME> 进入iredis Bash
    $ sudo docker exec -it iredis bash
    
    # 进入redis Shell,在iredis容器内执行
    $ redis-cli
    
    # 上方两个命令可以连起来写
    $ sudo docker exec -it iredis redis-cli
    
    # 退出redis Shell
    $ exit
    #iredis Bash
    $ exit

停止容器

  • Docker容器停止redis

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    # 查看运行中的容器
    $ sudo docker ps
    
    # 如果没有iredis 说明启动失败 查看错误日志
    $ sudo docker logs iredis
    
    # 查看 iredis 的 ip 挂载 端口映射等信息
    $ sudo docker inspect iredis
    
    # 查看 iredis 的端口映射
    $ sudo docker port iredis
    
    # <CONTAINER ID> 根据容器ID停止容器
    $ sudo docker stop a632a7578b20
    
    # <CONTAINER NAMES> 根据容器名停止容器
    $ sudo docker stop iredis

Go客户端

  • https://redis.io/clients#go
  • https://github.com/gomodule/redigo/redis

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    package main
    
    import (
    	"fmt"
    	"github.com/gomodule/redigo/redis"
    )
    
    func main() {
    	// change xxxxxx to your redis's ip
    	conn, err := redis.Dial("tcp", "xxxxx:6379")
    	if err != nil {
    		fmt.Println("redis dial error : ", err)
    		return
    	}
    	defer conn.Close()
    	
    	reply, err := conn.Do("SET", "hello", "Redis")
    	fmt.Println(reply) // OK
      
    	reply, err = conn.Do("GET", "hello")
    	fmt.Println(string(reply.([]uint8))) // Redis
    }

链接