【发布时间】: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