12

I am getting this error while running my rails app with docker and docker-compose Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)

Please find my Docker file

# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5

# Copy the main application.
COPY . ./app

# Expose port 3000 to the Docker host, so we can access it
# from the outside.
EXPOSE 3000

# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]

Please find my docker-compose.yml file

version: "2"
services:
  redis:
    image: redis
    command: redis-server
    ports:
      - "6379:6379"
  postgres:
    image: postgres:9.4
    ports:
      - "5432"

  app:
    build: .
    command: rails server -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    links:
      - postgres
      - redis
      - sidekiq

  sidekiq:
    build: .
    command: bundle exec sidekiq
    depends_on:
      - redis
    volumes:
      - .:/app
    env_file:
      - .env

Thanks in Advance!

Rahul Ojha
  • 396
  • 1
  • 4
  • 11

2 Answers2

27

I use a docker-compose and i add in .env file

REDIS_URL=redis://redis:6379/0
roxdurazo
  • 745
  • 10
  • 21
  • This works for me, but would someone be able to explain why? I'd like to be able to understand why it works. I get it in a nutshell - It points it to the correct path/url but why do we have to put it in the .env file? – lscott3 Dec 10 '18 at 06:09
  • 1
    good question. i just get to this because googling but i don't know the why :( – roxdurazo Dec 11 '18 at 19:59
  • 4
    According to the sidekiq docs, sidekiq by default uses localhost:6379 to attempt to connect to redis. However, there are some issues using localhost with docker in development, which is why the rails server is bound to 0.0.0.0. Thus, we need to specify with an env variable the specific address of where sidekiq can connect to redis – Nathan Lauer Jan 23 '19 at 04:39
8

Links are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.

According to your docker-compose.yaml file you can access you redis container on 127.0.0.1:6379 only from host machine.

Containers communicate with each other in their network, so you can access your redis container on redis:6379 from rails app container.

nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • This doesn't answer the question, it only explains why it's happening. Is there a way to use a containerized Redis instance, but still have it served on 127.0.0.1, I guess not? – ortonomy Apr 12 '18 at 05:30
  • 1
    To have it served on 127.0.0.1, you should expose sidekiq's container port 6379 in your docker-compose file, this way you can refer to your local host, but be careful because it is not always a good idea to expose all your containers important ports – Rafael Baldasso Audibert Mar 07 '19 at 02:01