Docker

伺服器是否在主機“localhost”(::1) 上執行並在埠 5432 上接受 TCP/IP 連接

  • May 29, 2020

我試圖連接到 Ruby/Docker 容器中的 Postgres 數據庫;

docker-compose run app rails db:setup

只有我的堆棧跟踪報告;-

Starting reptrax-uk_postgres_1 ... done
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?
Couldn't create database for {"host"=>"localhost", "port"=>5432, "adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"development", "pool"=>10, "username"=>"user", "password"=>"secret"}
rails aborted!
PG::ConnectionBad: 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?

所以我發出一個

netstat -antu | grep 5432

要檢查與違規埠有關的任何內容,但什麼都沒有,它返回空白。

這是我的快照docker-compose.yml

version: '3'

services:
 app:
   build: .
   command: sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
   environment:
     - RABBITMQ_HOST=rabbitmq
     - MONGO_HOST=mongo
     - POSTGRES_HOST=postgres
     - POSTGRES_USER=postgres
     - POSTGRES_PASS=
   volumes:
     - .:/app
     - bundle:/bundle
     - node_modules:/app/node_modules
   ports:
     - "3000:3000"
   depends_on:
     - postgres
     - mongo
     - rabbitmq
     - redis
     - chrome
 postgres:
   image: postgres:9.5
   ports:
     - "5432:5432"
 mongo:
   image: mongo
   ports:

我昨天嘗試了各種 docker 命令,包括;-

docker-compose stop
docker-compose down
docker system prune
docker pull postgres:9.5

但仍然沒有喜悅。

你能排除故障嗎?

為我工作;-

在您docker-compose.yml的 postgres 設置下添加以下條目;-

postgres:
   image: postgres:9.5
   environment:
     - POSTGRES_HOST_AUTH_METHOD=trust
   ports:
     - "5432:5432"

然後在終端 - 繼續; -

docker-compose down
docker container stop $(docker container ls -aq)
docker network create x_middleware
docker network create foo
docker-compose build
docker-compose run app bin/rails db:setup profiles:build
docker-compose run app bin/rails profiles:build
docker-sync-stack start

引用自:https://unix.stackexchange.com/questions/589474