【发布时间】:2019-03-22 18:47:43
【问题描述】:
我检查了许多论坛条目(例如在 stackoverflow 中),但我仍然无法弄清楚我的 docker-compose 文件有什么问题。
所以当我启动我的应用程序 (content-app) 时,我遇到了以下异常:
Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=content-database)(port=3306)(type=master) : Connection refused (Connection refused)
我的应用程序是一个尝试连接数据库的 Spring boot 应用程序,JDBC URL 是
url: jdbc:mariadb://content-database:3306/contentdb?autoReconnect=true
Spring Boot 应用程序运行良好,因为本地(不使用 docker 时)可以连接到本地 mariadb。
所以content-app 容器看不到content-database 容器。我读到如果我指定一个网络并将容器分配给网络,那么它们应该能够相互连接。
当我连接到正在运行的content-app 容器时,我可以远程登录到content-database
root@894628d7bdd9:/# telnet content-database 3306
Trying 172.28.0.3...
Connected to content-database.
Escape character is '^]'.
n
5.5.5-10.4.3-MariaDB-1:10.4.3+maria~bionip/4X@wW/�#_9<b[~)N.:ymysql_native_passwordConnection closed by foreign host.
我的 docker-compose yaml 文件:
version: '3.3'
networks:
net_content:
services:
content-database:
image: content-database:latest
build:
context: .
dockerfile: ./database/Dockerfile
networks:
- net_content
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
content-redis:
image: content-redis:latest
build:
context: .
dockerfile: ./redis/Dockerfile
networks:
- net_content
content-app:
image: content-app:latest
build:
context: .
dockerfile: ./content/Dockerfile
networks:
- net_content
depends_on:
- "content-database"
有什么提示吗?
谢谢!
【问题讨论】:
标签: docker docker-compose