在Docker容器中上下移动docker-compose之间保持Redis数据活动 [英] Keep Redis data alive between docker-compose down and up in Docker container

查看:41
本文介绍了在Docker容器中上下移动docker-compose之间保持Redis数据活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是关于使 Redis 数据在 docker-compose up docker-compose down之间保持活动.

docker-compose.yaml 文件中,以下 db 服务使用- postgres_data:/var/lib/postgresql/data/卷使数据保持活动状态.我想对 redis 服务做这样的事情,但是我找不到可行的解决方案.我设法实现此目标的唯一方法是将数据存储在本地存储中.

问题是-是否可以像在 DB <中创建的一样,以某种方式在 docker-compose down docker-compose up 之间存储redis数据./code>服务?

很抱歉,问题是否幼稚……

谢谢

 版本:"3.8"服务:网络:建造: .命令:python/code/manage.py运行服务器0.0.0.0:8000env_file:-./series/.env数量:- .:/代码端口:-8000:8000取决于:- D b-重做D b:建造:语境: .dockerfile:postgres.dockerfile重启:总是env_file:-./series/.env环境:-POSTGRES_DB = postgres-POSTGRES_USER = postgres-POSTGRES_PASSWORD = 1q2w3e数量:-postgres_data:/var/lib/postgresql/data/端口:-目标:5432发表:5433通讯协定:tcp模式:主机Redis:图片:redis:alpine命令:redis-server --appendonly是端口:-目标:6379发行时间:6380通讯协定:tcp模式:主机数量:-./storage/redis/data:/data重启:总是环境:-REDIS_REPLICATION_MODE =主数量:postgres_data: 

解决方案

您只需要在 postgres_data 旁边添加Redis数据的命名卷:

 卷:postgres_data:redis_data: 

然后将主机路径更改为命名卷:

  redis:...数量:-redis_data:/数据 

如果Redis使用主机路径保存了数据,则以上内容将为您工作.我提到这一点是因为您必须将Redis配置为启用持久存储(请参阅Redis Docker Hub页面 https://hub.docker.com/_/redis ).

当心,运行 docker-compose down -v 也会破坏卷.

Question is about keeping Redis data alive between docker-compose up and docker-compose down.

In the docker-compose.yaml file bellow db service uses - postgres_data:/var/lib/postgresql/data/ volume to keep data alive. I would like to do something like this for redis service but I can not find workable solution to do so. Only one way I have managed to achieve this goal is to store data in local storage - ./storage/redis/data:/data. All experiments with external volume gave no results.

Question is -is it possible somehow to store redis data between docker-compose down and docker-compose up in a volume likewise it made in DB service?

Sorry if question is naive…

Thanks

version: '3.8'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    env_file:
      - ./series/.env
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
      - redis

  db:
    build:
      context: .
      dockerfile: postgres.dockerfile
    restart: always
    env_file:
      - ./series/.env
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=1q2w3e
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    ports:
      - target: 5432
        published: 5433
        protocol: tcp
        mode: host

  redis:
    image: redis:alpine
    command: redis-server --appendonly yes
    ports:
      - target: 6379
        published: 6380
        protocol: tcp
        mode: host
    volumes:
        - ./storage/redis/data:/data
    restart: always
    environment:
      - REDIS_REPLICATION_MODE=master

volumes:
    postgres_data:




解决方案

You just need to add a named volume for Redis data next to postgres_data:

volumes:
    postgres_data:
    redis_data:

Then change host path to named volume:

  redis:
    ...
    volumes:
        - redis_data:/data

If Redis saved data with host path, then the above will work for you. I mention that because you have to configure Redis to enable persistent storage (see Redis Docker Hub page https://hub.docker.com/_/redis).

Beware, running docker-compose down -v will destroy volumes as well.

这篇关于在Docker容器中上下移动docker-compose之间保持Redis数据活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆