今天小编给大家分享一下docker怎么安装redis挂载容器卷同时开启持久化的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
说明:centOS操作系统,操作系统已安装过redis,端口6379已被占用。容器将会使用6380端口。本次操作为了实例化redis数据,并挂载到宿主机,防止容器被删除导致的数据丢失!
[root@localhost]# docker search --limit 10 redis [root@localhost]# docker pull redis
[root@localhost redis]# pwd /docker/redis
复制原有redis.conf到/docker/redis/目录下
修改配置(最重要主要4项:修改后台运行默认为no、端口、存放位置、开启持久化):
requirepass 123 maxclients 10000 #如果要外网访问,请注释掉下面,或者修改为0.0.0.0,保险起见,也可以把protected-mode设置为no bind 0.0.0.0 protected-mode no #注意修改这里端口,根据你实际暴露端口情况配置 port 6380 tcp-backlog 511 timeout 0 tcp-keepalive 300 #注意这里要把后台运行设置为no,避免docker后台运行冲突 daemonize no supervised no pidfile /docker/redis/redis.pid loglevel notice 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 #注意修改这里的目录为容器内目录,默认reids进来是在/data/目录 dir /data/ 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 #注意修改这里的配置,yes开启持久化,no关闭持久化 appendonly yes 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
启动命令: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 redis d536dd728243ccee23b78e0289e30f7ee25084d308766fb9aa317d691d0dea7d [root@localhost]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d536dd728243 redis "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 6379/tcp, 0.0.0.0:6380->6380/tcp, :::6380->6380/tcp forredis2参数讲解:
参数介绍:
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 data]# docker exec -it forredis2 /bin/bash root@d536dd728243:/data# redis-server /etc/redis/redis.conf 24:C 02 Jun 2022 02:42:56.096 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 24:C 02 Jun 2022 02:42:56.096 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=24, just started 24:C 02 Jun 2022 02:42:56.096 # Configuration loaded 24: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
root@ce16f8c4fd8c:/data# redis-cli -p 6380 127.0.0.1:6380> auth 123 OK 127.0.0.1:6380> keys * (empty array) 127.0.0.1:6380> set a 1 OK 127.0.0.1:6380> keys * 1) "a"
为了验证redis持久化,删除容器后数据在宿主机不会丢失,我们尝试删除容器后重新启动
[root@localhost ~]# docker rm -f forredis2 forredis2 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e28f2bd4b59e redis "docker-entrypoint.s…" 10 hours ago Exited (130) 10 hours ago exciting_yalow 4e291d491cda redis "docker-entrypoint.s…" 10 hours ago Exited (0) 10 hours ago dreamy_rhodes be3f2f06ed9f redis "docker-entrypoint.s…" 12 hours ago Exited (0) 12 hours ago awesome_jones 9a206e517842 redis "docker-entrypoint.s…" 12 hours ago Exited (0) 12 hours ago hopeful_volhard 69c9f429c98a 7614ae9453d1 "docker-entrypoint.s…" 16 hours ago Exited (1) 16 hours ago youthful_goodall 25f26d7892d5 redis "docker-entrypoint.s…" 18 hours ago Exited (0) 16 hours ago amazing_lovelace [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# cd /docker/redis/data/ [root@localhost data]# ls appendonly.aof dump.rdb
[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 redis d536dd728243ccee23b78e0289e30f7ee25084d308766fb9aa317d691d0dea7dc
重复第【二】步的操作,进入redis,查看数据是否存在
[root@localhost ~]# docker exec -it forredis2 /bin/bash root@d536dd728243:/data# redis-cli -p 6380 127.0.0.1:6380> auth 123 127.0.0.1:6380> keys * 1) "a"
数据存在,成功!
以上就是“docker怎么安装redis挂载容器卷同时开启持久化”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。