简体   繁体   English

cocker-compose 中的 Redis:有什么方法可以指定 redis.conf 文件?

[英]Redis in cocker-compose: any way to specify a redis.conf file?

my Redis container is defined as a standard image in my docker_compose.yml我的 Redis 容器在我的 docker_compose.yml 中定义为标准图像

redis:  
  image: redis
  ports:
    - "6379"

I guess it's using standard settings like binding to Redis at localhost.我猜它使用标准设置,例如在本地主机上绑定到 Redis。 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 to use it?我需要绑定到0.0.0.0,有什么办法可以在本地添加一个redis.conf文件来改变绑定,让docker-compose来使用?

thanks for any trick...感谢您的任何技巧...

Yes.是的。 Just mount your redis.conf over the default with a volume:只需使用卷将您的redis.conf挂载到默认值上:

redis:  
  image: redis
  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/或者,基于 redis 映像创建一个新映像,并复制您的 conf 文件。完整说明位于: https ://registry.hub.docker.com/_/redis/

However, the redis image does bind to 0.0.0.0 by default.但是,redis 映像默认绑定到0.0.0.0 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.要从主机访问它,您需要使用 Docker 为您映射到主机的端口,您可以使用 docker docker ps或 docker docker port命令找到它,然后您可以在localhost:32678访问它,其中 32678 是映射端口. Alternatively, you can specify a specific port to map to in the docker-compose.yml .或者,您可以在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.由于您似乎是 Docker 新手,因此如果您从使用原始 Docker 命令而不是从 Compose 开始,这可能会更有意义。

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

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.不幸的是,对于 Docker,当涉及到 Redis 配置文件时,事情变得有点棘手,并且答案被评为最佳(我确信来自没有实际测试过它的人)它不起作用。

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.您可以在 yaml docker 文件的命令部分中传递所需的所有变量选项,方法是在其前面添加“--”,然后是变量值。

Never forget to set a password, and if possible close the port 6379.永远不要忘记设置密码,并尽可能关闭端口 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. PS:如果您在命令中注意到,我没有使用典型的 127.0.0.1,而是使用 redis 容器名称。 This is done for the reason that docker assigns ip addresses internally via it's embedded dns server.这样做是因为 docker 通过其嵌入式 dns 服务器在内部分配 IP 地址。 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如果您的 redis 容器名为“redis”并且您执行命令docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis (用于验证正在运行的容器的内部 IP 地址),就 docker 而言,docker 文件中给出的命令将在内部转换为类似: redis-server --bind 172.19.0.5 --requirepass some-long-password --maxmemory 256mb --maxmemory-policy allkeys-lru --appendonly yes

Based on David awnser but a more "Docker Compose" way is:基于 David awnser 但更“Docker Compose”的方式是:

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 by docker-compose.yml file and don't need a custom image.这样,您可以通过 docker-compose.yml 文件包含 .conf 文件,并且不需要自定义图像。

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 1 像这样创建你的 dockerfile

#/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.我们正在做的是告诉服务器将该文件包含在 Redis 配置中。 The settings you type there will override the default Redis have.您在此处键入的设置将覆盖 Redis 的默认设置。

2 Create your docker-compose 2 创建你的 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 3 创建你的配置文件,它必须和 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

I had the same problem when using Redis in docker environment that the Redis could not save data to disk on dump.rdb.在 docker 环境中使用 Redis 时,我遇到了同样的问题,即 Redis 无法将数据保存到 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 :问题是 Redis 无法读取配置 redis.conf ,我通过使用 docker compose 中的命令发送所需的配置来解决它,如下所示:

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.它工作正常。

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"
  1. mount your config /usr/local/etc/redis/redis.conf挂载你的配置/usr/local/etc/redis/redis.conf
  2. add command to execute redis-server with your config添加命令以使用您的配置执行redis-server
  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM