/home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/connection/ruby.rb:152:in `getaddrinfo': getaddrinfo:名称或服务未知(SocketError)

6
我正在使用docker-compose为开发人员提供ROR、postgres、redis、mongo等技术栈的开发环境。 docker-compose build 成功运行,但当我运行 docker-compose up 时遇到了以下错误。
web_1   | => Booting Thin
web_1   | => Rails 4.2.3 application starting in development on http://0.0.0.0:3000
web_1   | => Run `rails server -h` for more startup options
web_1   | => Ctrl-C to shutdown server
web_1   | Exiting
web_1   | /home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/connection/ruby.rb:152:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
web_1   |   from /home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/connection/ruby.rb:152:in `connect'
web_1   |   from /home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/connection/ruby.rb:211:in `connect'
web_1   |   from /home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/client.rb:322:in `establish_connection'
web_1   |   from /home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/client.rb:94:in `block in connect'
web_1   |   from /home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/client.rb:279:in `with_reconnect'

以下是Dockerfile。
# Base image.
FROM atlashealth/ruby:2.2.1

# System dependencies for gems.
#RUN apt-get update
#RUN apt-get install -y --no-install-recommends libmysqlclient-dev
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev 

# Add 'web' user which will run the application.
RUN adduser web --home /home/web --shell /bin/bash --disabled-password --gecos ""

# Add directory where all application code will live and own it by the web user.
RUN mkdir /app
RUN chown -R web:web /app

# Install gems separately here to take advantage of container caching of `bundle install`.
# Also, ensure that gems are installed as the web user and not system-wide so that we can run
# `fig web bundle install` and the web user will have permissions to update the shared Gemfile.lock.
ADD Gemfile /app/
ADD Gemfile.lock /app/
RUN chown -R web:web /app
USER web
ENV HOME /home/web
ENV PATH $PATH:/home/web/.gem/ruby/2.2.0/bin
ENV GEM_HOME /home/web/.gem/ruby/2.2.0
ENV GEM_PATH $GEM_HOME
RUN gem install --user-install bundler
WORKDIR /app/
RUN bundle install
USER root

# Add the whole application source to the image and own it all by web:web.
# Note: this is overwritten in development because fig mounts a shared volume at /app.
ADD . /app/
RUN chown -R web:web /app

# Clean up APT and /tmp when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Default command to run when this container is run.
USER web
WORKDIR /app/
CMD ["bundle", "exec", "rails", "server"]

docker-compose.yml

db:  
  image: postgres
  ports:
    - "5432"

redis:  
  image: redis
  ports:
    - "6379"

sidekiq:  
  build: .
  command: bundle exec sidekiq
  links:
    - db
    - redis

web:  
  build: .
  command: bundle exec rails s -b 0.0.0.0
  volumes:
    - .:/app
  ports:
    - "3000:3000"
  links:
    - db
    - redis

我根据 docker-compose run web env 命令输出的内容,设置了 Postgres 和 Redis 主机和端口的环境变量。
命令 docker-compose run web cat /etc/hosts 的输出为:
Starting xyz_db_1...
Starting xyz_redis_1...
172.17.0.32 2b231a706e7b
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.31 redis 799b65ac89cd xyz_redis_1
172.17.0.31 redis_1 799b65ac89cd xyz_redis_1
172.17.0.30 db 0bd898e19822 xyz_db_1
172.17.0.30 db_1 0bd898e19822 xyz_db_1
172.17.0.30 xyz_db_1 0bd898e19822
172.17.0.31 xyz_redis_1 799b65ac89cd

config/sidekiq.yml

concurrency: 25
pidfile: ./tmp/pids/sidekiq.pid
logfile: ./log/sidekiq.log
queues:
  - default
  - [priority, 2]
daemon: true

config/app-config.yml

default: &default
  redis_host: <%= ENV['XYZ_REDIS_1_PORT_6379_TCP_ADDR'] %>
  redis_port: <%= ENV ['XYZ_REDIS_1_PORT_6379_TCP_PORT'] %>
  redis_namespace: 'RAILS_CACHE'

development:
  <<: *default
  redis_host: <%= ENV['XYZ_REDIS_1_PORT_6379_TCP_ADDR'] %>
  redis_port: <%= ENV['XYZ_REDIS_1_PORT_6379_TCP_PORT'] %>

config/initializers/redis.rb

# global variable to access Redis cache
# Requirement: Redis server should be up and running
# at below specified host and port
$redis = Redis.new(
  :host => APP_CONFIG["redis_host"],
  :port => APP_CONFIG["redis_port"]
)

不了解应用程序的情况下很难说这里发生了什么。我建议:不要使用环境变量,而是使用由/etc/hosts提供的主机名(docker-compose run web cat /etc/hosts来检查它们)。例如:只使用redis而不是redis_1。我记得ruby和带有下划线的主机名之间存在问题。 - dnephin
@dnephin,看一下修改。我已经加上了“docker-compose run web cat /etc/hosts”的输出结果。 - Ajeet Khan
不好意思,如果我误解了,请原谅。看起来你的应用程序redis配置不正确。你是只使用redis gem本身,还是使用像sidekiq/activejob这样的库? - Christos Hrousis
@Anti-Fun 使用 Redis gem 和 Sidekiq。 - Ajeet Khan
@AjeetKhan 在 config/initializers/sidekiq.rb 下,您可以配置 Redis 主机名,尝试像这样做:Sidekiq.configure_server do |config| config.redis = { url: 'redis://172.17.0.31:6379' } end您也可以创建一个 Redis 配置文件,在 config/initializers/redis.rb 中设置 $redis = Redis.new(:host => 'localhost', :port => 6379),这可能会更加成功。如果这对错误有所影响,这可能表明我正在正确的轨道上,并且我将写一个适当的答案... - Christos Hrousis
@Anti-Fun,我已经这样做了,但仍然出现相同的错误。 - Ajeet Khan
1个回答

1
REDIS_URL环境变量可用于向Sidekiq和Redis gem传递Redis URL。
可以通过以下方式在docker-compose.yml中设置此环境变量:
db:  
  image: postgres
  ports:
    - "5432"

redis:  
  image: redis
  ports:
    - "6379"

web:  
  build: .
  command: bundle exec rails s -b 0.0.0.0
  volumes:
    - .:/app
  ports:
    - "3000:3000"
  links:
    - db
    - redis
  environment:
    REDIS_URL: "redis://redis:6379"

由于Sidekiq和Redis Gem默认使用REDIS_URL,因此您还需要确保不在配置文件中覆盖这个默认行为。 您不再需要文件config/initializers/redis.rb,并且您的config/app-config.yml应仅包含命名空间。
default: &default
  redis_namespace: 'RAILS_CACHE'

development:
  <<: *default

你是否有一些配置或初始化文件覆盖了 SidekiqRedis 的配置?这可能解释了为什么 REDIS_URL 变量被覆盖。 - haradwaith
config/app-config.ymlconfig/initializers/redis.rb 可能会产生干扰。你的 Redis.new(:host, :post) 正在覆盖配置,所以你的应用程序不会使用 REDIS_URL 环境变量。如果你在这些配置文件中删除主机/端口配置,一切都应该正常工作。 - haradwaith
我在这些配置文件中使用了Docker自动生成的环境变量,为什么还会出现这个问题呢? - Ajeet Khan
也许是 app-config.ymlENV['IMLIBACKEND_REDIS_1_PORT_6379_TCP_PORT'] 之间的额外空格导致的问题? - haradwaith
尝试了两种方法,一是删除多余的空格,二是按照您的建议在app-config.yml和redis.rb中删除了与Redis相关的配置。现在出现以下错误: /myapp/app/utils/common_module/v1/cache.rb:17:in write': undefined method set' for nil:NilClass (NoMethodError) - Ajeet Khan
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接