LLLeon / Blog

LLLeon 的部落格
15 stars 4 forks source link

在 CentOS 7 环境下用 Docker 运行 Redis 服务 #10

Open LLLeon opened 6 years ago

LLLeon commented 6 years ago

这里简单介绍使用 Docker Compose 来运行 Redis 服务,并通过挂载卷进行数据持久化。关于 Docker Compose,建议看官方文档

安装 Docker Compose

这一部分的前提是已经安装好了 Docker。

先安装 EPEL 库:

yum install epel-release

然后安装 python-pip:

yum install -y python-pip

使用 pip 安装 Docker Compose:

pip install docker-compose

升级 CentOS 7 上的所有 Python 包:

yum upgrade python*

检查 Docker Compose 版本,验证是否成功安装:

docker-compose -v

如果打印出了如下信息,说明完成安装:

docker'compose version 1.16.1, build 6d1ac219

Docker Compose 文件

在 docker-compose.yml 文件中定义容器如何运行:

version: '3'
services:
  Redis:
    image: "redis:5.0-rc"
    container_name: redis
    command: /usr/local/etc/redis/redis.conf
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis/data:/data
    ports:
      - "127.0.0.1:6379:6379"
    privileged: true
    restart: always

对以上内容简单说明:

运行容器

执行命令 docker-compose up -d,运行 redis 容器。

由于之前没有看 dockerfile 的内容,是按照 macOS 中的目录 /usr/local/var/db/redis 挂载的持久卷,执行此命令后,容器总是重启,查看日志发现以下信息:

[offset 0] Unexpected EOF reading RDB file
[additional info] While doing: start
[additional info] Reading type 0 (string)
[info] 0 keys read
[info] 0 expires
[info] 0 already expired
1:C 14 Aug 2018 04:22:25.052 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 14 Aug 2018 04:22:25.052 # Redis version=4.9.104, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 14 Aug 2018 04:22:25.052 # Configuration loaded
1:M 14 Aug 2018 04:22:25.053 * Running mode=standalone, port=6379.
1:M 14 Aug 2018 04:22:25.053 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 14 Aug 2018 04:22:25.053 # Server initialized
1:M 14 Aug 2018 04:22:25.053 # Short read or OOM loading DB. Unrecoverable error, aborting now.
1:M 14 Aug 2018 04:22:25.053 # Internal error in RDB reading function at rdb.c:2055 -> Unexpected EOF reading RDB file
[offset 0] Checking RDB file dump.rdb

后来查看 dockerfile 文件,其中 VOLUMEWORKDIR 指定的目录是 /data。修改后,停止并删除容器,删除宿主机 ./redis/data 目录中的内容,再重新运行容器,正常工作。