I’m trying to connect to my container with my empty java maven project. But I received an error Socket fail to connect to host:address=(host=studentdb)(port=3306)(type=primary). studentdb
What would be the problem in my docker-compose.yml
version: '3.8'
services:
mariadb:
build:
context: .
container_name: studentdb
networks:
- default
ports:
- "3306:3306"
expose:
- "3306"
volumes:
- ./data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_APPLICATION_USER}
MYSQL_PASSWORD: ${DB_APPLICATION_PASSWORD}
And this is my application.properties:
spring.application.name=student
spring.datasource.url=jdbc:mariadb://studentdb:3306/student
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.javax.persistence.validation.mode=none
server.error.whitelabel.enabled=false
2
Answers
How you connect to the database will depend on whether you are connecting from the host or from another container.
I’m assuming that you have an
.env
file set up with the variables referenced in yourdocker-compose.yml
.From Host
Since you are exposing port 3306 on the
mariadb
service you can connect to this directly from the host using 127.0.0.1 as the database IP.Note: I’m using the username and password from
.env
.From Another Container
If you are connecting to the database from another container (on the same Docker network) then you’d use either the service name (
mariadb
) or the container name (studentdb
) rather than an IP address.For example, adding another service to the Docker Compose stack above.
The command waits 5 seconds (crude way to ensure that the database is ready) and then connects using the credentials defined in
.env
andmariadb
as the database host name.🚨 If you want to use the service or container name to reference the database host then you should do so from another container in the same Docker network.
When running your Maven project locally with MariaDB deployed in Docker, you should connect to MariaDB using localhost if MariaDB is also running locally. However, if MariaDB is deployed remotely, you should use the IP address of the remote host instead of localhost.
Docker Compose does create a network automatically which allows containers within the same Docker Compose project to communicate using service names as hostnames. However, this only works if both the Maven application and MariaDB are running as services within the same Docker Compose configuration. If your Maven project is not running inside a Docker container managed by the same Docker Compose that manages your MariaDB container, you won’t be able to use the service name directly. Instead, you would need to use localhost to connect to MariaDB when it’s deployed locally, or the public IP address of the server when it’s deployed remotely.
Remember, when using Docker Compose, the network created isolates your services from external access by default, but all services within the same Compose file can access each other by their respective service names. If your setup does not allow using service names (e.g., Maven running natively on your host machine outside Docker), then the connection endpoint must be adjusted as described above.