安装docker
详见:https://blog.youkuaiyun.com/qq_40261601/article/details/143570184
使用docker安装redis
方式一 通过镜像拉取(需要连网)
此种方式前提是允许访问外网,可通过ping 镜像域名查看是否满足条件。
ping vyryif4w.mirror.aliyuncs.com
1、拉取redis镜像
查看redis版本
docker search redis
使用如下命令安装指定版本或最新版本:
# 安装指定版本
docker pull redis:7.2.0
# 安装最新版本
docker pull redis
安装完成,查看本地镜像
docker images
2、创建redis配置文件
# 创建用来存放docker下配置文件的目录,比如我创建的自定义目录是/home/docker/redis
mkdir -p /home/docker/redis
#进入redis文件夹,在此位置创建或上传redis.conf文件
cd /home/docker/redis
#创建redis.conf
vi redis.conf
内容如下
# 监听的IP地址
# bind 127.0.0.1
bind 0.0.0.0
# 监听端口
port 6379
# 设置密码
requirepass 123654
# 保护模式
protected-mode yes
# 守护进程模式(docker此处必须为no,否则一启动就会停止)
daemonize no
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# TCP keepalive.
tcp-keepalive 300
# 日志等级
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
# 日志输出
logfile "server_log.txt"
syslog-enabled yes
# 设置数据库
databases 16
always-show-logo yes
# The working directory.
dir ./
# in order to get the desired effect.
tcp-backlog 511
# Specify the source name of the events in the Windows Application log.
syslog-ident redis
# Save the DB on disk:
# save <seconds> <changes>
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush 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
3、启动redis
以下为启动并生成容器的命令,运行后会在docker中生成一个redis容器,一般只需要一个容器,所以该命令只在第一次启动时使用,后续再启动可以用docker start id
启动容器。
# redis最新版本的启动命令
docker run --restart=always -p 6379:6379 --name redis -v /home/docker/redis/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf
# redis指定版本的启动命令
docker run --restart=always -p 6379:6379 --name redis -v /home/docker/redis/redis.conf:/etc/redis/redis.conf -d redis:指定版本 redis-server /etc/redis/redis.conf
–restart=always 总是开机启动
-p 6379:6379 将 6379 端口挂载出去
–name 给容器命名
-v 数据卷挂载,将自定义路径下的 redis.conf 和默认redis.conf 挂载在一起
-d redis 表示后台启动 redis
redis-server /etc/redis/redis.conf 以配置文件启动redis,加载容器内的 conf 文件,最终找到的是自定义的redis.conf
查看启动状态:
docker ps -a
STATUS为Up代表启动成功。
4、测试redis连接
# docker exec -it 容器名 redis-cli
docker exec -it redis redis-cli
#密码验证
auth 123654
报错及解决办法
若报错:
1、Error response from daemon: Get “https://index.docker.io/v1/search?q=redis&n=25”: dial tcp: lookup index.docker.io: i/o timeout
2、Error response from daemon: Get “https://registry-1.docker.io/v2/”: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
方法一:可能是不通外网,用ping www.baidu.com检测下;如果网络环境不允许,请使用离线安装。
方法二:新增镜像源
方法三:修改DNS配置文件
在 /etc/resolv.conf 目录下新增两个免费DNS地址 :wq 保存
vi /etc/resolv.conf
方式二 离线安装redis
1、获取redis的docker镜像
服务器不能联网的情况下,只能先获取redis的docker镜像文件,然后上传至服务器安装。
获取方法有两种:
1、直接在网上搜索需要的redis的docker镜像版本进行下载;
2、先找一台可以联网的linux机器,安装docker,使用方式一docker pull redis:版本
获取镜像,然后docker save
保存镜像文件到指定目录,再将该文件上传至离线服务器。步骤如下:
①在联网环境安装docker后拉取redis指定版本
# 获取镜像
docker pull redis:7.2.0
# 查看镜像
docker images
②在联网环境导出镜像
docker save -o [输出的文件路径] [REPOSITORY]:[TAG]
-o : 指定output输出的路径
[REPOSITORY]: docker images展示的名称
[TAG]: docker images展示的标签
# 导出镜像tar文件到/home/docker/Redis7文件夹下
docker save -o /home/docker/Redis7/redis7.tar redis:7.2.0
2、在离线服务器上传加载redis镜像
在离线服务器新建文件夹,上传刚刚生成的镜像文件
mkdir -p /home/docker/Redis7
cd /home/docker/Redis7
在该文件夹下用docker命令加载镜像
# 给文件授权
chmod -R 777 redis7.tar
# 加载镜像
docker load -i ./redis7.tar
3、创建配置文件
同方式一
4、启动redis
同方式一 ,这里我使用指定版本(7.2.0)的命令
# redis指定版本的启动命令
docker run --restart=always -p 6379:6379 --name redis -v /home/docker/redis/redis.conf:/etc/redis/redis.conf -d redis:7.2.0 redis-server /etc/redis/redis.conf
5、测试redis连接
同方式一
附录 docker卸载删除redis
# 查看容器
docker ps
# 停止容器
docker stop <CONTAINERID>
# 删除容器
docker rm <CONTAINERID>
# 查看镜像
docker images
# 删除镜像
docker rmi <IMAGEID>
参考文章:
https://xie.infoq.cn/article/d4c03cbec3c1735f0535a30b6
https://blog.youkuaiyun.com/weixin_47316183/article/details/131987609