【问题标题】:Docker error Getaddrinfo: Name or service not known in Puma Nginx server-Mac osDocker 错误 Getaddrinfo: Name or service not known in Puma Nginx server-Mac os
【发布时间】:2019-03-12 10:54:26
【问题描述】:

我在运行docker run myapp 时遇到以下错误:

! Unable to load application: SocketError: getaddrinfo: Name or service not 
     known
     bundler: failed to load command: puma (/usr/local/bundle/bin/puma)

以下是我的配置和 Dockerfile:

应用程序中的 DockerFile

FROM ruby:2.5.0
# Set an environment variable where the Rails app is installed to inside of Docker image
ENV RAILS_ROOT /Users/admin/git/generic/myapp
ENV REDIS_URL=redis://redis:6379/0
RUN mkdir -p $RAILS_ROOT 
# Set working directory
WORKDIR $RAILS_ROOT
# Setting env up
ENV RAILS_ENV='production'
ENV RACK_ENV='production'
# Adding gems
#COPY ./Users/admin/git/generic/myapp/Gemfile ./Users/admin/git/generic/myapp/app/docker/app/Gemfile
#COPY ./Users/admin/git/generic/myapp/Gemfile.lock ./Users/admin/git/generic/myapp/app/docker/web/Gemfile.lock

COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock

RUN bundle install
# Adding project files
COPY . .
RUN gem install bundler
RUN bundle install
COPY . .
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb","production"]

Web 中的 DockerFile

FROM nginx
# Install dependencies
RUN apt-get update -qq && apt-get -y install apache2-utils
# establish where Nginx should look for files
ENV RAILS_ROOT /Users/admin/git/generic/myapp
# Set our working directory inside the image
WORKDIR $RAILS_ROOT
# create log directory
RUN mkdir log
# copy over static assets
COPY public public/
# Copy Nginx config template
COPY docker/web/nginx.conf /tmp/docker.nginx
# put the final config in its place
RUN envsubst '$RAILS_ROOT' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf
EXPOSE 80
# Use the "exec" form of CMD so Nginx shuts down gracefully on SIGTERM (i.e.  `docker stop`)
CMD [ "nginx", "-g", "daemon off;" ]

我的 Nginx 配置

 upstream docker { 
     server 127.0.0.1:3000; 
 }

 server {  
     # define your domain  
     # define the public application root  
     listen 80;
     root   $RAILS_ROOT/public;  
     index  index.html;
     # define where Nginx should write its logs  
     access_log $RAILS_ROOT/log/nginx.access.log;  
     error_log $RAILS_ROOT/log/nginx.error.log;   


      # send non-static file requests to the app server  
      location / { 
          proxy_pass http://docker; 
      } 
} 

docker-compose.yml

version: '3'
volumes:  
  postgres_data: {} 

services:
  redis:
    image: redis
    command: redis-server
    ports:
      - "6379:6379"
  app:    
    build:      
      context: .      
      dockerfile: /Users/admin/git/generic/myapp/docker/app/DockerFile 
    depends_on:      
      - db  
  db:    
    image: postgres    
    volumes:      
      - postgres_data:/var/lib/postgresql/data  
  web:    
    build:      
      context: .      
      dockerfile: /Users/admin/git/generic/myapp/docker/web/DockerFile  
    depends_on:      
      - app    
    ports:      
      - 80:80

Puma.rb

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
 threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'production'

on_worker_boot do
ActiveRecord::Base.establish_connection
end

谁能帮助解决这个套接字错误。本地主机已添加到我的主机文件中。我也尝试过更改 nginx 配置。我仍然面临同样的问题。

【问题讨论】:

  • 可以添加config/puma.rb的内容吗?
  • 我也添加了 puma.rb 配置
  • 我尝试使用 docker-machine env myapp 检查 docker 机器的 ip,但我得到的 Docker 机器“myapp”不存在。
  • 现在我在运行 localhost:3000 时遇到 postgres 错误
  • 无法连接到服务器:连接被拒绝服务器是否在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:无法分配请求的地址服务器是否在主机“localhost”(::1) 上运行并接受端口 5432 上的 TCP/IP 连接?

标签: ruby-on-rails docker nginx docker-compose


【解决方案1】:

在 docker compose 上下文中,每个容器都有自己的 IP 地址。您可以使用 docker compose 服务名称来寻址这些容器,就好像它是它们的主机名一样。

你的 nginx 配置应该是:

upstream docker { 
     server app:3000; 
}

由于appweb 容器的IP 地址不同,您还需要确保app 容器进程绑定到0.0.0.0:3000。我不熟悉 rails 或 puma,但您似乎可以在 app Dockerfile 中使用 CMD ["bundle", "exec", "puma", "-C", "config/puma.rb", "production", "-b", "tcp://0.0.0.0:3000"] 执行此操作。

【讨论】:

  • 好的,谢谢。我进行了更改。我正在再次运行 docker-compose build。
  • 我仍然面临问题。
  • docker-compose up 已经启动 puma 没有任何问题。但是 docker run myapp 得到这个错误
  • !无法加载应用程序:SocketError: getaddrinfo: Name or service not known bundler: failed to load command: puma (/usr/local/bundle/bin/puma)
  • docker compose up 和 docker run 有什么区别?
猜你喜欢
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 2019-08-22
  • 2012-11-08
  • 2021-08-07
  • 1970-01-01
相关资源
最近更新 更多