Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Stack/Compose Redis Instance Not Persisting Data

Tags:

docker

redis

I'm trying to use Redis as a cache in my Sails.js application, but am having a hard time getting the volumes to work correctly and to get the Redis instance to persist the data automatically.

Here's (the relevant part of) my stack.yml file:

redis:
  image: 'redis:latest'
  command: redis-server --appendonly yes
  command: ['redis-server', '--requirepass', 'mypasswordgoeshere']
  volumes:
    - ./redis-data:/data
  deploy:
    replicas: 1

I have a Sails.js service and an Angular app service as well, but didn't show them as they aren't really needed here.

The above doesn't work exactly as expected out of the box. I expected Docker to create the ./redis-data folder for me automatically if not present, but it doesn't. So I had to create the folder and make sure the permissions were set correctly. Then I expected the Redis container to persist its data to an appendonly.aof file and have that automatically be saved periodically. That's what happens on my local machine when I tried this.

However, on the server, it doesn't save the appendonly.aof file for me, and thus the data I want to persist doesn't get saved so that it's there when the stack is restarted.

The weirdest part of this is that I created a volume for the Sails.js app to get to the log files, and I didn't need to create the folder for the application; it just created it and everything worked as expected on its own. Am I missing something obvious here? Any help would be greatly appreciated. Thanks!

EDIT

Also, if I go in to the container (docker exec -it container_id /bin/bash, there's nothing in that /data folder that it drops you in. Which explains why there's nothing there on the host machine either.

like image 329
pjlamb12 Avatar asked Aug 07 '26 14:08

pjlamb12


2 Answers

For anyone using Redis Stack, what resolved my problem was adding the following

environment:
    REDIS_ARGS: --save 20 1

I assume any values for the parameters, or using the AOF mode, would also work, but I have not verified this.

I initially tried specifying this as

command: redis-server --save 20 1

which was based on this tutorial. However, this resulted in a ConnectionError("Connection is not ready") (source) when running docker-compose up.

Unfortunately, declaring REDIS_ARGS env variables did not work for me. What worked was specifying --dir folder for redis-server and adding persistence method directly in the redis-server commmand. I am using both AOF & RDB methods for persistence. The full command is: redis-server --dir /data --requirepass $REDIS_PASSWORD --save 60 1 --appendonly yes.

Also, if you need to load modules, use the command redis-server --dir /data --requirepass $REDIS_PASSWORD --save 60 1 --appendonly yes --loadmodule /opt/redis-stack/lib/redisearch.so --loadmodule /opt/redis-stack/lib/rejson.so

Here is the docker compose version(3)

    services:
      localredis:
        image: redis/redis-stack-server:latest
        container_name: rediscontainer
        # environment:
        #   REDIS_ARGS: --save 60 1 --appendonly yes
        command: redis-server --dir /data --requirepass $REDIS_PASSWORD --save 60 1 --appendonly yes
        ports:
          - 6379:6379
        volumes:
          - redis_data:/data
        healthcheck:
          test: ["CMD", "redis-cli", "-h", "localhost", "-p", "6379", "ping"]
          interval: 2s
          timeout: 1m30s
          retries: 5
          start_period: 5s
        restart: unless-stopped
    volumes:
      redis_data:
like image 33
Deepak Avatar answered Aug 10 '26 12:08

Deepak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!