How to fix the redis warnings with docker

When we start up a redis service with docker, we will meet some warnings which maybe make your service shut down. Like, the limitation of maximum connections. So what we should do is fix these warnings. In normal case, we will see four kind of warnings like below.

Fig.1 redis warnings

First of all, we should give a specific config for redis. So we have to copy(or mount) a config file to the docker container. If you don’t have the file redis.conf, please refer to https://redis.io/topics/config. You can find every versions’ config.

Dockerfile
1
2
3
4
5
FROM redis:alpine
WORKDIR /redis
COPY redis.conf /usr/local/etc/redis/redis.conf
COPY init.sh ./
RUN chmod +x init.sh

The easy way is that we could modify the system configurations before starting redis server. The other recommanded way is that we can mount these system files from host machine to docker container(But I never tried it, I’m not sure if it’s good or not.). Below is the example of the easy way.

init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#  WARNING you have Transparent Huge Pages (THP) support enabled in your kernel.
# This will create latency and memory usage issues with Redis.
# To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root,
# and add it to your /etc/rc.local in order to retain the setting after a reboot.
# Redis must be restarted after THP is disabled.

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

# WARNING: The TCP backlog setting of 511 cannot be enforced
# because /proc/sys/net/core/somaxconn is set to the lower value of 128.

sysctl -w net.core.somaxconn=512

# WARNING overcommit_memory is set to 0! Background save may fail under low memory condition.
# To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot
# or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
# The overcommit_memory has 3 options.
# 0, the system kernel check if there is enough memory to be allocated to the process or not,
# if not enough, it will return errors to the process.
# 1, the system kernel is allowed to allocate the whole memory to the process
# no matter what the status of memory is.
# 2, the system kernel is allowed to allocate a memory whose size could be bigger than
# the sum of the size of physical memory and the size of exchange workspace to the process.

sysctl vm.overcommit_memory=1

# start redis server

redis-server /usr/local/etc/redis/redis.conf --bind 0.0.0.0

ATTENTION PLEASE!
DON NOT FORGET THE OPTION ‘—bind 0.0.0.0‘ IF YOU WANT TO ACCESSS THE REDIS SERVER FROM THE OUTSIDE OF CONTAINER.

And we also should add an optionwhich named ‘privileged‘ in docker compose file, otherwise you will be not allowed to modify the system config in containter.

docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
version: '3'

services:
redis:
build:
context: ./redis
image: myredis
privileged: true
command: sh -c "./init.sh"
volumes:
- redis_data:/data:rw
ports:
- 6379:6379


How to fix the redis warnings with docker
https://r-future.github.io/post/how-to-fix-redis-warnings-with-docker/
Author
Future
Posted on
December 4, 2019
Licensed under