Issue detail:
I have a docker-compose file with 1 MariaDb database and 1 simple SpringBoot application which connects to the database.
The docker-compose.yml file:
version: '3.9'
volumes:
out:
driver: local
services:
users:
image: <MY_HOST>/users:0.0.1-SNAPSHOT
container_name: users
environment:
spring.datasource.url: jdbc:mariadb://users-db:3306/users
ports:
- "9003:9003"
depends_on:
- users-db
networks:
- my-network
users-db:
image: mariadb:10.6.4
restart: always
container_name: users-db
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: users
MYSQL_USER: testuser
MYSQL_PASSWORD: test
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
networks:
- my-network
volumes:
- ./out/users_db:/var/lib/mysql
networks:
my-network:
driver: bridge
In my SpringBoot application.properties file (users server name in docker-compose.yml) i have the following content:
spring.datasource.url=jdbc:mariadb://localhost:3306/users
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.username=testuser
spring.datasource.password=test
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
logging.level.hibernate=trace
spring.datasource.hikari.data-source-properties.useUnicode=true
spring.datasource.hikari.data-source-properties.characterEncoding=UTF-8
The environment variable is overriding the default value of the mariadb URL inside the application.properties file.
Actually, when only the database is running and i use the localhost property inside my springboot application, i am able to connect to it. When i dockerize my springboot application and i try to use the service-name users-db it is not able anymore.
Here's the output from docker:
java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=users-db)(port=3306)(type=master) : Socket fail to connect to host:users-db, port:3306. Connection refused
at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73) ~[mariadb-java-client-2.7.4.jar!/:na]
at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:197) ~[mariadb-java-client-2.7.4.jar!/:na]
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1394) ~[mariadb-java-client-2.7.4.jar!/:na]
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:635) ~[mariadb-java-client-2.7.4.jar!/:na]
at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:150) ~[mariadb-java-client-2.7.4.jar!/:na]
at org.mariadb.jdbc.Driver.connect(Driver.java:89) ~[mariadb-java-client-2.7.4.jar!/:na]
Does anybody have an idea about what the issue is?