I
I
Ismail2020-04-22 20:26:18
Ruby on Rails
Ismail, 2020-04-22 20:26:18

Rails not seeing Redis in docker-compose?

Good evening.
I'm trying to connect React frontend with Rails via websocket. When sending a POST request to the server, I get a 500 error and the following message: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED).
I work in docker-compose and I don't understand what the problem is. Below is the docker-compose up log :

docker-compose log

docker-compose up
Starting api_task_db_1           ... done
Starting api_task_bundle_cache_1 ... done
Recreating api_task_redis_1      ... done
Recreating api_task_api_1        ... done
Recreating api_task_sidekiq_1    ... done
Recreating api_task_client_1     ... done
Recreating api_task_nginx_1      ... done
Attaching to api_task_bundle_cache_1, api_task_db_1, api_task_redis_1, api_task_sidekiq_1, api_task_api_1, api_task_client_1, api_task_nginx_1
client_1        | yarn install v1.22.4
client_1        | warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
client_1        | [1/4] Resolving packages...
db_1            | 2020-04-22 17:16:20.162 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1            | 2020-04-22 17:16:20.162 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1            | 2020-04-22 17:16:20.166 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
api_task_bundle_cache_1 exited with code 0
db_1            | 2020-04-22 17:16:20.208 UTC [18] LOG:  database system was shut down at 2020-04-22 17:16:09 UTC
db_1            | 2020-04-22 17:16:20.215 UTC [1] LOG:  database system is ready to accept connections
redis_1         | 1:C 22 Apr 2020 17:16:20.948 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1         | 1:C 22 Apr 2020 17:16:20.948 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1         | 1:C 22 Apr 2020 17:16:20.948 # 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 22 Apr 2020 17:16:20.951 * Running mode=standalone, port=6379.
redis_1         | 1:M 22 Apr 2020 17:16:20.952 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1         | 1:M 22 Apr 2020 17:16:20.952 # Server initialized
redis_1         | 1:M 22 Apr 2020 17:16:20.952 # 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 22 Apr 2020 17:16:20.952 # 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 22 Apr 2020 17:16:20.952 * DB loaded from disk: 0.000 seconds
redis_1         | 1:M 22 Apr 2020 17:16:20.952 * Ready to accept connections
sidekiq_1       | 2020-04-22T17:16:25.863Z 1 TID-gsb0kmf45 INFO: Booting Sidekiq 5.2.7 with redis options {:url=>"redis://redis:6379/0", :id=>"Sidekiq-server-PID-1"}
sidekiq_1       | 2020-04-22T17:16:26.426Z 1 TID-gsb0kmf45 INFO: Running in ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux-musl]
sidekiq_1       | 2020-04-22T17:16:26.426Z 1 TID-gsb0kmf45 INFO: See LICENSE and the LGPL-3.0 for licensing details.
sidekiq_1       | 2020-04-22T17:16:26.427Z 1 TID-gsb0kmf45 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
sidekiq_1       | 2020-04-22T17:16:26.434Z 1 TID-gsb0kmf45 INFO: Starting processing, hit Ctrl-C to stop
db_1            | 2020-04-22 17:16:27.016 UTC [25] ERROR:  database "app_development" already exists
db_1            | 2020-04-22 17:16:27.016 UTC [25] STATEMENT:  CREATE DATABASE "app_development" ENCODING = 'unicode'
api_1           | Database 'app_development' already exists
db_1            | 2020-04-22 17:16:27.036 UTC [26] ERROR:  database "app_test" already exists
db_1            | 2020-04-22 17:16:27.036 UTC [26] STATEMENT:  CREATE DATABASE "app_test" ENCODING = 'unicode'
api_1           | Database 'app_test' already exists
api_1           | Model files unchanged.
client_1        | success Already up-to-date.
client_1        | Done in 2.60s.
client_1        | yarn run v1.22.4
client_1        | $ react-scripts start
api_1           | config/routes.rb was annotated.
api_1           | Puma starting in single mode...
api_1           | * Version 4.3.3 (ruby 2.6.5-p114), codename: Mysterious Traveller
api_1           | * Min threads: 5, max threads: 5
api_1           | * Environment: development
client_1        | ℹ 「wds」: Project is running at http://172.24.0.6/
client_1        | ℹ 「wds」: webpack output is served from 
client_1        | ℹ 「wds」: Content not from webpack is served from /app/public
client_1        | ℹ 「wds」: 404s will fallback to /
client_1        | Starting the development server...
client_1        | 
api_1           | * Listening on tcp://0.0.0.0:8080
api_1           | Use Ctrl-C to stop
client_1        | Compiled with warnings.



gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.5'

gem 'rails', '~> 6.0.2', '>= 6.0.2.2'

gem 'pg', '>= 0.18', '< 2.0'

gem 'puma', '~> 4.1'

gem 'bootsnap', '>= 1.4.2', require: false

gem 'rack-cors'

gem 'redis'

gem 'active_model_serializers', '~> 0.10.0'

gem 'sidekiq'

group :development, :test do
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  gem 'factory_bot_rails'
  gem 'ffaker'
  gem 'rspec-rails', '~> 4.0.0'
end

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'annotate'
  gem 'pry-rails'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

gem 'rubocop', '~> 0.81.0', require: false



Sidekiq.rb

require "sidekiq"

Sidekiq.configure_server do |config|
  config.redis = { url: "redis://redis:6379/0" }
end



redis.rb

uri = "redis://redis:6379/0"

Rails.application.config.cache_store = :redis_store, uri



docker-compose.yml

version: "3.7"
x-api-volumes: &api-volumes
  - .:/app:cached
  - ~/.ssh:/root/.ssh
  - ~/.bash_history:/root/.bash_history
  - bundle_cache:/bundle_cache
x-api-environment: &api-environment
  - RAILS_PORT=8080
  - RAILS_SECRET_KEY_BASE=secret_key_base
  - DATABASE_HOST=db
  - REDIS_URL=redis://redis:6379/0
  - REDIS_PORT=6379
  - DATABASE_USERNAME=postgres
  - BUNDLE_PATH=/bundle_cache
  - GEM_HOME=/bundle_cache
  - GEM_PATH=/bundle_cache:/usr/local/bundle
services:
  api:
    build:
      context: .
      dockerfile: Dockerfile.api
    depends_on:
      - db
      - redis
    volumes: *api-volumes
    ports:
      - 8080:8080
    environment: *api-environment
    command: bash -c "bundle install && bundle exec rake db:create db:migrate && bundle exec puma -C config/puma.rb -e development"
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"
  sidekiq:
    depends_on:
      - 'db'
      - 'redis'
    build: .
    command: bundle exec sidekiq
    volumes:
      - '.:/app'
    env_file:
      - '.env'
  db:
    image: postgres:10.3-alpine
  bundle_cache:
    image: busybox
    volumes:
      - bundle_cache:/bundle_cache
  client:
    build:
      context: client
      dockerfile: Dockerfile.client
    ports:
      - "3000:3000"
    depends_on:
      - api
    environment:
      - NODE_ENV=development
      - API_HOST=api
      - API_PORT=8080
      - CI=true
      - CHOKIDAR_USEPOLLING=true
    command: sh -c "yarn && yarn start"
    volumes:
      - ./client:/app:cached
      - client:/app/build
  nginx:
    build: ./infrastructure/nginx
    volumes:
      - client:/app/dist
      - ./public:/app/public
      - ./infrastructure/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./infrastructure/nginx/conf.d:/etc/nginx/conf.d
    ports:
      - "80:80"
    links:
      - api
volumes:
  bundle_cache:
  client:

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ismail, 2020-04-22
@gosugod

The problem was solved, corrected cable.yml:

production:
  adapter: redis
  url: redis://redus:6379/0

development:
  adapter: redis
  url: redis://redis:6379/0

test:
  adapter: redis
  url: redis://redus:6379/0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question