Keep Redis data alive between docker-compose down and up in Docker container

回眸只為那壹抹淺笑 提交于 2021-01-29 05:00:32

问题


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:





回答1:


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.



来源:https://stackoverflow.com/questions/63906856/keep-redis-data-alive-between-docker-compose-down-and-up-in-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!