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"
}
]
},
Good it worked but here you see you were using DB_HOST earlier, now you are using DATABASE_HOST= mysqlDb , which is currently pointing to your mysql service name.
this service name can be discovered within the single network created by docker compose.
You were hardcoding DB_HOST=192.168.0.1 and trying to connect using this IP , which is invalid as docker compose create its network.
You have to mention service name here as in your case db service name is mysqlDb. Below is the updated application.properties
spring.datasource.url=jdbc:mysql://$(DB_HOST:mysqlDb):3306/springdemo?allowPublicKeyRetrieval=true&useSSL=true
spring.datasource.username = root
spring.datasource.password = root
server.port=8443
Try this:
Replace DB_HOST= 192.168.0.7 to DB_HOST= mysqldb in your docker-compose file.
And update your application.properties file:
spring.datasource.url=jdbc:mysql://$(DB_HOST:mysqlDb):3306/springdemo?allowPublicKeyRetrieval=true&useSSL=true
to this:
spring.datasource.url=jdbc:mysql://$(mysqlDb):3306/springdemo?allowPublicKeyRetrieval=true&useSSL=true
Thanks everyone : I got the solution by hit and trail :P
spring.datasource.url=jdbc:mysql://${DATABASE_HOST:localhost}:${DATABASE_PORT:3306}/springdemo?allowPublicKeyRetrieval=true&useSSL=true
Related
I have a problem here that I really cannot understand. I already saw few topics here with the same problem and those topics was successfully solved. I basically did the same thing and cannot understand what I'm doing wrong.
I have a Spring application container that tries to connect to a Mongo container through the following Docker Composer:
version: '3'
services:
app:
build: .
ports:
- "8080:8080"
links:
- db
db:
image: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"
In my application.properties:
spring.data.mongodb.uri=mongodb://db:27017/app
Finally, my Dockerfile:
FROM eclipse-temurin:11-jre-alpine
WORKDIR /home/java
RUN mkdir /home/java/bar
COPY ./build/libs/foo.jar /home/java/bar/foo.jar
CMD ["java","-jar", "/home/java/bar/foo.jar"]
When I run docker compose up --build I got:
2022-11-17 12:08:53.452 INFO 1 --- [null'}-db:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server db:27017
Caused by: java.net.UnknownHostException: db
Running the docker compose ps I can see the mongo container running well, and I am able to connect to it through Mongo Compass and with this same Spring Application but outside of container. The difference running outside of container is the host from spring.data.mongodb.uri=mongodb://db:27017/app to spring.data.mongodb.uri=mongodb://localhost:27017/app.
Also, I already tried to change the host for localhost inside of the spring container and didnt work.
You need to specify MongoDB host, port and database as different parameters as mentioned here.
spring.data.mongodb.host=db
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
As per the official docker-compose documentation the above docker-compose file should worked since both db and app are in the same network (You can check if they are in different networks just in case)
If the networking is not working, as a workaround, instead of using localhost inside the spring container, use the server's IP, i.e, mongodb://<server_ip>:27017/app (And make sure there is no firewall blocking it)
So, I searched around for an answer on this matter but either people don't address the issue or they say there's no problem doing this on their computer (mac or linux). It seems like this might be a windows problem.
I have a spring api running on a docker container (linux container). I use docker desktop on windows and I'm trying to make a request (in insomnia/postman/wtv) to that api.
If I run the api locally making the following request works perfectly:
http://localhost:8080/api/task/
This will list multiples task elements.
I've containerized this application like so:
Dockerfile
FROM openjdk:11.0.7
COPY ./target/spring-api-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
RUN sh -c 'touch spring-api-0.0.1-SNAPSHOT.jar'
ENTRYPOINT ["java", "-jar", "spring-api-0.0.1-SNAPSHOT.jar"]
docker-compose.yml
version: '3.8'
services:
api:
build: .
depends_on:
- mysql
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/test?createDatabaseIfNotExist=true
ports:
- "8080:80"
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=root
- MYSQL_PASSWORD=root
- MYSQL_DATABASE=test
If I do docker-compose up this works without issue:
The problem is, if I try to call the same endpoint as before from localhost I don't get any response.
Insomnia returns an error saying: Error: Server returned nothing (no headers, no data)
I've also tried connecting to the container's ip (got it from docker inspect) but no luck.
Ports are exposed in docker-compose.yml. What am I missing?
Thanks in advance.
Port mapping is incorrect.Spring boot application started at 8080 (from the image I see) inside container and it should be mapped to 8080 inside the container.
It should be like below:
ports:
- "8080:8080"
I have some issues to dockerize my Spring boot Cassandra application.
When I use Idea to run my project it's work fine correctly but when I turn my application into a docker container I have this issues :
"RetryingCassandraClusterFactoryBean : All host(s) tried for query failed (tried: localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect))"
my application.properties:
spring.data.cassandra.keyspace-name=keyspace
spring.data.cassandra.contact-points=127.0.0.1
spring.data.cassandra.port=9042
spring.data.cassandra.schema-action=create_if_not_exists
server.port=8081
my cassandra :
cassandradb:
image: "cassandra:latest"
container_name: "cassandradb"
ports:
- "9042:9042"
- "7191:7191"
- "7001:7001"
- "9160:9160"
- "7000:7000"
environment:
CASSANDRA_CLUSTER: "myCluster"
CASSANDRA_ENDPOINT_SNITCH: "GossipingPropertyFileSnitch"
CASSANDRA_DC: "data"
CASSANDRA_LISTEN_ADDRESS: "auto"
photos:
image: "photos:latest"
container_name: "photo"
ports:
- "8081:8081"
links:
- "cassandradb"
Error happens, becouse your spring cant reach cassandradb. You should connect or via host ip, or usecassandradb` host, anc configure docker-compose.yml links part
I develop a simple example about the CRUD spring boot application by Cassandra.
You can check it in github. here are a few differences in the comparison of mine.
I have created a spring app and i want to connect it to redis server which is deployed on docker-compose i put the needed properties as follow :
spring.redis.host=redis
spring.redis.port=6379
But i keep getting a ConnexionException so how can i Know on which host redis is running and how to connect to it.
Here is my docker-compose file :
version: '2'
services:
redis:
image: 'bitnami/redis:5.0'
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
ports:
- '6379:6379'
volumes:
- 'redis_data:/bitnami/redis/data'
volumes:
redis_data:
driver: local
From docker compose documentation
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name
If you want to access redis by container name ('redis' in this case), the Spring boot application has also be deployed as a docker compose service, but it doesn't appear in the docker-compose file that you've provided in the question, so please add it.
Alternatively If you're trying to run the spring boot application on host machine, use 'localhost' instead of 'redis' in order to access the redis container.
Another approach you can use is "docker network" , Below are the steps to follow :
Create a docker network for redis
docker network create redis-docker
Spin up redis container is redis-docker network.
docker run -d --net redis-docker --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
Inspect the redis-docker container
docker network inspect redis-docker
Copy the "IPv4Address" IP and paster in application.yml
Now build , start your application.
I am developing a web application by using the Spring Boot, Spring Cloud Config and Docker. There are 3 projects. One is springboot web, Spring Cloud Config Server and MySql Database. I have use the Spring Boot Web with Themeleaf. It's basically perform a CRUD operation. In the development phase it's working fine. But when I deploy it in the Docker
Spring-Boot webapp container(springboot-thymeleaf-web-crud) is giving me the following error--
APPLICATION FAILED TO START
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
I have created 3 container on Docker Compose file.
version: '3'
services:
springboot-thymeleaf-web-crud:
image: numery/springboot-thymeleaf-web-crud:latest
networks:
- employee-network
ports:
- 8080:8080
depends_on:
- employee-database
- spring-cloud-config-server-employee
spring-cloud-config-server-employee:
image: numery/spring-cloud-config-server-employee:latest
networks:
- employee-network
ports:
- 8888:8888
employee-database:
image: mysql:5
networks:
- employee-network
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_DATABASE=employee_directory
volumes:
- /home/numery/Docker-Database/employee_directory:/var/lib/mysql
networks:
employee-network:
SpringBoot Web Application.properties is given below
spring.application.name=employee-service-mysql
server.port=8080
spring.cloud.config.uri=http://spring-cloud-config-server-
employee:8888
spring.profiles.active=dev
SpringBoot Config Server is providing the following properties--
spring.datasource.url: jdbc:mysql://employee-
database:3306/employee_directory?
useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username: root
spring.datasource.password: rootpassword
spring.datasource.validationQuery: SELECT 1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.tomcat.max-wait: 20000
spring.tomcat.max-active: 50
spring.tomcat.max-idle: 20
spring.tomcat.min-idle: 15
spring.jpa.properties.hibernate.dialect:
org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql: true
spring.jpa.format-sql: true
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: create
Out of these 3 containers Mysql and Spring Cloud Config container is working fine. But for the Spring Boot Webapp(springboot-thymeleaf-web-crud) is exiting by giving the error above.
Note: I use the same datasouce(Spring Data JPA) configuration on some other SpringBoot Rest API. Which are working fine. But this is the first time I am using SpringBoot Web. Do I need to explicitly define any configuration on the data-source.
Please help!!
The datasource URL, that is spring.datasource.url: jdbc:mysql://employee- may be wrong.
OR Should add your datasource url to your Application.properties file
The url should be in the format jdbc:oracle:thin:#hostname:portNumber/schemaName
For further details and clarification, Not using the hostname and port in jdbc url
I perform the following task to solve the problem--
1) We should have up and running the mysql
container.
2) Inspect mysql container which ip running--
docker inspect mysqlcontainer | grep IP
3) Add the IP of that mysql container to my
springboot web app--
"spring.datasource.url:
jdbc:mysql://172.20.0.2:3306/employee_directory?
useSSL=false&serverTimezone
=UTC&useLegacyDatetimeCode=false"
4) run mvn clean, mvn install and build the
SpringBoot Web image.
5) If we down the container we need to perform the
same task again. Because each time Mysql
container obtain the different ip.