Reputation: 239
I try to connet my spring boot app with MySQL Docker Container. I ran :
docker run --name mysql-demo -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:lat
and got
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d540d1824b9 mysql:latest "docker-entrypoint.s…" 23 minutes ago Up 23 minutes 3306/tcp, 33060/tcp mysql-demo
In my spring app I have a property file:
spring.datasource.url=jdbc:mysql://mysql-demo:3306/test
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
when I try to run this app or build a jar it crash with this error
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
The app working if I use mysql locally without docker
Upvotes: 1
Views: 68
Reputation: 239
I ran the 2 containers in the same network
more info here: how to access mysql docker container in spring boot docker container
Upvotes: 0
Reputation: 36163
You didn't expose the port 3306. You must do that by using -p3306:3306 otherwise you cannot access this port.
docker run -p3306:3306 --name mysql-demo -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:lat
Upvotes: 1