Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:




like image 635
Aleksei Khatkevich Avatar asked Sep 15 '20 17:09

Aleksei Khatkevich


1 Answers

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

volumes:
    postgres_data:
    redis_data:

Then change host path to the 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.

like image 53
anemyte Avatar answered Sep 25 '22 10:09

anemyte