Docker环境下三种方式安装redis并配置redis-cli远程连接

本文不再更新,可能存在内容过时的情况,实时更新请访问原地址:Docker环境下三种方式安装redis并配置redis-cli远程连接

本文不再更新,可能存在内容过时的情况,实时更新请访问原地址:Docker环境下三种方式安装redis并配置redis-cli远程连接

本文不再更新,可能存在内容过时的情况,实时更新请访问原地址:Docker环境下三种方式安装redis并配置redis-cli远程连接

这里记录使用docker和docker-compose两种方式安装redis的过程:

一、准备

1.安装docker-ce

安装过程参考: CentOS安装docker ce的三种方式

2.安装docker-compse

安装过程参考: CENTOS安装Docker Compose

3.准备配置文件

在官网找个配置文件示例: http://download.redis.io/redis-stable/redis.conf,然后保存到本地,比如我的路径是:/home/winbert/redis-pkg/redis.conf

配置文件样例:

[winbert@winbert-server redis-pkg]$ cat redis.conf |grep -v ^# | grep -v ^$
bind 0.0.0.0 
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

主要是根据官方文件修改了绑定IP为0.0.0.0

二、安装

1.使用docker安装

1)拉取镜像


docker pull redis:latest
docker run --name some-redis -p 6379:6379 -v /home/winbert/redis-pkg/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf

2.Dockerfile安装

新建Dockerfile,内容如下:

FROM redis
COPY redis.conf /etc/redis/redis.conf
CMD [ "redis-server", "/etc/redis/redis.conf" ]

运行:

docker build -t some-redis .

docker image ls -a


#基于some-redis运行
docker run --name some-redis -p 6379:6379 -d some-redis

3.使用docker-compose安装

新建一个/home/winbert/redis-pkg/docker-compose.yml文件,内容如下:

version: '3'
services:
    redis:
      image: redis:latest
      container_name: redis
      ports:
        - "6379:6379"
      volumes:
        - /home/winbert/redis-pkg/redis.conf:/etc/redis/redis.conf 
      command: redis-server /etc/redis/redis.conf 
      privileged: true

然后启动:

docker-compose up -d

三、连接

[winbert@winbert-server redis-pkg]$ docker exec -it some-redis redis-cli
127.0.0.1:6379> set url www.4spaces.org
OK
127.0.0.1:6379> get url
"www.4spaces.org"
127.0.0.1:6379> exit

Windows可以使用图形界面进行连接: Medis

posted @ 2020-07-18 22:04  Cobcmw  阅读(3532)  评论(0编辑  收藏  举报