Redis in docker-compose: any way to specify a redis.conf file?
Asked Answered
T

8

69

My Redis container is defined as a standard image in my docker_compose.yml:

redis:  
  image: redis
  ports:
    - "6379"

I guess it's using standard settings like binding to Redis at localhost.

I need to bind it to 0.0.0.0, is there any way to add a local redis.conf file to change the binding and let docker-compose use it?

Trommel answered 30/5, 2015 at 14:20 Comment(1)
very important for anyone reading this, your custom redis.conf file needs to bind 0.0.0.0 -::1 else it wont work no matter which answer you follow below, i tried all of them before writing thisTricia
C
67

Yes. Mount the config into the image with a volume and modify the command to call it e.g:

redis:  
  image: redis
  command: redis-server /usr/local/etc/redis/redis.conf
  volumes:
    - ./redis.conf:/usr/local/etc/redis/redis.conf
  ports:
    - "6379"

Alternatively, create a new image based on the redis image with your conf file copied in. Full instructions are at: https://registry.hub.docker.com/_/redis/

However, the redis image does bind to 0.0.0.0 by default. To access it from the host, you need to use the port that Docker has mapped to the host for you which you find by using docker ps or the docker port command, you can then access it at localhost:32678 where 32678 is the mapped port. Alternatively, you can specify a specific port to map to in the docker-compose.yml.

As you seem to be new to Docker, this might all make a bit more sense if you start by using raw Docker commands rather than starting with Compose.

Campy answered 30/5, 2015 at 17:5 Comment(8)
thanks a lot ... I've been thru Docker aw commands before... I'm now learning Compose ... I'll try both suggestions ...Trommel
I tried to follow your recommendation. But Redis does not read my config file, and seems to just load its own default conf; for instance, I have required a password inside the conf file, but when the instance runs, there is no password requirement.Coenesthesia
@Coenesthesia that should work, open a separate question if it doesn't. It should be simple to debug though by just execing into the container.Campy
I did: #32404925 I'd really appreciate if you could help me outCoenesthesia
It's worth noting that you will still see the warning in the console that you are using the default config file in this case. This threw me, and I spent some time before realising I was actually using my custom config file.Gurango
what about version 3's config option in docker-compose.yml? I can't find how one can specify the host path to a config file there - does it need to be in the container already at build time? if so, what is the config option even doing in addition?Menace
@Coenesthesia - See Marcola's answer. You need to set the command as well, or it will fail to load /usr/local/etc/redis/redis.conf on startup.Ory
This does not work any more, and should be updated or the other answer that does work should be selected as answerFranchescafranchise
K
57

Old question, but if someone still want to do that, it is possible with volumes and command:

command: redis-server /usr/local/etc/redis/redis.conf
volumes:
 - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
Kearse answered 1/6, 2016 at 20:16 Comment(3)
Would this work if the container was on another host? volumes mounts directories on_the_host, right?Outface
Worth noting that redis configuration only allows connections from 127.0.0.1 by default, so if you want to connect from the host for testing purposes, then you need to tweak the options.Heddie
Specify an absolute path mapping in Volumes is safer, like this: volumes: - ${PWD}/redis/redis.conf:/usr/local/etc/redis/redis.confDiamonddiamondback
G
34

Unfortunately with Docker, things become a little tricky when it comes to Redis configuration file, and the answer voted as best (im sure from people that did'nt actually tested it) it DOESNT work.

But what DOES WORK, fast, and without husles is this:

 command: redis-server --bind redis-container-name --requirepass some-long-password --maxmemory 256mb --maxmemory-policy allkeys-lru --appendonly yes

You can pass all the variable options you want in the command section of the yaml docker file, by adding "--" in the front of it, followed by the variable value.

Never forget to set a password, and if possible close the port 6379.

Τhank me later.

PS: If you noticed at the command, i didnt use the typical 127.0.0.1, but instead the redis container name. This is done for the reason that docker assigns ip addresses internally via it's embedded dns server. In other words this bind address becomes dynamic, hence adding an extra layer of security.

If your redis container is called "redis" and you execute the command docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis (for verifying the running container's internal ip address), as far as docker is concerned, the command give in docker file, will be translated internally to something like: redis-server --bind 172.19.0.5 --requirepass some-long-password --maxmemory 256mb --maxmemory-policy allkeys-lru --appendonly yes

Gt answered 30/10, 2019 at 11:10 Comment(2)
This is the correct answer. I messed around with various configurations for the better half of a day and in the end, passing the options in the cmd is the only method that reliably and consistently works and does not require a ton of extra configurations. Only thing is I'll opt to thank you now as opposed to later :-D Thanks!Grange
Using requirepass some-long-password in production is a big security issue. ps will list your password and redis-cli will even have a warning that the password is exposed "Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.". Always use redis.conf in production.Ultrastructure
H
8

Based on David anwser but a more "Docker Compose" way is:

redis:
  image: redis:alpine
    command: redis-server --include /usr/local/etc/redis/redis.conf
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf

That way, you include the .conf file using the docker-compose.yml file and don't need a custom image.

Hendon answered 22/8, 2021 at 0:10 Comment(1)
while using it in my project I noticed that I had to also configure the bind key to the container name I was using, as : bind cache or bind [your-container-name]Myrtia
T
5
  1. mount your config /usr/local/etc/redis/redis.conf
  2. add command to execute redis-server with your config
  redis:
    image: redis:7.0.4-alpine
    restart: unless-stopped
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ########################################
    # or using command if mount not work
    ########################################
    command: >
      redis-server --bind 127.0.0.1
      --appendonly no
      --save ""
      --protected-mode yes
Tillotson answered 7/9, 2022 at 5:22 Comment(0)
S
4

I think it will be helpful i am sharing working code in my local

redis:
    container_name: redis
    hostname: redis
    image: redis
    command: >
      --include /usr/local/etc/redis/redis.conf
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"
Sahara answered 15/8, 2022 at 6:27 Comment(0)
I
3

It is an old question but I have a solution that seems elegant and I don't have to execute commands every time ;).

1 Create your dockerfile like this

#/bin/redis/Dockerfile
FROM redis
CMD ["redis-server", "--include /usr/local/etc/redis/redis.conf"]

What we are doing is telling the server to include that file in the Redis configuration. The settings you type there will override the default Redis have.

2 Create your docker-compose

redisall:
      build:
        context: ./bin/redis
      container_name: 'redisAll'
      restart: unless-stopped
      ports:
        - "6379:6379"
      volumes:
        - ./config/redis:/usr/local/etc/redis

3 Create your configuration file it has to be called the same as Dockerfile

//config/redis/redis.conf
requirepass some-long-password
appendonly yes

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.*
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

 // and all configurations that can be specified
 // what you put here overwrites the default settings that have the
 container
Indetermination answered 5/1, 2020 at 16:40 Comment(0)
C
3

I had the same problem when using Redis in docker environment that the Redis could not save data to disk on dump.rdb. The problem was the Redis could not read the configurations redis.conf , I solve it by sending the required configurations with the command in docker compose as below :

redis19:
   image: redis:5.0
   restart: always
   container_name: redis19
   hostname: redis19
   command: redis-server --requirepass some-secret  --stop-writes-on-bgsave-error no --save 900 1 --save 300 10 --save 60 10000


volumes:
  - $PWD/redis/redis_data:/data
  - $PWD/redis/redis.conf:/usr/local/etc/redis/redis.conf
  - /etc/localtime:/etc/localtime:ro

and it works fine.

Cis answered 16/8, 2021 at 12:41 Comment(1)
I prefer this solutionChiseler

© 2022 - 2024 — McMap. All rights reserved.