Open LLLeon opened 6 years ago
这里简单介绍使用 Docker Compose 来运行 Redis 服务,并通过挂载卷进行数据持久化。关于 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.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
对以上内容简单说明:
version:Compose 文件格式版本号。
services:要构建的服务名称,这里是 Redis,即生产环境中的容器。服务只运行一个镜像,它规定镜像运行的方式 - 应该使用哪些端口,应该运行多少个容器副本,等等。
image:指定从哪个镜像构建此容器,这里使用的是官方镜像。
container_name:容器名称。
command:容器启动后执行的命令。这里是指定配置文件,虽然官方镜像的 dockerfile 里面已经指定了 CMD 为 redis-server,这里指定配置文件位置的命令会覆盖 CMD 的命令。
CMD
redis-server
看了下 dockerfile 中指定的 docker-entrypoint.sh 文件,里面运行了 redis-server 命令,command 标签后面的命令会追加到此命令。最终效果就是执行了:redis-server /usr/local/etc/redis/redis.conf 命令。
docker-entrypoint.sh
redis-server /usr/local/etc/redis/redis.conf
volumes:
第一行是将宿主机上指定的配置文件挂载到 redis 默认的配置文件上以覆盖它。
第二行是将容器中的目录映射到指定的宿主机目录,以做数据持久化。
ports:将容器的 6379 端口映射到宿主机对应端口,以便容器外部访问。
restart:容器总是会重启。
执行命令 docker-compose up -d,运行 redis 容器。
docker-compose up -d
由于之前没有看 dockerfile 的内容,是按照 macOS 中的目录 /usr/local/var/db/redis 挂载的持久卷,执行此命令后,容器总是重启,查看日志发现以下信息:
/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 文件,其中 VOLUME 和 WORKDIR 指定的目录是 /data。修改后,停止并删除容器,删除宿主机 ./redis/data 目录中的内容,再重新运行容器,正常工作。
VOLUME
WORKDIR
/data
./redis/data
这里简单介绍使用 Docker Compose 来运行 Redis 服务,并通过挂载卷进行数据持久化。关于 Docker Compose,建议看官方文档。
安装 Docker Compose
这一部分的前提是已经安装好了 Docker。
先安装 EPEL 库:
然后安装 python-pip:
使用 pip 安装 Docker Compose:
升级 CentOS 7 上的所有 Python 包:
检查 Docker Compose 版本,验证是否成功安装:
如果打印出了如下信息,说明完成安装:
Docker Compose 文件
在 docker-compose.yml 文件中定义容器如何运行:
对以上内容简单说明:
version:Compose 文件格式版本号。
services:要构建的服务名称,这里是 Redis,即生产环境中的容器。服务只运行一个镜像,它规定镜像运行的方式 - 应该使用哪些端口,应该运行多少个容器副本,等等。
image:指定从哪个镜像构建此容器,这里使用的是官方镜像。
container_name:容器名称。
command:容器启动后执行的命令。这里是指定配置文件,虽然官方镜像的 dockerfile 里面已经指定了
CMD
为redis-server
,这里指定配置文件位置的命令会覆盖CMD
的命令。看了下 dockerfile 中指定的
docker-entrypoint.sh
文件,里面运行了redis-server
命令,command 标签后面的命令会追加到此命令。最终效果就是执行了:redis-server /usr/local/etc/redis/redis.conf
命令。volumes:
第一行是将宿主机上指定的配置文件挂载到 redis 默认的配置文件上以覆盖它。
第二行是将容器中的目录映射到指定的宿主机目录,以做数据持久化。
ports:将容器的 6379 端口映射到宿主机对应端口,以便容器外部访问。
restart:容器总是会重启。
运行容器
执行命令
docker-compose up -d
,运行 redis 容器。由于之前没有看 dockerfile 的内容,是按照 macOS 中的目录
/usr/local/var/db/redis
挂载的持久卷,执行此命令后,容器总是重启,查看日志发现以下信息:后来查看 dockerfile 文件,其中
VOLUME
和WORKDIR
指定的目录是/data
。修改后,停止并删除容器,删除宿主机./redis/data
目录中的内容,再重新运行容器,正常工作。