首页 > 解决方案 > 为什么redis保存后卷文件夹为空?

问题描述

代码

我正在尝试运行在 a 中定义的 redis 服务docker-compose.yml,如下所示:

version: '3'
services:
  redis:
    image: "redis:5-alpine"
    volumes:
      - ./redis-vol:/home/data 
  app:
    build: .
    ports:
      - 8080:8080
    volumes:
      - .:/home/app/

这是Dockerfile

FROM python:2.7-alpine3.8
WORKDIR /home/app
COPY ./requirements.txt .
RUN apk add python2-dev build-base linux-headers pcre-dev && \
    pip install -r requirements.txt
# Source files
COPY ./api.py . 
COPY ./conf.ini .
CMD ["uwsgi", "--ini", "conf.ini"]

该应用程序包含uwsgi在端口上运行接口的此代码段8080

import uwsgi
import redis
r = redis.Redis('redis')
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    r.append('hello', 'world!')
    r.save()
    return [b"Hello World"]

这是 conf.ini 文件:

[uwsgi]
http = :8080 
wsgi-file = api.py 
master = true
process = 2
enable-threads = true
uid = 1001
gid = 1001

应用服务应该在key:value每次收到对http://localhost:8080. 请求成功后,docker-compose 进程会返回以下日志:

redis_1_bdf757fbb2bf | 1:M 26 Nov 2018 15:38:20.399 * DB saved on disk
app_1_5f729e6bcd36   | [pid: 17|app: 0|req: 1/1] 172.21.0.1 () {38 vars in 690 bytes} [Mon Nov 26 15:38:20 2018] GET / => generated 11 bytes in 8 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1 switches on core 0)
redis_1_bdf757fbb2bf | 1:M 26 Nov 2018 15:38:20.998 * DB saved on disk
app_1_5f729e6bcd36   | [pid: 17|app: 0|req: 2/2] 172.21.0.1 () {40 vars in 691 bytes} [Mon Nov 26 15:38:20 2018] GET /favicon.ico => generated 11 bytes in 4 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1 switches on core 0)

问题

尽管有DB saved on disk日志,但该redis_vol文件夹是空的,并且 dump.rdb 文件似乎没有保存在其他任何地方。

我做错了什么?我也尝试过redis:alpine用作图像,但在启动时出现以下错误:

redis_1_bdf757fbb2bf | 1:M 26 Nov 14:57:27.003 # Can't handle RDB format version 9
redis_1_bdf757fbb2bf | 1:M 26 Nov 14:57:27.003 # Fatal error loading the DB: Invalid argument. Exiting.

而且我还尝试将dump.rdb映射到redis服务中,如下所示:

  redis:
    image: "redis:5-alpine"
    volumes:
      - ./redis-vol/dump.rdb:/home/data/dump.rdb

但是泊坞窗创建了一个名为dump.rdb/而不是可读文件的文件夹。

标签: dockerredisdocker-compose

解决方案


根据DockerHub页面上的redis文档

如果启用了持久性,则数据存储在 VOLUME /data

所以你使用了错误的卷路径。你应该/data改用

volumes:
    - ./redis-vol:/data 

推荐阅读