简体   繁体   中英

Spring boot jar docker deployment throwing Connection Exception with MySQL Container

I just started exploring docker today and I've been trying to spin-up Spring Boot Web app backed by MySQL DB.

I pulled a MySQL container and ran it using:

docker run -t --name mysql-docker-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=**** -e MYSQL_DATABASE=spring_app_db -e MYSQL_USER=app_user -e MYSQL_PASSWORD=**** -d mysql

My application.properties file:

spring.datasource.url=jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.username=app_user
spring.datasource.password=test123

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

with this, when I run my spring boot in IDE it's able to connect and perform CRUD on db table. However, when I try to deploy it on docker and link with MySQL container it throws Connection Refused error. My Dockerfile:

FROM openjdk:11
LABEL maintainer="baljinder@gmail.com"
VOLUME /tmp
EXPOSE 8080
ADD target/demo-0.0.1-SNAPSHOT.jar bootmysql.jar
ENTRYPOINT ["java", "-jar", "bootmysql.jar"]

The command I'm using to run docker image of the boot:

docker run -t --name spring-jpa-container -p 8080:8080 --link mysql-docker-container:mysql spring-jpa-app

Can someone please help with this. I've tried deploying both on the same container network by creating a docker network ( docker network create -d bridge sql_spring_network ) but the error persists.

When using the legacy linking of containers using the --link flag. The "linked" container is available as a host in the running container with it's container name. So in your case, the mysql container is available in your app container with hostname mysql .

Your database url, therefore should be: jdbc:mysql://mysql:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10

use:

jdbc:mysql://mysql-docker-container:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10

replace:

jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM