码
我正在尝试运行在docker-compose.yml
中定义的redis服务,如下所示:
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
接口(interface)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
的请求时通过redis保存一对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.
而且我还尝试如下在redis服务中映射dump.rdb:
redis:
image: "redis:5-alpine"
volumes:
- ./redis-vol/dump.rdb:/home/data/dump.rdb
但是 docker 会创建一个名为
dump.rdb/
的文件夹,而不是一个可读文件。 最佳答案
根据DockerHub页面上的redis文档
因此,您使用了错误的卷路径。您应该改用/data
volumes:
- ./redis-vol:/data
关于docker - 为什么保存Redis后卷文件夹为空?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53485205/