[英]Docker-compose with redis, sidekiq, rails SocketError
我對 docker-compose 配置中的錯誤感到困惑。 我正在將 redis 與 sidekiq 添加到使用 docker-compose 的現有 Rails 應用程序中,但我在嘗試使容器相互通信時失敗了。 我嘗試了幾種不同的選擇,並查看了幾乎所有與此主題相關的合理主題,但我一直在基本相同的事情上失敗。 這是我當前的配置(或至少它的相關部分)和錯誤:
services:
web:
<<: *common-settings
build:
context: .
dockerfile: docker/development/Dockerfile
target: web
command: dumb-init bash -c "(bundle check || bundle install) && rails s -b 0.0.0.0 -p 3000"
depends_on:
- db
- redis
- elasticsearch
ports:
- '3000:3000'
expose:
- 3000
redis:
image: 'redis:5-alpine'
ports:
- '6379:6379'
volumes:
- 'redis:/data'
sidekiq:
depends_on:
- db
- redis
- web
build: .
command: bundle exec sidekiq
volumes:
- .:/app:cached
- bundle:/bundle
env_file:
- ./.env
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://redis:6379/12' }
end
Sidekiq.configure_client do |config|
config.redis = { url: 'redis://redis:6379/12' }
end
是的,這是目前公開的硬編碼,但我嘗試過只使用 ENV 變量的配置,結果完全相同,無論我從哪里得到的值都是相同的。 現在嘗試這樣的東西,因為它沒有任何區別。
這是我嘗試訪問 localhost:3000/sidekiq 時的錯誤
Error connecting to Redis on redis:6379 (SocketError)
Application Trace | Framework Trace | Full Trace
redis (4.4.0) lib/redis/client.rb:384:in `rescue in establish_connection'
redis (4.4.0) lib/redis/client.rb:365:in `establish_connection'
redis (4.4.0) lib/redis/client.rb:117:in `block in connect'
redis (4.4.0) lib/redis/client.rb:330:in `with_reconnect'
redis (4.4.0) lib/redis/client.rb:116:in `connect'
redis (4.4.0) lib/redis/client.rb:403:in `ensure_connected'
redis (4.4.0) lib/redis/client.rb:255:in `block in process'
redis (4.4.0) lib/redis/client.rb:342:in `logging'
redis (4.4.0) lib/redis/client.rb:254:in `process'
redis (4.4.0) lib/redis/client.rb:148:in `call'
redis (4.4.0) lib/redis.rb:307:in `block in info'
redis (4.4.0) lib/redis.rb:70:in `block in synchronize'
/usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
redis (4.4.0) lib/redis.rb:70:in `synchronize'
redis (4.4.0) lib/redis.rb:306:in `info'
sidekiq (6.2.2) lib/sidekiq.rb:120:in `block in redis_info'
sidekiq (6.2.2) lib/sidekiq.rb:98:in `block in redis'
connection_pool (2.2.5) lib/connection_pool.rb:63:in `block (2 levels) in with'
connection_pool (2.2.5) lib/connection_pool.rb:62:in `handle_interrupt'
connection_pool (2.2.5) lib/connection_pool.rb:62:in `block in with'
connection_pool (2.2.5) lib/connection_pool.rb:59:in `handle_interrupt'
connection_pool (2.2.5) lib/connection_pool.rb:59:in `with'
sidekiq (6.2.2) lib/sidekiq.rb:95:in `redis'
sidekiq (6.2.2) lib/sidekiq.rb:114:in `redis_info'
sidekiq (6.2.2) lib/sidekiq/web/helpers.rb:177:in `redis_info'
.
.
.
當我嘗試從 Web 應用程序運行工作程序或去那里使用bundle exec sidekiq
時發生同樣的錯誤
這是我運行docker-compose up
時注銷的內容
redis_1 | 1:C 12 Oct 2021 11:44:19.103 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 12 Oct 2021 11:44:19.103 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 12 Oct 2021 11:44:19.103 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 12 Oct 2021 11:44:19.105 * Running mode=standalone, port=6379.
redis_1 | 1:M 12 Oct 2021 11:44:19.105 # Server initialized
redis_1 | 1:M 12 Oct 2021 11:44:19.105 # 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.
redis_1 | 1:M 12 Oct 2021 11:44:19.105 # 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.
redis_1 | 1:M 12 Oct 2021 11:44:19.105 * DB loaded from disk: 0.000 seconds
redis_1 | 1:M 12 Oct 2021 11:44:19.106 * Ready to accept connections
sidekiq_1 | 2021-10-12T11:44:26.712Z pid=1 tid=ouccdy1p5 INFO: Booting Sidekiq 6.2.2 with redis options {:url=>"redis://redis:6379/12"}
sidekiq_1 | 2021-10-12T11:44:27.174Z pid=1 tid=ouccdy1p5 INFO: Booted Rails 5.2.6 application in development environment
sidekiq_1 | 2021-10-12T11:44:27.174Z pid=1 tid=ouccdy1p5 INFO: Running in ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux-musl]
sidekiq_1 | 2021-10-12T11:44:27.174Z pid=1 tid=ouccdy1p5 INFO: See LICENSE and the LGPL-3.0 for licensing details.
sidekiq_1 | 2021-10-12T11:44:27.174Z pid=1 tid=ouccdy1p5 INFO: Upgrade to Sidekiq Pro for more features and support: https://sidekiq.org
至於使用 ENV 變量,我通常嘗試在.env
文件中設置這樣的值
REDIS_URL=redis://redis:6379/12
當我檢查 docker ps 時,redis 容器通常在那里。 如果我理解正確,URL redis://redis:6379/12
使用redis
作為主機的名稱,它對應於我在 docker-compose.yml 中指定的服務的名稱,並且應該允許容器與之通信彼此。 這個文件最初有更多的服務,比如 elasticsearch,它們工作得很好。
我嘗試了很多不同的設置,但我覺得我的頭在摔倒。 我錯過了什么?
您可能沒有將 redis 端口暴露給 sidekiq 服務,請嘗試在 docker compose 中公開它。 這可能會奏效。
原來我錯過了一個關於配置的重要細節
networks:
app:
添加后
networks:
- app
對於 redis 和 sidekiq,一切都能夠很好地相互交談。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.