不使用官方镜像安装配置redis docker,并挂载外部数据目录的方案

0. 前言

本文目标是:
(1)建立一个docker镜像,里面能够运行redis-server,并单独维护一个redis数据库(即进入容器的时候无需指定```-p 6379:6379```)。
(2)这样我在本容器内可以运行自己的client程序,对数据进行存删操作。
(3)同时电脑断电或者其他不可抗力导致docker容器中断时,在docker容器内进行增删的数据库能够备份和恢复。

1. 拉取镜像ubuntu:16.04

# 先进入root用户,这样方便些
root@PCname:/home# docker pull ubuntu:16.04

2. 创建并进入容器, 假设本容器SHA码为831b7adc7d9f

root@PCname:/home# docker run -it ubuntu:16.04 bash
root@831b7adc7d9f:/#

3. 下载redis-server

root@831b7adc7d9f:/# apt-get update
root@831b7adc7d9f:/# apt-get install redis-server

4. 在一个肯定有读写权限的目录里创建自己的数据文件夹用以存放redis数据库备份文件,这里假设在/home下创建目录redis-data/来存放数据备份文件(*.rdb)

root@831b7adc7d9f:/# cd home
root@831b7adc7d9f:/home# mkdir redis-data

5. 进入redis配置文件redis.conf(一般在/etc/redis/redis.conf)改写配置

root@831b7adc7d9f:/home# apt-get install vim #先下载vim用来打开和编辑配置文件
root@831b7adc7d9f:/home# vim /etc/redis/redis.conf #打开配置文件

找到以下段落:

# The filename where to dump the DB
dbfilename dump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis/

键入i, 进入INSERT插入模式,并将最后一行dir /var/lib/redis/改写成:

dir /home/redis-data/

假如希望改动redis更新备份文件的频次,需要找到以下段落:

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

需要改动最后三行来改变备份文件更新频次。
这三行含义如下:

save 900 1     #每900s若产生1次改动就进行一次备份
save 300 10    #每300s若产生10次改动就进行一次备份
save 60 10000  #每60s若产生10000次改动就进行一次备份

然后键入Esc, 并输入:wq进行保存退出。

6. 另外开启一个命令行窗口(进入root账户),对刚刚的容器进行提交,然后退出并删除刚刚的容器

root@PCname:/home# docker commit 831b7adc7d9f myredis:1.0  # docker commit 容器号 镜像名

ctrl+D退出容器

root@831b7adc7d9f:/home# exit
root@PCname:/home# docker rm 831 # 删除容器831b7adc7d9f

7. 在HOST主机上建立运行容器时的数据管理目录,假设在/home/myredis/下管理数据库文件

root@PCname:/home# mkdir myredis
root@PCname:/home# cd myredis   #进入该数据管理目录
root@PCname:/home/myredis#

因为实际部署时,可能还需要管理redis外其他的数据,因此在本目录下另外创建redis-data/专门用来管理redis的数据库备份文件。

root@PCname:/home/myredis# mkdir redis-data
root@PCname:/home/myredis# ls
redis-data
root@PCname:/home/myredis#

8-1. 每次创建并执行容器:进入容器后,首先执行配置文件,并启动redis-server

/home/myredis目录下,进入新创建的镜像的容器中:

root@PCname:/home/myredis# docker run -it --privileged=true --volume=$PWD/redis-data/:/home/redis-data/ myredis:1.0 bash
root@63ccc5448b73:/# redis-server /etc/redis/redis.conf #执行配置文件
root@63ccc5448b73:/# /etc/init.d/redis-server start #启动redis-server
root@63ccc5448b73:/# ... #运行自己的client程序, 比如python中利用redis对数据进行存删

8-2. 每次创建并执行容器:直接在HOST主机目录下创建容器并运行内部程序(假设运行内部程序的指令为 python /home/app.py):

/home/myredis目录下,执行:

root@PCname:/home/myredis# docker run -it --privileged=true --volume=$PWD/redis-data/:/home/redis-data/ myredis:1.0 sh -c "redis-server /etc/redis/redis.conf && /etc/init.d/redis-server start && python /home/app.py"

... #程序运行时的命令行输出

8-1、8-2方式执行容器,能完成断电备份、数据不丢失;每次这样启动容器后,均能拿到上次断电前的数据。

附:以上docker run参数说明

参数 说明
-it 表示以“交互模式”运行容器,并且启动后会进入其命令行
–privileged=true 使容器内的root拥有外部root权限
$PWD 当前目录路径
–volume=a:b 映射外部路径a至内部路径b(会同步该两个路径下的文件)
bash、sh ENTRYPOINT, 进入容器时的第一个执行的程序
sh -c “a && b” -c是command,意思是命令行上依次运行a、b

猜你喜欢

转载自blog.csdn.net/weixin_36047799/article/details/90291865