Spring Boot Application not able to connect with MySQL in Docker - mysql

While I am going to start my spring boot application image in the docker engine with the below command then I am getting an error
The command which I am using to start my application image
docker run -p 8082:8082 --name e-health e-health
GETTING BELOW ERROR
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:953) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
... 57 common frames omitted
Caused by: java.net.UnknownHostException: mysqldb
at java.net.InetAddress.getAllByName0(InetAddress.java:1281) ~[na:1.8.0_212]
at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
... 60 common frames omitted
I have created a MySQL container with the below command
docker run -d -p 3306:3306 --name mysqldb -e MYSQL_ROOT_PASSWORD=123456789 -e MYSQL_DATABASE=eHealth mysql
I have created my Spring boot application Image with below command
docker build -t e-health .
My Spring Boot project application.properties file looks like below
spring.datasource.name=eHealth
spring.datasource.url=jdbc:mysql://mysqldb:3306/eHealth
spring.datasource.username=root
spring.datasource.password=123456789
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
server.port=8082
My Dockerfile looks like below
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/eHealth.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]
Docker container Looks like
I have followed the below question but it's not helping me
Spring Boot MySQL Docker Caused by: java.net.ConnectException: Connection refused (Connection refused)
Any suggestions or solutions, Please
As per the suggestion I have tried to create a container like below also, But still I am getting the same error
docker run -d -p 3306:3306 --name mysqldb --hostname=mysqldb -e MYSQL_ROOT_PASSWORD=123456789 -e MYSQL_DATABASE=eHealth MySQL
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
469e171ff3e2 mysql "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysqldb

it depends on your use case if you are trying to run both the spring boot application and MySQL in docker, you need to make sure that both containers are in the same network.
one solution is to manually create the networks using the docker command and run both container on this created network. or an easier solution is to write a docker-compose.yml file
by default the docker-compose.yml will create a default network.
docker-compose.yml
version: '3'
services:
mysql-standalone:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=***
- MYSQL_USER=***
- MYSQL_PASSWORD=**
volumes:
- data:/var/lib/mysql
spring-boot:
image: *your-app-name-image*
ports:
[8080:8080]
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysql-standalone
volumes:
data:
you can also create your own network like the following
networks:
mynetwork:
driver: bridge
so your docker-compose.yml file will look like the following
version: '3'
services:
mysql-standalone:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=***
- MYSQL_USER=***
- MYSQL_PASSWORD=**
volumes:
- data:/var/lib/mysql
networks:
- mynetwork
spring-boot:
image: *your-app-name-image*
ports:
[8080:8080]
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysql-standalone
networks:
- mynetwork
volumes:
data:
networks:
mynetwork:
driver: bridge
in your application.yml file you need to have the following
server.port=8080
spring.datasource.url=jdbc:mysql://<container-name>:3306/<db-name>
spring.datasource.username=***
spring.datasource.password=***
I would suggest having multiple profiles using the spring profile.
The second use case is if you are running MySQL in a container and your spring boot application locally, then you need to change this:
spring.datasource.url=jdbc:mysql://mysqldb:3306/eHealth
to
spring.datasource.url=jdbc:mysql://localhost:3306/eHealth
this should solve the error mentioned Caused by: java.net.UnknownHostException: mysqldb.
if you decided that writing a docker-compose.yml file does not suit your use case, check this https://docs.docker.com/engine/reference/commandline/network_create/ to check how to create networks from the command line

Related

Docker, Spring, Mysql java.net.ConnectException: Connection refused (Connection refused) while trying to run docker app

I have a Springboot application which connects to MySQL database. When I try to run the application it gives following exceptions :
- 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.
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
- java.net.ConnectException: Connection refused (Connection refused)
My Dockerfile is this :
FROM openjdk:8
ADD target/studyapp.jar studyapp.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "studyapp.jar"]
My application.properties :
spring.datasource.url=jdbc:mysql://localhost:3306/study # ${MYSQL_HOST:localhost}
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=false
spring.jpa.database=mysql
spring.hibernate.dialect=org.hibernate.dialect.MySQLDialect
security.basic.enable: false
security.ignored=/**
This is the command that I used for running mysql container :
sudo docker run --net studyapp-network --name localhost -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=study -e MYSQL_USER=cnyt -e MYSQL_PASSWORD=root -d mysql:5.7.32
And this is for running the app container :
sudo docker run --net studyapp-network -p 8080:8080 cuneytyvz/studyapp
I'm using 'localhost' as name for my mysql container. I tried to change this, same error occured.
I also can run this app using docker-compose with no problem. But I want to see it working without docker-compose also. What might be the problem?
This is the working docker-compose file :
version: '3.3'
services:
#service 1: definition of mysql database
mysql-db:
image: mysql:latest
container_name: mysql-db
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=cnyt
ports:
- "3306:3306"
restart: always
volumes:
- my-datavolume:/var/lib/mysql
#service 2: definition of phpMyAdmin
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: my-php-myadmin
ports:
- "8082:80"
restart: always
depends_on:
- mysql-db
environment:
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
#service 3: definition of your spring-boot app
studyapp: #it is just a name, which will be used only in this file.
image: studyapp #name of the image after dockerfile executes
container_name: studyapp #name of the container created from docker image
build:
context: . #docker file path (. means root directory)
dockerfile: Dockerfile #docker file name
ports:
- "8080:8080" #docker containter port with your os port
restart: always
depends_on: #define dependencies of this app
- mysql-db #dependency name (which is defined with this name 'db' in this file earlier)
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db:3306/study?createDatabaseIfNotExist=true
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
volumes:
my-datavolume:
It's because I was using 'localhost' as name for my mysql container. It probable confuses my application docker container. I changed it and it works correctly.

Cannot connect MySQL with Spring Boot in docker-compose

I have the problem when running spring boot with mysql using Docker. It runs well locally without Docker.
My dockerfile
FROM maven:3.3-jdk-8-alpine
ENV MAVEN_HOME /usr/lib/mvn
ENV PATH $MAVEN_HOME/bin:$PATH
ENV SPRING_DATASOURCE_URL jdbc:mysql://my-db:3306/registration_springboot?createDatabaseIfNotExist=true
ENV SPRING_DATASOURCE_USERNAME vnphu
ENV SPRING_DATASOURCE_PASSWORD password
docker-compose.yml file
version: '3'
services:
my-db:
image: mysql/mysql-server:5.7
container_name: my-db
environment:
MYSQL_USER: vnphu
MYSQL_DATABASE: registration_springboot
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
ports:
- '3306-3306'
web:
build: .
working_dir: /app
volumes:
- .:/app
- ~/.m2:/root/.m2
ports:
- "8080:8080"
command: mvn clean spring-boot:run
depends_on:
- my-db
The error when run: docker-compose up --build
Caused by: java.net.ConnectException: Connection refused (Connection refused)
web_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
web_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
web_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
web_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
web_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
web_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
web_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.19.jar:8.0.19]
web_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.19.jar:8.0.19]
web_1 | ... 115 common frames omitted
web application is trying to connect to my-db which is not yet available(database is still loading) you can use any of the following solutions depending on your preference.
Restart-on failure
modify docker-compose file to
web:
ports:
"8080:8080"
restart:on-failure
sleep
modify docker-compose file under environment
web:
ports:
"8080:8080"
environment:
-SLEEP_LENGTH=5
wait-for-it.sh file
wait-for-it file
this approach needs you to modify your Dockerfile as well as docker-compose
file
The problem is with my ENV in Dockerfile because I also have an environment variable with the same name in Shell which will take precedence.
ENV SPRING_DATASOURCE_URL jdbc:mysql://my-db:3306/registration_springboot?createDatabaseIfNotExist=true
After adding the variable to environment in the docker-compose.yml, It worked
web:
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://my-db:3306/registration_springboot?createDatabaseIfNotExist=true
docker-compose.yml will look up as below order:
Compose file
Shell environment variables
Environment file
Dockerfile
Variable is not defined
You can refer to this article for more details: https://vsupalov.com/override-docker-compose-dot-env/
Your service and db are not on the same network. Consider defining a network and adding to each of service and db

Can't connect a spring-boot 2 service to mysql in a different container

I have a spring-boot 2.16 service that connects to a my-sql data base. It works as expected as far as all the components are on the same host. I tried to improve the solution and to have a docker container running the sprin-boot service with Tomcat embedded and another docker container running the my-sql server.
The same code doesn't work any more. here is the exception:
2019-06-28 12:23:48 ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
com.mysql.jdbc.exceptions.jdbc4.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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
....
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 66 common frames omitted
So, it's like the my-sql server IP address is not found or the server is not running, etc.
Here is the docker-compse.yml file:
version: "2"
services:
rdf-data:
container_name: rdf-data
image: mysql:latest
environment:
MYSQL_DATABASE: test
MYSQL_USER: nicolas
MYSQL_PASSWORD: mysql
MYSQL_ROOT_PASSWORD: mysql
ports:
- 3306:3306
- 33060:33060
hostname: rdf-data
networks:
net1:
ipv4_address: 192.19.0.4
rdf-core:
image: openjdk:8-jdk-alpine
depends_on:
- rdf-data
container_name: rdf-core
links:
- rdf-data:rdf-data
volumes:
- ../docker:/usr/local/share/rdf
ports:
- "8080:8080"
entrypoint: /usr/local/share/rdf/run.sh
hostname: rdf-core
environment:
PROFILE: "default"
SERVER_PORT: "8080"
networks:
net1:
ipv4_address: 192.19.0.8
networks:
net1:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
ipam:
driver: default
config:
- subnet: 192.19.0.0/24
gateway: 192.19.0.1
Here there are two containers: rfd-core having the IP address of 192.19.0.8 and rdf-data having the IP address 192.19.0.4. Each container is able to ping the other. The hst machine is able to ping each container. From the host machine I'm able to correctly connect to the my-sql server running on rfd-data using the my-sql command line client.
However, my service connection shown here below doesn't work:
database=mysql
spring.datasource.url=jdbc:mysql://192.19.0.4/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class=com.mysql.cj.jdbc.MysqlXADataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
...
Is there anything I'm missing?
Try specifying the parameters of the spring boot app in the environment part of your docker-compose file :
rdf-core:
image: openjdk:8-jdk-alpine
depends_on:
- rdf-data
container_name: rdf-core
links:
- rdf-data:rdf-data
volumes:
- ../docker:/usr/local/share/rdf
ports:
- "8080:8080"
entrypoint: /usr/local/share/rdf/run.sh
hostname: rdf-core
environment:
PROFILE: "default"
SERVER_PORT: "8080"
spring.datasource.url=jdbc:mysql://ipaddress/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class=com.mysql.cj.jdbc.MysqlXADataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
Another possible cause of the problem : the depends_on doesn't guarantee that the dependency container is fully running. so if the MySQL containers takes more time to fully start than the Spring Boot container, the connection fails.
Take a look at this:
Spring Boot + docker-compose + MySQL: Connection refused
There are few possible ways to make a container wait for another:
Use the restart on failure feature, or docker compose wait (
Github link )

SpringBoot on Docker unable to connect to MySQL

I have written docker compose for my SpringBoot app with MySQL. When I run docker-compose up eventually I get an error:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:325) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
... 69 common frames omitted
Caused by: 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.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at java.sql.DriverManager.getConnection(DriverManager.java:664) ~[na:1.8.0_111]
at java.sql.DriverManager.getConnection(DriverManager.java:208) ~[na:1.8.0_111]
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:159) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
... 70 common frames omitted
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
At first I thought the the cause was the db but I managed to run local instance of the app and connect to the docker mysql.
Next I assumed that the spring app tries to connect to mysql before it is up and running but I restarted the app (mysql was already running) and got the same error.
Currently I think that this might be caused by the windows10 firewall but I am running low on ideas.
Dockerfile:
FROM java:8
LABEL maintainer="maciej"
WORKDIR /app
COPY target/kamienica.jar /app/kamienica.jar
ENTRYPOINT ["java", "-jar","kamienica.jar", "--spring.profiles.active=docker"]
docker-compose.yml
version: '3'
services:
mysql-docker-container:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=maciej
- MYSQL_DATABASE=kamienica
- MYSQL_USER=maciej
- MYSQL_PASSWORD=maciej
volumes:
- /data/mysql
ports:
- 3333:3306
container_name: kamienica-db
kamienica-app:
image: kamienica-image
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysql-docker-container
ports:
- 8080:8080
volumes:
- /data/spring-boot-app
container_name: kamienica-app
and the application-docker.properties:
jdbc.url = jdbc:mysql://kamienica-db:3333/kamienica?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username = maciej
jdbc.password = maciej
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.current_session_context_class=thread
hibernate.connection.characterEncoding=UTF-8
hibernate.connection.charSet=UTF-8
Try to change the url into this your properties file
jdbc:mysql://mysql-docker-container:3306/kamienica
jdbc:mysql://[mysql-docker-container-name]:[mysql-port]/[db-name]
in your docker-compose.yml mysql-docker-container-name is mysql-docker-container
so change your spring boot application.properties file mysql url as following
jdbc:mysql://mysql-docker-container:3306/kamienica

Docker Spring MVC

I am totally new to Docker. Now I have this Spring MVC app (running on wildfly) which uses a MySQL DB which i want to Dockerize. I did create a Docker file whose contents are below
FROM jboss/wildfly
# ADD nummerreeks.war /opt/jboss/wildfly/standalone/deployments/
ADD test.war /home/Docker/app
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
And a docker-compose file which is below :
version: '2'
services:
database:
image: mysql:latest
ports:
- 3306
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_USER: test
MYSQL_PASSWORD: admin
MYSQL_DATABASE: dbpass
wildfly:
image: bitnami/wildfly:latest
ports:
- '8080:8080'
- '9990:9990'
links:
- app
volumes_from:
- app
app:
image: test/demo
volumes:
- wars:/opt/jboss/wildfly/standalone/deployments/
links:
- database
volumes:
wars:
driver: local
Now when run this using :
docker run -t -p 8080:8080 --name dockerapp test
I get this DB exception,
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:289)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy56.listRequesterData(Unknown Source)
at nl.naturalis.nrs.controller.NummerreeksController.getRequesterList(NummerreeksController.java:547)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
I believe its reading the db config from the DB connection values from the DB.config file in the application instead of the docker-compose file.
Anyone with any pointers to what am i doing wrong here?

Categories