Disable redis persistence in docker
Asked Answered
F

5

14

According to this link, persistence of redis instances can be prevented by setting --save ''. However when I try to use that argument in my docker-compose.yml the redis instance still loads a database from disk and persists all data:

version: '3'
services:
  redis:
    image: "redis:alpine"
    command: ["redis-server", "--save"]  # disable persistence
    ports:
      - "7777:6379"

I also tried command: ["redis-server", "--save", ""] but the problem preserves.

How can I enforce non-persistence (empty redis on every restart)?

Fetid answered 5/2, 2019 at 11:22 Comment(2)
It is unclear to me whether you want to a) prevent Redis writing anything to disk as it runs, so it just runs from RAM, or b) you want to flush the entire contents of Redis at every restart. You can achieve a) in your Redis config file. You can achieve b) using @grapes answer, or using the CLI to run a flushall.Ruzich
I want b) but only for one redis instance. In the same docker-compose file I have a second redis-instance that should be persistedFetid
D
15

Not very elegant, but works:

  redis:
    image: "redis:5.0.3-alpine"
    command: [sh, -c, "rm -f /data/dump.rdb && redis-server"]  # disable persistence

/data/dump.rdb is where Redis stores the database.

Diffident answered 22/3, 2019 at 10:36 Comment(1)
A bit briefer: command: sh -c "rm -f /data/dump.rdb && redis-server"Karmenkarna
M
13

--renew-anon-volumes addresses the underlying problem of Redis retrieving a volume from the previous container after being stopped.

docker-compose up --renew-anon-volumes

Note that this affects other containers that keep their volumes around after being stopped, such as MySQL and Postgres.

Metamorphose answered 1/6, 2020 at 16:27 Comment(2)
This worked, as opposed to --force-recreate flag which did not. Can you explain please why in the world redis container ended up being persistable? Containers are said to be stateless, right? And I did not add any voulmes for /data dir of redis, all I did was volumes:-./redis.conf:/redis.confMicron
@AnatolyAlekseev Good question! The redis Dockerfile actually creates an anonymous volume for you, which Docker Compose keeps track of unless you explicitly call docker-compose down --volumes or docker-compose up --renew-anon-volumes.Metamorphose
J
9

For me work

command: redis-server --save "" --appendonly no
image: redis:alpine
J answered 22/2, 2022 at 11:0 Comment(0)
A
2

Try --force-recreate option for docker-compose. This just recreates all services containers from image every time, giving a guarantee that no data persists at all.

Useful for ci testing pipeline when you don't want to deal with individual flags for all services.

docker-compose up --force-recreate
Archespore answered 5/2, 2019 at 11:36 Comment(3)
can I recreate a specific service? Because I have other services that should persistFetid
yes you can recreate a specific service in docker-compose up <servicename>Recoil
Tried this, but it does not work for me. Maybe the setting only cares about building images, not removing containers?Nauseous
G
2

For me this works:

image: redis:4.0-alpine
command: [sh, -c, "rm -f /data/dump.rdb && redis-server --save ''"]
Gothar answered 23/6, 2019 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.