Docker-compose 与 redis、sidekiq、rails SocketError
Posted
技术标签:
【中文标题】Docker-compose 与 redis、sidekiq、rails SocketError【英文标题】:Docker-compose with redis, sidekiq, rails SocketError 【发布时间】:2021-12-01 00:40:29 【问题描述】:我对 docker-compose 配置中的错误感到困惑。我正在将 redis 与 sidekiq 添加到使用 docker-compose 的现有 rails 应用程序中,但我在尝试使容器相互通信时失败了。我尝试了几种不同的选择,并查看了几乎所有关于 SO 的合理主题,这些主题涉及到这个主题,但我在基本相同的事情上一直失败。这是我当前的配置(或至少是相关部分)和错误:
docker-compose.yml
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
config/initializers/sidekiq.rb
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 以及 docker 中的 redis? 【参考方案1】:您可能没有将 redis 端口暴露给 sidekiq 服务尝试在 docker compose 中暴露它。这可能会奏效。
【讨论】:
你的意思是在redis服务中添加expose
参数?以前也试过,可惜没区别【参考方案2】:
原来我错过了有关配置的重要细节
networks:
app:
添加后
networks:
- app
对于 redis 和 sidekiq,一切都可以很好地相互交谈。
【讨论】:
以上是关于Docker-compose 与 redis、sidekiq、rails SocketError的主要内容,如果未能解决你的问题,请参考以下文章
通过 docker-compose 快速部署 Redis 保姆级教程
Docker部署Docker-compose部署redis容器及启动失败挂载失败等问题
Docker部署Docker-compose部署redis容器及启动失败挂载失败等问题