Docker and Redis

History of f#ckups

Posted on 2017-06-22 20:58:14

Recently I deployed my small pet-project with docker-compose to VPS. And I noticed that from time to time my Sidekiq logs contain the following line:

Error fetching job: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persi st on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.

Redis logs accordingly contain:

Can't save in background: fork: Cannot allocate memory

I googled a little bit and found the following explanation:

There might be errors during the bgsave process due to low memory. Try this (from redis background save FAQ)

Redis documentation says:

Background saving fails with a fork() error under Linux even if I have a lot of free RAM!
Short answer: echo 1 > /proc/sys/vm/overcommit_memory :)

First I tried to fix this inside docker container, passing sysctl variable through docker-compose.

    privileged: true
      - vm.overcommit_memory=1

But unfortunately it didn’t help, I got the following error:

ERROR: for 2f611be5a7e8_2f611be5a7e8_2f611be5a7e8_myproject_redis_1  Cannot start service redis: oci runtime error: sysctl "vm.overcommit_memory" is not in a separate kernel namespace

I even tried to do it inside Dockerfile

But the reason is that, if your host system does not have the vm.overcommit_memory=1 enabled, you will not be able to switch it inside container.

So the final solution was to enable it on host system.