首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >/home/web/.gem/ruby/2.2.0/gems/redis-3.2.1/lib/redis/connection/ruby.rb:152:in‘getaddrinfo’:getaddrinfo:名称或服务未知(SocketError)

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

Stack Overflow用户
提问于 2015-10-08 23:26:13
回答 1查看 1.7K关注 0票数 6

我正在使用坞-撰写为开发人员提供的技术栈ROR,postgres,redis,mongo的开发环境。docker-compose build正在成功运行,但当我运行docker-compose up时,遇到了以下错误。

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
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输出

代码语言:javascript
复制
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/config kiq.yml

代码语言:javascript
复制
concurrency: 25
pidfile: ./tmp/pids/sidekiq.pid
logfile: ./log/sidekiq.log
queues:
  - default
  - [priority, 2]
daemon: true

config/app-config.yml

代码语言:javascript
复制
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'] %>

配置/初始化器/redis.rb

代码语言:javascript
复制
# 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"]
)
EN

回答 1

Stack Overflow用户

发布于 2015-10-14 11:17:25

REDIS_URL环境变量可用于将Redis传递给Sidekiq和Redis。

这个环境变量可以在docker-compose.yml中设置如下:

代码语言:javascript
复制
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在默认情况下将使用REDIS_URL,因此您还需要确保没有在配置文件中覆盖此默认行为。您不再需要文件config/initializers/redis.rb了,您的config/app-config.yml应该只包含您的命名空间:

代码语言:javascript
复制
default: &default
  redis_namespace: 'RAILS_CACHE'

development:
  <<: *default
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33027564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档