docker安装redis挂载容器卷同时开启持久化

目录

  • 一、安装
    • 1.搜索redis容器镜像并拉取容器镜像
    • 2.在宿主机本地创建redis存储配置文件和数据的目录,我这里创建/docker/redis下
    • 3.配置文件
    • 4.启动容器
  • 二、进入容器,指定配置文件启动redis服务
    • 1.启动redis服务
    • 2.指定6380端口登陆客户端
  • 三、删除容器后重新启动容器
    • 1.删除,然后查看宿主机目录下是否有持久化文件,查看这一步可以放在上一步后
    • 2.重启容器
说明:centOS操作系统,操作系统已安装过redis,端口6379已被占用。容器将会使用6380端口。本次操作为了实例化redis数据,并挂载到宿主机,防止容器被删除导致的数据丢失!

一、安装
1.搜索redis容器镜像并拉取容器镜像
[root@localhost]# docker search --limit 10 redis[root@localhost]# docker pull redis

【docker安装redis挂载容器卷同时开启持久化】
2.在宿主机本地创建redis存储配置文件和数据的目录,我这里创建/docker/redis下
[root@localhost redis]# pwd/docker/redis


3.配置文件
复制原有redis.conf到/docker/redis/目录下
修改配置(最重要主要4项:修改后台运行默认为no、端口、存放位置、开启持久化):
requirepass 123maxclients 10000#如果要外网访问,请注释掉下面,或者修改为0.0.0.0,保险起见,也可以把protected-mode设置为nobind 0.0.0.0protected-mode no#注意修改这里端口,根据你实际暴露端口情况配置port 6380tcp-backlog 511timeout 0tcp-keepalive 300#注意这里要把后台运行设置为no,避免docker后台运行冲突daemonize nosupervised nopidfile /docker/redis/redis.pidloglevel noticedatabases 16always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdb#注意修改这里的目录为容器内目录,默认reids进来是在/data/目录dir /data/replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush no#注意修改这里的配置,yes开启持久化,no关闭持久化appendonly yesappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes


4.启动容器
启动命令:docker run -p 6380:6380 --name forredis2 --privileged=true -v /docker/redis/redis.conf:/etc/redis/redis.conf -v /docker/redis/data:/data -d redis
效果如下:
[root@localhost]# docker run -p 6380:6380 --name forredis2 --privileged=true -v /docker/redis/redis.conf:/etc/redis/redis.conf -v /docker/redis/data:/data -d redisd536dd728243ccee23b78e0289e30f7ee25084d308766fb9aa317d691d0dea7d[root@localhost]# docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESd536dd728243redis"docker-entrypoint.s…"3 seconds agoUp 2 seconds6379/tcp, 0.0.0.0:6380->6380/tcp, :::6380->6380/tcpforredis2参数讲解:

参数介绍:
  • docker run -p 6380:6380 --name forredis2别名
  • --privileged=true挂载容器卷目录权限
  • -v /docker/redis/redis.conf[宿主机配置文件]:/etc/redis/redis.conf[容器配置文件]
  • -v /docker/redis/data[宿主机数据存储位置]:/data [容器数据存储位置]
  • -d redis[:版本号]

二、进入容器,指定配置文件启动redis服务
1.启动redis服务
[root@localhost data]# docker exec -it forredis2 /bin/bashroot@d536dd728243:/data# redis-server /etc/redis/redis.conf24:C 02 Jun 2022 02:42:56.096 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo24:C 02 Jun 2022 02:42:56.096 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=24, just started24:C 02 Jun 2022 02:42:56.096 # Configuration loaded24:M 02 Jun 2022 02:42:56.097 * monotonic clock: POSIX clock_gettime_.__.-``__ ''-.__.-```.`_.''-._Redis 6.2.6 (00000000/0) 64 bit.-`` .-```.```\/_.,_ ''-._ (',.-`| `,)Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'|Port: 6380 |`-._`._/_.-'|PID: 24`-._`-._`-./_.-'_.-' |`-._`-._`-.__.-'_.-'_.-'| |`-._`-.__.-'_.-'|https://redis.io`-._`-._`-.__.-'_.-'_.-' |`-._`-._`-.__.-'_.-'_.-'| |`-._`-.__.-'_.-'|`-._`-._`-.__.-'_.-'_.-'`-._`-.__.-'_.-'`-.__.-'`-.__.-'

如果要后台运行,将启动redis命令后加上&,即
redis-server /etc/redis/redis.conf &
“/etc/redis/redis.conf”为容器内配置文件,已通过启动容器时挂载到宿主机的/docker/redis/redis.conf

2.指定6380端口登陆客户端
root@ce16f8c4fd8c:/data# redis-cli -p 6380127.0.0.1:6380> auth 123OK127.0.0.1:6380> keys *(empty array)127.0.0.1:6380> set a 1OK127.0.0.1:6380> keys *1) "a"


三、删除容器后重新启动容器 为了验证redis持久化,删除容器后数据在宿主机不会丢失,我们尝试删除容器后重新启动

1.删除,然后查看宿主机目录下是否有持久化文件,查看这一步可以放在上一步后
[root@localhost ~]# docker rm -f forredis2forredis2[root@localhost ~]# docker ps -aCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESe28f2bd4b59eredis"docker-entrypoint.s…"10 hours agoExited (130) 10 hours agoexciting_yalow4e291d491cdaredis"docker-entrypoint.s…"10 hours agoExited (0) 10 hours agodreamy_rhodesbe3f2f06ed9fredis"docker-entrypoint.s…"12 hours agoExited (0) 12 hours agoawesome_jones9a206e517842redis"docker-entrypoint.s…"12 hours agoExited (0) 12 hours agohopeful_volhard69c9f429c98a7614ae9453d1"docker-entrypoint.s…"16 hours agoExited (1) 16 hours agoyouthful_goodall25f26d7892d5redis"docker-entrypoint.s…"18 hours agoExited (0) 16 hours agoamazing_lovelace[root@localhost ~]# docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES[root@localhost ~]# cd /docker/redis/data/[root@localhost data]# lsappendonly.aofdump.rdb


2.重启容器
[root@localhost data]# docker run -p 6380:6380 --name forredis2 --privileged=true -v /docker/redis/redis.conf:/etc/redis/redis.conf -v /docker/redis/data:/data -d redisd536dd728243ccee23b78e0289e30f7ee25084d308766fb9aa317d691d0dea7dc

重复第【二】步的操作,进入redis,查看数据是否存在
[root@localhost ~]# docker exec -it forredis2 /bin/bashroot@d536dd728243:/data# redis-cli -p 6380127.0.0.1:6380> auth 123127.0.0.1:6380> keys *1) "a"

数据存在,成功!
到此这篇关于docker安装redis挂载容器卷同时开启持久化的文章就介绍到这了,更多相关docker 安装redis挂载容器卷内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读