Sidekiq Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) on docker-compose
Asked Answered
F

8

54

I'm trying to run sidekiq worker with Rails. When I try to docker-compose up worker I get the following error:

worker_1 | Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:332:in `rescue in establish_connection'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:318:in `establish_connection'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:94:in `block in connect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:280:in `with_reconnect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:93:in `connect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:351:in `ensure_connected'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:208:in `block in process'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:293:in `logging'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:207:in `process'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:113:in `call'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:211:in `block in info'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:57:in `block in synchronize'
worker_1 | /usr/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:57:in `synchronize'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:210:in `info'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/cli.rb:71:in `block in run'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq.rb:84:in `block in redis'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:64:in `block (2 levels) in with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `handle_interrupt'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `block in with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `handle_interrupt'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq.rb:81:in `redis'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/cli.rb:68:in `run'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/bin/sidekiq:13:in `<top (required)>'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/bin/sidekiq:23:in `load'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/bin/sidekiq:23:in `<main>'
nyvur_worker_1 exited with code 1

Here's my docker-compose file:

web: &app_base
  build: .
  ports:
    - "80:80"
  volumes:
    - .:/Nyvur
  command: /usr/bin/start_server.sh
  links:
    - postgres
    - mongo
    - redis
  environment: &app_environment
    SIDEKIQ_CONCURRENCY: 50
    SIDEKIQ_TIMEOUT: 10
    ENABLE_DEBUG_SERVER: true
    RACK_ENV: production
    RAILS_ENV: production

worker:
  build: .
  volumes:
    - .:/Nyvur
  ports: []
  links:
    - postgres
    - mongo
    - redis
  command: bundle exec sidekiq -c 50

postgres:
  image: postgres:9.1
  ports:
    - "5432:5432"
  environment:
    LC_ALL: C.UTF-8
    POSTGRES_DB: Nyvur_production
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: 3x1mpl3

mongo:
  image: mongo:3.0.7
  ports:
    - "27017:27017"

redis:
  image: redis
  ports:
    - "6379:6379"

My Dockerfile:

FROM phusion/passenger-customizable
MAINTAINER VodkaMD <[email protected]>
ENV RACK_ENV="production" RAILS_ENV="production"
SECRET_KEY_BASE="e09afa8b753cb175bcef7eb5f737accd02a4c16d9b6e5d475943605abd4277cdf47c488812d21d9c7117efd489d876f34be52f7ef7e88b21759a079339b198ce"
ENV HOME /root
CMD ["/sbin/my_init"]

RUN /pd_build/utilities.sh
RUN /pd_build/ruby2.2.sh
RUN /pd_build/python.sh
RUN /pd_build/nodejs.sh
RUN /pd_build/redis.sh
RUN /pd_build/memcached.sh
RUN apt-get update && apt-get install -y vim nano dialog net-tools build-essential wget libpq-dev git
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# RUN mkdir /etc/nginx/ssl
# RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
RUN rm -f /etc/service/nginx/down
RUN rm -f /etc/service/redis/down
RUN rm -f /etc/service/sshd/down
RUN rm -f /etc/service/memcached/down
WORKDIR /tmp
ADD Gemfile /tmp/
ADD Gemfile.lock /tmp/
RUN mkdir /home/app/Nyvur
ADD . /home/app/Nyvur
RUN chown -R app:app /home/app/Nyvur
WORKDIR /home/app/Nyvur
RUN bundle install --deployment
RUN bundle exec rake assets:precompile
RUN rm /etc/nginx/sites-enabled/default
COPY config/nginx/nginx_nyvur.conf /etc/nginx/sites-enabled/nginx_nyvur.conf
ADD config/nginx/postgres-env.conf /etc/nginx/main.d/postgres-env.conf
ADD config/nginx/rails-env.conf /etc/nginx/main.d/rails-env.conf
ADD config/nginx/start_server.sh /usr/bin/start_server.sh
RUN chmod +x /usr/bin/start_server.sh
RUN mkdir -p /home/app/Nyvur/tmp/pids
RUN mkdir -p /home/app/Nyvur/tmp/sockets
RUN mkdir -p /home/app/Nyvur/log
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EXPOSE 80 443 9292

I've tried different configurations, I've checked other builds, but the problem still persists, So far, Sidekiq runs well outside of Docker.

Foment answered 11/1, 2016 at 19:44 Comment(0)
O
39
Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)

Your app tries to connect on the localhost interface of the container it is running in, but redis is running in a different container.

Modify your app config to use the link name of the redis container (redis in your case) as hostname for the connection.

Otterburn answered 11/1, 2016 at 20:57 Comment(4)
Thanks! I replaced the IP's with Docker env's, restarted everything and it worked out.Foment
where do you set up this config for docker?Landrum
When instantiating redis on boot up (Redis.new), I have ENV['REDIS_PORT_6379_TCP_ADDR'] || '0.0.0.0', incase I run it outside of Docker.Foment
Achieve it adding JOB_WORKER_URL=redis://redis:6379 in your .env file if you have sidekiq_config = { url: ENV['JOB_WORKER_URL'] } in sidekiq.rbAndromede
T
59

Check if your redis server is running, start redis by using the following command in the terminal:

redis-server
Tragic answered 23/4, 2016 at 12:41 Comment(3)
Yeah, I was pretty naive lol. I had success pointing at redis://redis:6379.Foment
Nice, I always have this when i use "killall -9 ruby" in my terminal,.Tragic
I had to make sure redis was actually installed. On MacPorts, I ran sudo port install redis and then sudo port load redis.Windbound
O
39
Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)

Your app tries to connect on the localhost interface of the container it is running in, but redis is running in a different container.

Modify your app config to use the link name of the redis container (redis in your case) as hostname for the connection.

Otterburn answered 11/1, 2016 at 20:57 Comment(4)
Thanks! I replaced the IP's with Docker env's, restarted everything and it worked out.Foment
where do you set up this config for docker?Landrum
When instantiating redis on boot up (Redis.new), I have ENV['REDIS_PORT_6379_TCP_ADDR'] || '0.0.0.0', incase I run it outside of Docker.Foment
Achieve it adding JOB_WORKER_URL=redis://redis:6379 in your .env file if you have sidekiq_config = { url: ENV['JOB_WORKER_URL'] } in sidekiq.rbAndromede
T
14

On macOS (using Homebrew), I was able to fix this by running:

brew services start redis

If you haven't yet installed redis, you'll need to run the following first:

brew install redis
Twofold answered 31/8, 2020 at 23:33 Comment(0)
N
1

I have solved this error using the following snippet

sudo apt install redis-server

and then by running the redis-server using —

redis-server
Nebulosity answered 8/3, 2021 at 13:50 Comment(0)
G
0

Redis quick review

Had a similar issue, in my case I have a script that starts redis and other initial settings and I forgot to run it. It usually breaks with CaS (Tomcat) but this time it broke due to redis and I had all but forgotten that was being setup by the script.

Anyway to complement other answers, a quick way to check if there are issues is to run

redis-cli

Which will let you know if redis is working and therefore help you remember if you need to run it.

If Redis isn't running you'll get an error:

redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

If it is running you'll get a redis console (in interactive mode), see documentation:

redis-cli
127.0.0.1:6379>

For example you can say: PING and it will return PONG

redis-cli
127.0.0.1:6379> PING
PONG

To run the Redis Server don't forget to just do:

redis-server
Greenhaw answered 25/3, 2021 at 18:27 Comment(0)
M
-1

Just start your redis server by using redis-server command. After start sidekiq

Mazdaism answered 26/8, 2016 at 11:46 Comment(0)
H
-1

If you are using heroku and having such problem then you need to specify REDIS_PROVIDER_URL in your env

Hersey answered 11/12, 2019 at 7:17 Comment(0)
H
-1

Make sure you installed redis-server on your system :-

 sudo apt install redis-server
Hammy answered 13/7, 2022 at 16:35 Comment(1)
This question is trying to connect to Redis in another container and that configuration is included in the docker-compose.yml file already. In a Docker context, 127.0.0.1 wouldn't reach the host system either.Stacystadholder

© 2022 - 2024 — McMap. All rights reserved.