[英]The well-known 'Docker containers don't see each other' problem
我检查了很多论坛条目(例如在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启动应用程序,尝试连接到数据库,JDBC URL是
url: jdbc:mariadb://content-database:3306/contentdb?autoReconnect=true
Spring Boot应用程序可以正常工作(当没有使用docker时)可以连接到本地mariadb。
因此content-app
容器看不到content-database
容器。 我读到如果我指定一个网络并将容器分配给网络,那么它们应该能够相互连接。
当我连接到正在运行的content-app
容器时,我可以telnet到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"
有什么提示吗?
谢谢!
您必须在./database/Dockerfile
的Dockerfile中./database/Dockerfile
内容数据库正在侦听的./database/Dockerfile
我猜MariaDB正在监听默认端口3307 ,这意味着你的应用程序也必须连接到这个端口。 我想这就是你正在将容器的端口3307映射到“外部”的情况。
更改连接字符串中的端口:
url: jdbc:mariadb://content-database:3307/contentdb?autoReconnect=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.