08.docker安装Redis

/ Docker / 没有评论 / 810浏览

指定容器卷启动

命令

docker run -p 6379:6379 --name mr1 --privileged=true -v /Users/huzd/docker/redis/redis.conf:/etc/redis/redis.conf -v /Users/huzd/docker/redis/data:/data -d redis:6.0.8 redis-server /etc/redis/redis.conf

docker run -it -p6379:6379 --name mr2 --privileged=true -v /Users/huzd/docker/redis/redis.conf:/etc/redis/redis.conf -v /Users/huzd/docker/redis/data:/data  redis:6.0.8 redis-server /etc/redis/redis.conf

实操

  1. 在本地创建文件目录分别用来存放redis配置文件以及数据

    mkdir /Users/huzd/docker/redis
    mkdir /Users/huzd/docker/redis/data
    
  2. 创建外置配置文件

    #官方下载默认的配置文件
    #https://redis.io/docs/manual/config/ 
    #根据版本选择
    #修改以下配置项
    
    # bind 127.0.0.1
    daemonize no  #这里千万不要设置成yes否则启动会失败,因为这个问题浪费了5个小时解决。
    protected-mode no
    databases 5
    
  3. 使用脚本启动

    docker run -p 6379:6379 --name mr1 --privileged=true -v /Users/huzd/docker/redis/redis.conf:/etc/redis/redis.conf -v /Users/huzd/docker/redis/data:/data -d redis:6.0.8 redis-server /etc/redis/redis.conf
    
  4. 登录容器校验

    ➜  docker docker ps
    CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                    NAMES
    ae78a65f3231   redis:6.0.8   "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:6379->6379/tcp   mr1
    ➜  docker docker exec -it ae78a65f3231 /bin/bash 
    root@ae78a65f3231:/data# redis-cli
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379> select 6
    (error) ERR DB index is out of range
    127.0.0.1:6379> select 5
    (error) ERR DB index is out of range
    127.0.0.1:6379> select 4
    OK
    127.0.0.1:6379[4]>