[英]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.