I am new baby to the Docker. I am using docker within the spring boot application. Below is my file .
Application.properties file:
spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:3306/${DATABASE_NAME}?allowPublicKeyRetrieval=true&useSSL=true
spring.datasource.username = root
spring.datasource.password = root
server.port=8443
DockerFile:
FROM openjdk:latest
ADD target/app.jar app.jar
EXPOSE 8443
ENTRYPOINT ["java","-jar","app.jar", "--spring.profiles.active=docker"]
Docker Compose :
version: '3.1'
services:
mysqlDb:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=springdemo
- MYSQL_USER=root
- MYSQL_PASSWORD=root
- DATABASE_HOST= 192.168.0.1
volumes:
- /data/mysql
ports:
- "3306:3306"
container_name: mysql-db
springbootdocker:
image: xyz/signup
restart: always
ports:
- "8080:8080"
depends_on:
- mysqlDb
links:
- mysqlDb
environment:
- DATABASE_HOST= mysqlDb
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=springdemo
- DATABASE_PORT=3306
healthcheck:
test: ["CMD","curl","-f","http://localhost:8080/health"]
interval: 1m30s
timeout: 10s
retries: 3
1st part of Issue is Resolved : when i run the App , i get the following error :
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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.
Caused by: java.net.UnknownHostException: ${DATABASE_HOST}
1) What i am trying to Achieve -
- Want to create a jar for my application.
- After creating a jar , i want to create the image from the jar
- After creating the image of the application , i want to push into the docker hub .
- From other server , I will run docker-compose.yml file . It will fetch the mysql and image from the docker hub and run the application in the other port as mentioned in the docker-compose.
Hope I am clear ,what i want to acheive .
Here comes the problem :
when i am trying to run the application locally , i am able to create the jar only when ,i write this line in application.properties class.
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springdemo?allowPublicKeyRetrieval=true&useSSL=true
I am able to create the jar and after that image to -> upload that image to docker hub , when i am running the docker-compose able to pull that image but can't able to connect to the mysql workbench.
So,I thought, I am running the docker-compose in diffrent System/Server. There ip address is different.
So, I changed the url to this :
spring.datasource.url=jdbc:mysql://192.168.0.1:3306/springdemo?allowPublicKeyRetrieval=true&useSSL=true
where
192.168.0.1 - this ip is for the diffrent server, where i want to run the docker-compose file
But when i am running the spring boot application , I am unable to run the application because it's saying the almost the same error as mentioned above i.e Connection refuse . So i am unable to create the jar so , unable to create the image . So my idea fails here also.
- Then Again, One idea again come into my mind , can't we put the dynamic ip address in application.properties file,so i tried this line :
spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:3306/${DATABASE_NAME}?allowPublicKeyRetrieval=true&useSSL=true
DATABASE_HOST- you can find it in docker-compose file
I thought may be this one : ${DATABASE_HOST} , will pick the value of the DATABASE_HOST from the docker-compose .
But my idea failed , I am getting the error as mentioned in top while running the application. can't able to create jar ,so unable to create the image .
2nd Part - Issue is still Open
when i am able to run the docker-compose in my local terminal , and able to create the container . I can also see mysql is connected as well as my schema . But when i am trying to access my API using postman or web browser then , it's saying not connected
http://localhost:8080/dockerex
Inside my Controller Class:
@RestController
@RequestMapping(value = "/dockerex")
public class SpringBootDockerController {
@GetMapping
public String check() {
return "checking";
}
}
when i inspect my container , ip address is coming blank : snapshot of conatiner :
[
{
"Id": "7664bab5ed452e3e2243e1802010240ab5a3eb817af6164e985b10e729ff4c8f",
"Created": "2020-02-04T17:30:08.5351511Z",
"Path": "java",
"Args": [
"-jar",
"app.jar",
"--spring.profiles.active=docker"
],
"State": {
"Status": "running"
.......................
"PortBindings": {
"8080/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},