[英]Rails Is the server running on host “localhost” (127.0.0.1) and accepting TCP/IP connections on port 5432?
I'm building a rails app and I'm having trouble installing docker.我正在构建一个 Rails 应用程序,但在安装 docker 时遇到问题。
The build works, but when I try to create the DB with this command:构建工作,但是当我尝试使用此命令创建数据库时:
docker-compose run web bundle exec rails db:create
it throws this error:它抛出这个错误:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
I've try to rename the host from localhost to db, it didn't work and I also tried to edit my etc/hosts.我尝试将主机从 localhost 重命名为 db,但没有成功,我还尝试编辑我的 etc/hosts。
docker-compose.yml docker-compose.yml
version: '3'
services:
localhost:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- localhost
working_dir: /myapp
Dockerfile Dockerfile
FROM ruby:2.6.2-alpine
RUN apk add --no-cache build-base postgresql-dev tzdata libxml2-dev libxslt-dev nodejs yarn
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
COPY package.json /myapp/package.json
RUN gem install bundler:2.1.2
RUN bundle install
COPY . /myapp
RUN yarn install --check-files
database.yml数据库.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: postgres
password:
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
/etc/hosts /etc/hosts
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
127.0.0.1 postgres
How can I fix this issue?我该如何解决这个问题? I'm not sure what I'm missing!
我不确定我错过了什么!
In Docker each "service" runs in its own container.在 Docker 中,每个“服务”都在自己的容器中运行。 So you would have the Rails app and the database in different containers.
因此,您将在不同的容器中拥有 Rails 应用程序和数据库。 Each service is conceptually speaking a different server.
从概念上讲,每个服务都是不同的服务器。
Your error ( Is the server running on host "localhost" (127.0.0.1)..
) suggests that your Rails application is looking for a database engine on the same machine.您的错误(
Is the server running on host "localhost" (127.0.0.1)..
)表明您的 Rails 应用程序正在同一台机器上寻找数据库引擎。 That DB is running on a separate server.该数据库在单独的服务器上运行。
In a docker-compose file each service is addressable by its name.在 docker-compose 文件中,每个服务都可以通过其名称进行寻址。 So change the connection string or setting in your Rails app from
localhost
to db
or something.因此,将 Rails 应用程序中的连接字符串或设置从
localhost
更改为db
或其他内容。
Edit: I just now see that you named your DB server localhost
, that's asking for trouble.编辑:我刚才看到您将数据库服务器命名为
localhost
,这是自找麻烦。 Change that to db
or something?把它
db
什么的? :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.