ruby-on-rails - Docker Postgres 连接问题
问题描述
在尝试打开 localhost:3000 时,我在使用 postgres 的 docker 中遇到以下问题
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?
在我正在使用的 docker-compose 文件下方
version: '3'
volumes:
postgres_data: {}
services:
redis:
image: redis
command: redis-server
ports:
- "6379:6379"
app:
build:
context: .
dockerfile: /Users/admin/git/generic/myapp/docker/app/Dockerfile
depends_on:
- db
ports:
- 3000:3000
db:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
web:
build:
context: .
dockerfile: /Users/admin/git/generic/myapp/docker/web/Dockerfile
depends_on:
- app
ports:
- 80:80
有人可以帮忙吗?
解决方案
将此配置用于 docker-compose.yml:
version: '3.5'
services:
redis:
image: redis
command: redis-server
ports:
- "6379:6379"
app:
build:
context: .
dockerfile: /Users/admin/git/generic/myapp/docker/app/Dockerfile
depends_on:
- db
ports:
- "3000:3000"
networks:
services-network:
aliases:
- app
web:
build:
context: .
dockerfile: /Users/admin/git/generic/myapp/docker/web/Dockerfile
depends_on:
- app
ports:
- "80:80"
networks:
services-network:
aliases:
- web
db:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=db_name
expose:
- "5432"
networks:
services-network:
aliases:
- db
volumes:
postgres_data:
networks:
services-network:
name: services-network
driver: bridge
推荐阅读
- php - PHP pdo lastInsertId() 总是返回一个 0
- wso2 - 无法在 WSO2 API 管理器中使用 REST API 生成应用程序密钥
- html - 表中的子行与主表列对齐
- python - Pyspark - 在循环中应用函数时 DataFrame 未更新
- typescript - TypeScript(err:2532):对象可能是未定义的,尽管有空/未定义检查
- c# - 将 XLS 文件转换为 CSV 但错误编号单元格
- makefile - 为什么注释会使我的 Makefile 命令无效?
- c# - 如何在 Azure 服务总线选项的 MessageHandlerOptions 中设置自动完成(使用 Net Core 3.1)
- php - 如何循环 SELECT 查询,直到它在数据库中找到数据(PHP 到 MySQL)
- python - 在 python Dataframe 中遍历字典