简体   繁体   English

Sidekiq + redis + docker-compose + 导轨

[英]Sidekiq + redis + docker-compose + rails

I configured my project to work with the listed technologies, but when requesting the route http://localhost: 3000/sidekiq , it gives the error Error connecting to Redis on 127.0.0.1:6379 (Errno:: ECONNREFUSED) .我将我的项目配置为使用列出的技术,但是在请求路由http://localhost: 3000/sidekiq时,它给出错误 Error connecting to Redis on 127.0.0.1:6379 (Errno:: ECONNREFUSED) In the terminal where docker is running, you can see the following:在docker运行的终端中,可以看到如下内容:

#terminal #终端

redis_1    | 1:C 14 Apr 2021 04:55:29.294 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1    | 1:C 14 Apr 2021 04:55:29.294 # Redis version=5.0.12, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1    | 1:C 14 Apr 2021 04:55:29.294 # 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 14 Apr 2021 04:55:29.297 * Running mode=standalone, port=6379.
redis_1    | 1:M 14 Apr 2021 04:55:29.297 # Server initialized
redis_1    | 1:M 14 Apr 2021 04:55:29.297 # 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 14 Apr 2021 04:55:29.297 # 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 14 Apr 2021 04:55:29.297 * DB loaded from disk: 0.000 seconds
redis_1    | 1:M 14 Apr 2021 04:55:29.297 * Ready to accept connections
sidekiq_1  | 2021-04-14T04:55:32.421Z pid=1 tid=4w5 INFO: Booting Sidekiq 6.2.1 with redis options {:url=>"redis://redis:6379/0"}
sidekiq_1  | 2021-04-14T04:55:32.547Z pid=1 tid=4w5 INFO: Booted Rails 6.1.3.1 application in development environment
sidekiq_1  | 2021-04-14T04:55:32.548Z pid=1 tid=4w5 INFO: Running in ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-linux]

#docker-compose.yml #docker-compose.yml

version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
  
  redis:
    image: 'redis:5-alpine'
    command: redis-server
    ports:
      - '6379:6379'

  sidekiq:
    depends_on:
      - 'redis'
    build: .
    command: bundle exec sidekiq
    volumes:
      - .:/myapp
    env_file:
    - .env
  
  web:
    depends_on:
      - 'db'
      - 'sidekiq'
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"

#Dockerfile #Dockerfile

FROM ruby:2.7.3
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp


COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000


CMD ["rails", "server", "-b", "0.0.0.0"]

#routes.rb #routes.rb

require 'sidekiq/web'
Rails.application.routes.draw do
      
    
      
  mount Sidekiq::Web => '/sidekiq'
      
  ...other routes
end

#sidekiq.rb #sidekiq.rb

sidekiq_config = { url: ENV['REDIS_URL'] }

Sidekiq.configure_server do |config|
  config.redis = sidekiq_config
end

Sidekiq.configure_client do |config|
  config.redis = sidekiq_config
end

#.env #.env

REDIS_URL=redis://redis:6379/0 REDIS_URL=redis://redis:6379/0

Please tell me if I missed some configuration file or where I am wrong with the settings?请告诉我是否遗漏了某些配置文件或设置有误?

In my case the reason was that I forgot to add就我而言,原因是我忘记添加

env_file:
- .env

in #docker-compose.yml in section "web".在“web”部分的#docker-compose.yml 中。 If someone will ever needs more info, I can answer here:)如果有人需要更多信息,我可以在这里回答:)

I had the same issue but it was not a .env file problem.我遇到了同样的问题,但这不是.env文件问题。 For me it was the format of REDIS_URL that was wrong.对我来说,错误的是REDIS_URL的格式。

I had:我有:

REDIS_URL=redis:http://localhost:6379

And I changed it to:我将其更改为:

REDIS_URL=redis://redis:6379/0

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

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