[英]Redis in cocker-compose: any way to specify a redis.conf file?
我的 Redis 容器在我的 docker_compose.yml 中定義為標准圖像
redis:
image: redis
ports:
- "6379"
我猜它使用標准設置,例如在本地主機上綁定到 Redis。 我需要綁定到0.0.0.0,有什么辦法可以在本地添加一個redis.conf文件來改變綁定,讓docker-compose來使用?
感謝您的任何技巧...
是的。 只需使用卷將您的redis.conf
掛載到默認值上:
redis:
image: redis
volumes:
- ./redis.conf:/usr/local/etc/redis/redis.conf
ports:
- "6379"
或者,基於 redis 映像創建一個新映像,並復制您的 conf 文件。完整說明位於: https ://registry.hub.docker.com/_/redis/
但是,redis 映像默認綁定到0.0.0.0
。 要從主機訪問它,您需要使用 Docker 為您映射到主機的端口,您可以使用 docker docker ps
或 docker docker port
命令找到它,然后您可以在localhost:32678
訪問它,其中 32678 是映射端口. 或者,您可以在docker-compose.yml
中指定要映射到的特定端口。
由於您似乎是 Docker 新手,因此如果您從使用原始 Docker 命令而不是從 Compose 開始,這可能會更有意義。
老問題,但如果有人仍然想這樣做,可以使用卷和命令:
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
不幸的是,對於 Docker,當涉及到 Redis 配置文件時,事情變得有點棘手,並且答案被評為最佳(我確信來自沒有實際測試過它的人)它不起作用。
但是,什么是有效的,快速的,沒有麻煩的是:
command: redis-server --bind redis-container-name --requirepass some-long-password --maxmemory 256mb --maxmemory-policy allkeys-lru --appendonly yes
您可以在 yaml docker 文件的命令部分中傳遞所需的所有變量選項,方法是在其前面添加“--”,然后是變量值。
永遠不要忘記設置密碼,並盡可能關閉端口 6379。
稍后謝謝我。
PS:如果您在命令中注意到,我沒有使用典型的 127.0.0.1,而是使用 redis 容器名稱。 這樣做是因為 docker 通過其嵌入式 dns 服務器在內部分配 IP 地址。 換句話說,這個綁定地址是動態的,因此增加了額外的安全層。
如果您的 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
基於 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
這樣,您可以通過 docker-compose.yml 文件包含 .conf 文件,並且不需要自定義圖像。
這是一個老問題,但我有一個看起來很優雅的解決方案,我不必每次都執行命令;)。
1 像這樣創建你的 dockerfile
#/bin/redis/Dockerfile
FROM redis
CMD ["redis-server", "--include /usr/local/etc/redis/redis.conf"]
我們正在做的是告訴服務器將該文件包含在 Redis 配置中。 您在此處鍵入的設置將覆蓋 Redis 的默認設置。
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 創建你的配置文件,它必須和 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
在 docker 環境中使用 Redis 時,我遇到了同樣的問題,即 Redis 無法將數據保存到 dump.rdb 上的磁盤。 問題是 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
它工作正常。
我認為我在本地共享工作代碼會很有幫助
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"
/usr/local/etc/redis/redis.conf
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.