简体   繁体   English

Rails 服务器是否在主机“localhost”(127.0.0.1)上运行并接受端口 5432 上的 TCP/IP 连接?

[英]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.

相关问题 服务器是否在主机“localhost”(::1) 上运行并在端口 5432 上接受 TCP/IP 连接? - Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? linux 中的导轨。 服务器是否在主机“localhost”(::1) 上运行并接受端口 5432 上的 TCP/IP 连接? - Rails in linux. Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432? psql:无法连接到服务器:连接被拒绝服务器是否在主机“ localhost”(:: 1)上运行并在端口5432上接受TCP / IP连接? - psql: could not connect to server: Connection refused Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432? Docker:服务器是否在主机“localhost”(::1) 上运行并接受端口 5432 上的 TCP/IP 连接? - Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? PostgreSQL不接受端口5432上的TCP / IP作为Linux上的Rails - postgresql not accepting tcp/ip on port 5432 for rails on linux 服务器是否在本地运行并接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接? Dockerized Rails - Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? Dockerized Rails 没有这样的文件或目录服务器是否在本地运行并且在Unix域套接字“ /tmp/.s.PGSQL.5432”上接受连接? - No such file or directory Is the server running locally and accepting connections on Unix domain socket “/tmp/.s.PGSQL.5432”? BlueHost-没有这样的文件或目录服务器是否在本地运行并且在Unix域套接字“ /tmp/.s.PGSQL.5432”上接受连接 - BlueHost - No such file or directory Is the server running locally and accepting connections on Unix domain socket “/tmp/.s.PGSQL.5432” 没有这样的文件或目录 服务器是否在本地运行并接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接? 在 docker - No such file or directory Is the server running locally and accepting connections on Unix domain socket “/tmp/.s.PGSQL.5432”? at docker postgresql 数据库错误:服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接? - error with postgresql datababse : Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM