Running Spring Boot app inside Docker container, unable to connect MySQL - mysql

I'm trying to learn Docker and I created a container which is running MySQL server. It works fine and I can use MySQL from my Spring Boot application, when I run Spring Boot application locally (without Docker). But when I try to run Spring Boot application inside another Docker container, connection to MySQL fails and I get error: java.net.ConnectException: Connection refused
In my Spring Boot application.properties I have this configuration:
spring.datasource.url: jdbc:mysql://127.0.0.1/mydb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
Any ideas what might be wrong?

When you want to use the MySQL container from your Spring Boot container a good idea would be to link to it like:
docker run ... --name spring-boot --link mysql ...
Assuming that mysql is the name of your MySQL container you could then use the following JDBC URL in your configuration:
spring.datasource.url: jdbc:mysql://mysql/mydb

The best way to handle this situation (which I used) would be to run your MySQL container in detached mode. Obviously, you can name your container as you like:
docker run --detach --name = my-MySQL --env = "MYSQL_ROOT_PASSWORD=your_password_here" mysql
Your container will be running in detached mode, now you can check the IP and the port on which its running using the inspect command :
docker inspect docker_mysql
And you can check the logs using:-
docker logs my-MySQL
Furthur you can use the IP you get after inspecting. My MySQL was running on 172.17.0.2 and 3306 port the default one:
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/mydb
You can also connect to the MySQL server using any client. I generally use the mysql-client:
sudo mysql -uroot -pyour_password_here -h 172.17.0.2 -P 3306

Related

unable to connect MYSQL docker container from spring boot app

I have MYSQL container Running i am able to execute it and able to connect from host machine using "mysql -h 172.17.0.2 -u root -p". I have executed "docker run --name=mysql-host -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32" to run the docker image.
but when i trying to access through Spring boot app it`s giving connection refused below are the properties in the application.properties
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/auditLog?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
i am runnung my jar as given below
java -jar service-1.0.0.jar spring.config.location=file:///home/10662598/Downloads/entity-search-service-deploymentFolder/entity-search-service/configs/application.properties
Error getting Caused by: java.net.ConnectException: Connection refused (Connection refused)
Your issue is probably because the Spring boot app can't reach the database (mysql), the reason for this is probably because they're not in same network.
If your Spring boot app is running inside container
From your question, I see that you're running containers using docker run..., therefore when you start MySQL and Spring boot app using that command, they're not in same network.
Solution for this is, first create a network, then start MySQL and Spring app to run within that network:
docker network create my-net
Then start your containers as usual but remember to add your containers to the network you just created:
docker run ... --network my-net
Now your containers are in same network and should reach each other, you can verify this by exec to Spring boot app and try curl to Mysql container using its container name (and port):
curl <mysql_container_name>:3306
If Spring boot app is running on host machine
Then simply make sure you map Mysql port to host machine and connect using localhost:<mapped_port>
Good luck :)
Here, you must create a network layer between the containers.
First create network layer
docker network create spring-mysql-net
Build & Run MySql Docker image
docker run --name mysql -d -e MYSQL_ROOT_PASSWORD=root -v
mysql:/var/lib/mysql --network spring-mysql-net mysql:8
Build your spring boot or any API
docker build -t restapi .
Start container on that created the network
docker container run --network spring-mysql-net --name
mysql-restapi-container -p 8080:8080 -d restapi
a bit late but i think you need to forward port 3306 on local machine to the contain port 3306
try this
docker run --name=mysql-host -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32

Spring-boot failing to connect to mysql container even after supplying proper mysql service name

This might sound repeated question but it is not and this is a crazy bug I feel, however, let me quickly explain my setup:
A simple Spring bootstrap application that runs pretty well on my local and JDBC connection string in application.properites file is as follows.
spring.datasource.url=jdbc:mysql://minesql:3306/datamachine?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=****
The docker running instances are:
I copied (with the help of docker cp command) the war file to alpine (unix container) and running it in interactive mode to test and it is throwing exception as it is unable to ping the mysql server. I am certain that the database configurations are fine and clueless why the springboot app is failing to connect to mysql container instance. Note, the mysql container does have "datamachine" database created manually.
This is the error reported:
Please help me understand what I am missing here or what is going wrong.
Just in-case if you wish to know how I started these containers.
For mysql:
docker run -d --name minesql -e MYSQL_ROOT_PASSWORD=**** -p 3306:3306 mysql
Running Java app from the alpine container and this is how I am starting the alpine,
docker run -it --name unix alpine
The interactive mode present me the bash prompt to run the spring-boot war file. (..and running the war file after installing the java 8 in alpine)
You have two docker containers which are running and connected via default bridge network. From docker bridge documentation
Containers on the default bridge network can only access each other by
IP addresses, unless you use the --link option, which is considered
legacy. On a user-defined bridge network, containers can resolve each
other by name or alias.
If you need the second container to be able to resolve the name minesql from inside, you need to create a user-defined network bridge and connect the docker container containers to that.
Create a new network using
docker network create my-net
And add your containers as specified here
Other alternative is to use docker-compose and avoid manual creation of bridge networks for name resolution. For production environment, that would be ideal.

What is the DB_URL has to be in docker and spring boot configuration?

I try to deploy spring boot jar on docker container. But Connection is failed.
The DB_URL in application.properties is like below,
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost/test?characterEncoding=utf8&serverTimezone=Asia/Seoul
spring.datasource.username = root
spring.datasource.password = password
I build mysql docker container with mysql image from docker hub which port number is 3306. When I execute the application jar file on localhost with the following command,
java -Dfile.encoding=UTF8 -jar Spring-Blog-Jpa-0.0.1-SNAPSHOT.jar
it works well without errors. The application can communicate the mysql docker container having port number 3306 on localhost. Next I make docker container of application with below Dockerfile,
FROM openjdk:11-jdk
ADD target/Spring-Blog-Jpa-0.0.1-SNAPSHOT.jar blog-app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Dfile.encoding=UTF8","-jar","blog-app.jar"]
The application docker image is built successfully. But the container can not connect to MySQL Docker container at all. The exception is connection refused.
In localhost, the application Spring-Blog-Jpa-0.0.1-SNAPSHOT.jar can connect to MySQL docker container, But In application docker container connection to MySQL docker container failed. What does DB_URL have to be to connect the mysql container?

Configuring Spring boot Docker and Mysql

I am new to Spring Boot and Docker.
I am trying to create a Spring Boot application connecting to mysql and using Docker to run both.
Steps I followed
Step1 - Created mysql image and started running it.
docker run --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=test" mysql
Step2 Created a SpringBoot application
docker build -f Dockerfile -t gradle-springboot-docker .
Step3 Ran the Spring Boot app and linked with Mysql
docker run -t --name gradle-springboot-docker --link docker-mysql:mysql -p 8080:8080 gradle-springboot-docker
It gives basic connection error to mysql. I have listed the below application.properties. Is my connection information correct since I am using Docker. What would be the host for mysql?
SSL properties
server.port=8080
#DataSource
datasource.driver = com.mysql.jdbc.Driver
datasource.url= jdbc:mysql://localhost:3306/test?
autoReconnect=true&useSSL=false
datasource.username=root
datasource.password=root
# Hibernate
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql = true
hibernate.lazy = true
hibernate.max_fetch_depth = 3
hibernate.packagesToScan = com.springboot.poc
# Once DB is created change below property to 'update'
hibernate.hbm2ddl.auto = update
You should use the container name of mysql as the hostname, since these are linked the mysql container is discoverable by its name from spring. So you need to change datasource.url= jdbc:mysql://localhost:3306/test? to datasource.url= jdbc:mysql://docker-mysql:3306/test?
Or you can use the alias, as #g00glen00b suggested, like: datasource.url= jdbc:mysql://mysql:3306/test?

is it possible to use 3306 port (same port) in docker mysql running container as well as in windows 10 mysql

I deploy my war file in tomcat 7 successfully and start tomcat using following command
docker run -it --rm -p 7008:8080 -v //d/docker_tomcat/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml:ro -v //d/docker_tomcat/webapps:/usr/local/tomcat/webapps:rw tomcat:7.0
when tomcat start it shows following error logs:-
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#9030ca2)
LogAbandoned: true
RemoveAbandoned: true
RemoveAbandonedTimeout: 90
[localhost-startStop-1] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot create PoolableConnectionFactory
i think that above error means that database studentdb is not accessible
here is my hibernate.properties file:-
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.url=jdbc:mysql://10.0.75.x<ip of docker>:3306/studentdb?autoreconnect=true&zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false
show_sql=false
hibernate.jdbc.use_streams_for_binary=false
hibernate.dbcp.testOnBorrow=true
hibernate.dbcp.validationQuery=SELECT 1 FROM DUAL
hibernate.dbcp.testOnReturn=false
hibernate.dbcp.maxWait=2000
hibernate.dbcp.testWhileIdle=true
hibernate.dbcp.minEvictableIdleTimeMillis=1800000
hibernate.dbcp.timeBetweenEvictionRunsMillis=300000
hibernate.dbcp.numTestsPerEvictionRun=5
hibernate.dbcp.removeAbandoned=true
hibernate.dbcp.removeAbandonedTimeout=90
hibernate.dbcp.logAbandoned=true
i think there may be error in hibernate.connection.url property of
hibernate.properties file.
and also doubt is it becuase my windows 10 uses port 3306 for mysql as well as docker also uses port 3306 for mysql. if is it problem then how can i change port of mysql container running in docker with some different port
Use following command to forward your local port to docker container port
docker run -p <LOCAL-PORT>:3306 <mysql-image-name>