Reputation: 45
I am trying to connect to a MySQL docker container from a spring boot application. Every time I run my @SpringBootApplication class from eclipse, I am getting
com.mysql.cj.jdbc.exceptions.CommunicationsException: 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.
I am running my docker container with below command line:
docker container run --name expenses-mysql-db --network expensetracker-net -e MYSQL_ROOT_PASSWORD=password -d mysql
Below is my application.properties file content:
spring.datasource.url=jdbc:mysql://expenses-mysql-db:3306/expensedb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create
I expect the successful connection on running the spring api but its failing with above exception. Please help.
Upvotes: 0
Views: 485
Reputation: 683
You have to expose mysql port outside of the docker container. Use -p to expose the ports.
docker container run -p 3306:3306 --name expenses-mysql-db --network expensetracker-net -e MYSQL_ROOT_PASSWORD=password -d mysql
Upvotes: 1