[英]The well-known 'Docker containers don't see each other' problem
I checked many forum entries (eg in stackoverflow too) but I still cannot figure out what the problem is with my docker-compose file. 我检查了很多论坛条目(例如在stackoverflow中)但我仍然无法弄清楚我的docker-compose文件有什么问题。
So when I start my application ( content-app
) I got the following exception: 因此,当我启动我的应用程序(
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)
My application is a Spring boot app that tries to connect to the database, the JDBC URL is 我的应用程序是一个Spring启动应用程序,尝试连接到数据库,JDBC URL是
url: jdbc:mariadb://content-database:3306/contentdb?autoReconnect=true
The Spring Boot app works fine as locally (when no docker is used) can connect to the local mariadb. Spring Boot应用程序可以正常工作(当没有使用docker时)可以连接到本地mariadb。
So the content-app
container don't see the content-database
container. 因此
content-app
容器看不到content-database
容器。 I read that if I specify a network and I assign the containers to the network then they should be able to connect to each other. 我读到如果我指定一个网络并将容器分配给网络,那么它们应该能够相互连接。
When I connect to the running content-app
container then I can telnet to 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.
My docker-compose yaml file: 我的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"
Any hint please? 有什么提示吗?
Thanks! 谢谢!
您必须在./database/Dockerfile
的Dockerfile中./database/Dockerfile
内容数据库正在侦听的./database/Dockerfile
I guess MariaDB is listening on default port 3307 , this means your application has to connect to this port as well. 我猜MariaDB正在监听默认端口3307 ,这意味着你的应用程序也必须连接到这个端口。 I guess this is the case as you are mapping the port 3307 of your container to "the outside".
我想这就是你正在将容器的端口3307映射到“外部”的情况。
Change the port in your connection string: 更改连接字符串中的端口:
url: jdbc:mariadb://content-database:3307/contentdb?autoReconnect=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.