![服务器是否在主机“localhost”(::1) 上运行并接受端口 5432 上的 TCP/IP 连接](https://linux22.com/image/174145/%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%98%AF%E5%90%A6%E5%9C%A8%E4%B8%BB%E6%9C%BA%E2%80%9Clocalhost%E2%80%9D(%3A%3A1)%20%E4%B8%8A%E8%BF%90%E8%A1%8C%E5%B9%B6%E6%8E%A5%E5%8F%97%E7%AB%AF%E5%8F%A3%205432%20%E4%B8%8A%E7%9A%84%20TCP%2FIP%20%E8%BF%9E%E6%8E%A5.png)
我正在尝试使用以下命令连接到 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
但仍然没有喜悦。
你能排除故障吗?
答案1
为我工作;-
在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