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

在安装Ruby on Rail时,我在本地系统上运行Docker实例时出现问题。 请参阅我的dockerconfiguration文件: –

Dockerfile

FROM ruby:2.3.1 RUN useradd -ms /bin/bash web RUN apt-get update -qq && apt-get install -y build-essential RUN apt-get -y install nginx RUN apt-get -y install sudo # for postgres RUN apt-get install -y libpq-dev # for nokogiri RUN apt-get install -y libxml2-dev libxslt1-dev # for a JS runtime RUN apt-get install -y nodejs RUN apt-get update # For docker cache WORKDIR /tmp ADD ./Gemfile Gemfile ADD ./Gemfile.lock Gemfile.lock ENV BUNDLE_PATH /bundle RUN gem install bundler --no-rdoc --no-ri RUN bundle install # END ENV APP_HOME /home/web/cluetap_app RUN mkdir -p $APP_HOME WORKDIR $APP_HOME ADD . $APP_HOME RUN chown -R web:web $APP_HOME ADD ./nginx/nginx.conf /etc/nginx/ RUN unlink /etc/nginx/sites-enabled/default ADD ./nginx/cluetap_nginx.conf /etc/nginx/sites-available/ RUN ln -s /etc/nginx/sites-available/cluetap_nginx.conf /etc/nginx/sites-enabled/cluetap_nginx.conf RUN usermod -a -G sudo web 

泊坞窗,compose.yml

 version: '2' services: postgres: image: 'postgres:9.6' environment: - PGDATA=/var/lib/postgresql/data/pgdata - POSTGRES_PASSWORD= - POSTGRES_USER=postgres - POSTGRES_HOST=cluetapapi_postgres_1 networks: - default - service-proxy ports: - '5432:5432' volumes: - 'postgres:/var/lib/postgresql/data' labels: description: "Postgresql Database" service: "postgresql" web: container_name: cluetap_api build: . command: bash -c "thin start -C config/thin/development.yml && nginx -g 'daemon off;'" volumes: - .:/home/web/cluetap_app ports: - "80:80" depends_on: - 'postgres' networks: service-proxy: volumes: postgres: 

当我运行docker-compose builddocker-compose -d这两个命令时,它会成功运行,但是当我从url中find它时,会导致内部服务器错误 ,错误是

 Unexpected error while processing request: 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: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? 

我已经应用了一些解决scheme,但它并没有为我工作,请指导我我是docker和AWS新。

问题是你正试图连接到DB容器内的本地主机。 您为端口映射5432:5432为postgres映射5432到您的主机的本地主机。

现在,您的web容器代码正在容器中运行。 其本地主机上没有任何东西:5432。

因此,您需要在configuration中更改连接详细信息以连接到postgres:5432 ,这是因为您将postgres数据库服务命名为postgres

改变这一点,它应该工作。

默认情况下,postgres图像已经暴露给5432,所以你可以删除你的yml中的那个部分。

然后,如果你想检查Web服务是否可以连接到你的postgres服务,你可以运行这个docker-compose exec web curl postgres:5432然后它应该返回:

curl:(52)来自服务器的空回复

如果无法连接,将返回:

curl:(6)无法parsing主机:postgrescurl:(7)无法连接到postgres端口5432:连接被拒绝

更新:

我现在知道这个问题。 这是因为你正在尝试连接本地主机,你应该连接到postgres服务。