[英]App container cannot connect to mysql container in the same network
I have a problem with the docker and containerized apps not connecting to mysql database that is running in a container within the same docker network.我的 docker 和容器化应用程序无法连接到在同一 docker 网络内的容器中运行的 mysql 数据库时遇到问题。 My
docker-compose.yml
looks like this:我
docker-compose.yml
看起来像这样:
version: '3.8'
services:
mysqlserver:
image: mysql:5.7.24
container_name: mysql_app
networks:
- somenetwork
ports:
- "5647:3306"
environment:
MYSQL_DATABASE: database
MYSQL_USER: USER
MYSQL_PASSWORD: PASS
MYSQL_ROOT_PASSWORD: PASS
command:
- --max_connections=1000
restart: always
volumes:
- persistent:/var/lib/mysql
app:
image: app/appserver:latest
container_name: app_server
networks:
- somenetwork
ports:
- "8080:8080"
depends_on:
- mysqlserver
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysqlserver:5647/database?verifyServerCertificate=false&useSSL=false&requireSSL=false&autoReconnect=true
networks:
somenetwork:
name:
somenetwork_share_net
volumes:
persistent:
And looking at the logs from the app container, I found errors like this:查看应用程序容器中的日志,我发现了如下错误:
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago.
引起:com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure 最后一个数据包成功发送到服务器是 0 毫秒前。 The driver has not received any packets from the server.
驱动程序没有收到来自服务器的任何数据包。
and和
Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
引起:java.net.ConnectException:在 java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na] 的连接被拒绝(连接被拒绝)
Is there something wrong with my docker-compose.yml
?我
docker-compose.yml
有什么问题吗? Why the app container cannot connect为什么应用容器无法连接
You have to use the exposed port on container side 3303
and not the public one 5647
.您必须使用容器端
3303
上的公开端口,而不是公共端口5647
。
Just try你试一试
SPRING_DATASOURCE_URL: jdbc:mysql://mysqlserver:3306/database?verifyServerCertificate=false&useSSL=false&requireSSL=false&autoReconnect=true
The port in local network stays the original exposed port.本地网络中的端口保持原来的暴露端口。 In port section you just define the public ports on the host machine.
在端口部分,您只需定义主机上的公共端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.