简体   繁体   中英

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) . In the terminal where docker is running, you can see the following:

#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

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

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

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

#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

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". If someone will ever needs more info, I can answer here:)

I had the same issue but it was not a .env file problem. For me it was the format of REDIS_URL that was wrong.

I had:

REDIS_URL=redis:http://localhost:6379

And I changed it to:

REDIS_URL=redis://redis:6379/0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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