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. My docker-compose.yml
looks like this:
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. 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]
Is there something wrong with my docker-compose.yml
? Why the app container cannot connect
CodePudding user response:
You have to use the exposed port on container side 3303
and not the public one 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.