ewerttrewert
ewerttrewert

Reputation: 1

Redis doesn't persist data between restarting Docker containers

I have built flask REST API that has connection to mysql database via mysql.connector python, i decided to implement caching and use redis for this, i containerized my services with docker and manage it with docker compose, so api is a separate container just like database and redis as well. Everything works like a charm except one fact - persisting data between restarts of my containers so between (docker-compose up / docker-compose down) commands. I would like to persist data between restarts of those containers, in order to achieve this based on: text, i have two options:

  1. AOF
  2. RDB

I decided to go with AOF. This is my docker-compose.yml file:

services:
  db:
    image: mysql:8.0
    volumes:
      - mysql_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    ports:
      - "3306:3306"

  flaskapp:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db
      - redis
    environment:
      - FLASK_APP=app.py
      - FLASK_RUN_HOST=0.0.0.0
    volumes:
      - .:/app
  
  redis:
    image: redis/redis-stack:latest
    restart: always
    volumes:
      - redis-data:/data
    ports:
      - "6379:6379"
      - "8001:8001"
    command: ["redis-server", "--appendonly", "yes"]
      

volumes:
  mysql_data:
  redis-data:

also beneath there is an endpoint i created for the purpose of this question, it doesn't really matter what logic stands behind it except from setting and getting keys, just to let you know one more time everything works, except persisting data with my original api source code as well, but with this particular line, everything goes south:

command: ["redis-server", "--appendonly", "yes"]

i know i have to create volume where my AOF file will be placed after building up the container, i checked and it's there but obviously empty, also i cannot ping my redis container

redis = Redis(
    host=os.getenv("REDIS_HOST"),
    port=os.getenv("REDIS_PORT"),
    decode_responses=True,
    health_check_interval=30,
)


class HelloWorld(Resource):
    def get(self):
        try:
            redis.ping()
            redis.set("imie", "maciek")
            print("success, redis connected!, random key created")
        except Exception as e:
            print(f"Error connecting to redis: {e}")

        redis.incr("hits")
        counter = str(redis.get("hits"), "utf-8")
        ki = str(redis.get("imie"), "utf-8")
        return (
            "Welcome to this webapage!, This webpage has been viewed "
            + counter
            + " time(s)"
            + ki
        )

so if i changed the line(command: ["redis-server", "--appendonly", "yes"]) with command tag inside my docker-compose.yml(remove it completely) everything works,containers are connected to each other and the only thing is that data is not persisted between restarts of containers. From what i have read i know, i have to overwrite command in order to enable --appendonly, but eventhough i have logs like this:

redis-1     | 1:C 20 May 2024 15:51:15.406 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-1     | 1:C 20 May 2024 15:51:15.407 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
redis-1     | 1:C 20 May 2024 15:51:15.407 * Configuration loaded
redis-1     | 1:M 20 May 2024 15:51:15.407 * monotonic clock: POSIX clock_gettime
redis-1     | 1:M 20 May 2024 15:51:15.407 * Running mode=standalone, port=6379.
redis-1     | 1:M 20 May 2024 15:51:15.408 * Server initialized
redis-1     | 1:M 20 May 2024 15:51:15.411 * Creating AOF base file appendonly.aof.1.base.rdb on server start
redis-1     | 1:M 20 May 2024 15:51:15.421 * Creating AOF incr file appendonly.aof.1.incr.aof on server start
redis-1     | 1:M 20 May 2024 15:51:15.421 * Ready to accept connections tcp

i get the error after accessing the endpoint:

ConnectionError
redis.exceptions.ConnectionError: Error 104 while writing to socket. Connection reset by peer.

I also checked many stack questions about this topics, tried few of them but with no success. I would appreciate the help. Thanks in advance!

Upvotes: 0

Views: 266

Answers (1)

Reza Norouzzadeh
Reza Norouzzadeh

Reputation: 11

If you use the volume you created in Docker. You need to tell Docker Composer to use external volumes.

volumes:
  mysql_data:
    external: true

Upvotes: 0

Related Questions