docker安裝redis掛載容器卷同時開啟持久化

說明:centOS操作系統,操作系統已安裝過redis,端口6379已被占用。容器將會使用6380端口。本次操作為瞭實例化redis數據,並掛載到宿主機,防止容器被刪除導致的數據丟失!

一、安裝

1.搜索redis容器鏡像並拉取容器鏡像

[[email protected]]# docker search --limit 10 redis
[[email protected]]# docker pull redis

2.在宿主機本地創建redis存儲配置文件和數據的目錄,我這裡創建/docker/redis下

[[email protected] redis]# pwd
/docker/redis

3.配置文件

復制原有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

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

效果如下:

[[email protected]]# 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
[[email protected]]# 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[:版本號]

二、進入容器,指定配置文件啟動redis服務

1.啟動redis服務

[[email protected] data]# docker exec -it forredis2 /bin/bash
[email protected]:/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

2.指定6380端口登陸客戶端

[email protected]:/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持久化,刪除容器後數據在宿主機不會丟失,我們嘗試刪除容器後重新啟動

1.刪除,然後查看宿主機目錄下是否有持久化文件,查看這一步可以放在上一步後

[[email protected] ~]# docker rm -f forredis2
forredis2
[[email protected] ~]# 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
[[email protected] ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[[email protected] ~]# cd /docker/redis/data/
[[email protected] data]# ls
appendonly.aof  dump.rdb

2.重啟容器

[[email protected] 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,查看數據是否存在

[[email protected] ~]# docker exec -it forredis2 /bin/bash
[email protected]:/data# redis-cli -p 6380
127.0.0.1:6380> auth 123
127.0.0.1:6380> keys *
1) "a"

數據存在,成功!

到此這篇關於docker安裝redis掛載容器卷同時開啟持久化的文章就介紹到這瞭,更多相關docker 安裝redis掛載容器卷內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: