简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM