我已经构建了一个 tomcat 应用程序作为我的 api,并使用 postgresql 作为数据库。在正常部署中,应用程序在 ttomcat 应用程序可以很好地访问数据库的情况下工作正常,但是当我通过 docker 测试它时,我收到以下错误:
SEVERE [main] java.sql.SQLException.UserAccounts Cannot create PoolableConnectionFactory (Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.)
以下是我的docker compose文件;
version: "3.8"
services:
# Postgresql
postgres_db:
build: ./database # dockerfile with env and default sql copy
container_name: my_database
restart: always
ports:
- '5432:5432'
volumes:
- postgres_db:/var/lib/postgresql/data
# adminer
adminer:
image: adminer
restart: always
ports:
- 2010:8080
labels:
- "adminer.app.com=Database manager"
# tomcat
tomcat:
build: ./api
container_name: app_api
labels:
- "api.app.com=Application backend API"
environment:
- CATALINA_OPTS=-Xmx1024m
restart: always
depends_on:
- "postgres_db"
ports:
- "2020:8080"
links:
- "postgres_db"
有关如何纠正此 localhost:5432 连接拒绝问题的任何建议。请,我是 docker 的新手。任何帮助将不胜感激。
您需要在此处配置网络,以便您的管理员可以连接到数据库,您可以进行更改并连接到与 postgres_db 相同的网络
版本:“3.8”
服务: postgres_db: build: ./database # 带有 env 和默认 sql 副本的 dockerfile 容器名称:我的数据库 重新启动:始终 端口: - '5432:5432' 卷: - postgres_data:/var/lib/postgresql/data
管理员: 图片:管理员 重新启动:始终 端口: - 2010:8080 标签: - “adminer.app.com=数据库管理器” network_mode: "service:postgres_db" # 连接到与 postgres_db 相同的网络
雄猫: 构建:./api 容器名称:app_api 标签: - “api.app.com=应用程序后端API” 环境: - CATALINA_OPTS=-Xmx1024m 重新启动:始终 依赖于取决于: - postgres_db 端口: -“2020:8080”