Configuring Spring boot Docker and Mysql - 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?

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

Flyway Migrations fails when passing Environment Variables to Docker

Performing database migrations using flyway. Using the dockerized version and using conf files for mirgations configurations.
Below is my config file
flyway.url = jdbc:mysql://${MYSQLHOST}:3306/myschema
flyway.user = myusername
flyway.password = mypassword
flyway.schemas = myschema
flyway.cleanDisabled = true
Am running the below command to perform migration
sudo docker run -e "MYSQLHOST=myhostip" --rm -it -v `pwd`/path/to/confi/:/flyway/conf/ -v `pwd`/path/to/migrations:/flyway/sql boxfuse/flyway:5.1.4 -configFiles=/flyway/conf/flyway.conf migrate
Am getting the below error
ERROR:
Unable to obtain connection from database (jdbc:mysql://${MYSQLHOST}:3306/myschema) for user 'myuser': Could not connect to address=(host=${MYSQLHOST})(port=3306)(type=master) : ${MYSQLHOST}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State : 08
Error Code : -1
Message : Could not connect to address=(host=${MYSQLHOST})(port=3306)(type=master) : ${MYSQLHOST}
If I change the config file with my host ip details the migrations are successful without any errors. What am I doing wrongly?
Flyway allows you to pass in configuration parameters via a specific set of environment variables; it doesn't perform a complete variable substitution in the config file. See https://flywaydb.org/documentation/envvars.
What you could do is delete the first line of the config file, then
sudo docker run -e "FLYWAY_URL=jdbc:mysql://myhostip:3306/myschema" ...
I found it not very helpful to only see a page explaining that there are envVars -- but no mention of the exact env variable names to actually use.
Here is the definition of the env names / especially for the gradle plugin but I suspect that they are generally valid:
https://github.com/flyway/flyway/blob/master/flyway-core/src/main/java/org/flywaydb/core/internal/configuration/ConfigUtils.java#L139

Not able to connect to mysql running in docker from another docker container

I have a docker container running a mysql image,docker run command -
docker run --name=mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql/mysql-server:5.7
I am able to connect to this database from my SpringBoot app on localhost:3306, but when I run my springboot app -docker run command-
docker run --name=myservice -d -p 3306:3306 -p 8888:8888 <image-id>
it fails to start up and upon checking the logs I see, the following exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'liquibase' defined in class path resource
[org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration
$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is
liquibase.exception.DatabaseException:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
How do I make my spring boot app connect to the container running the mysql?
EDIT: adding the data-source part of my application.yml file for my app :
spring:
datasource:
url: jdbc:mysql://localhost:3306/<db-name>
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
jpa:
show-sql: true
hibernate:
ddl-auto: none
h2:
console:
enabledt: true
Got it working by using --link mysql in the docker run command and changing datasource url from 'localhost:3306' to 'mysql:3306' in my application.yml , as by default Docker has a DNS and it can call containers by name.So, once linked and adding grant permission for my user 'root' for all IPs in my DB by bashing into 'mysql' container, I was able to start my service with the DB in another container. However, for a more general solution , creating a network or using docker compose seems like a better idea

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?

Running Spring Boot app inside Docker container, unable to connect 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