Docker Spring MVC - mysql

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?

Related

springboot docker cannot connect to mysql when using docker-compose [duplicate]

I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. I initially put just the Postgres in Docker, and I had a docker-compose.yml file defined like this:
version: '2'
services:
db:
container_name: sample_db
image: postgres:9.5
volumes:
- sample_db:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=sample
- POSTGRES_USER=sample
- POSTGRES_DB=sample
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- 5432:5432
volumes:
sample_db: {}
Then, when I issued the commands sudo dockerd and sudo docker-compose -f docker-compose.yml up, it was starting the database. I could connect using pgAdmin for example, by using localhost as server and port 5432. Then, in my Spring Boot app, inside the application.properties file I defined the following properties.
spring.datasource.url=jdbc:postgresql://localhost:5432/sample
spring.datasource.username=sample
spring.datasource.password=sample
spring.jpa.generate-ddl=true
At this point I could run my Spring Boot app locally through Spring Suite, and it all was working fine. Then, I wanted to also add my Spring Boot app as Docker image. I first of all created a Dockerfile in my project directory, which looks like this:
FROM java:8
EXPOSE 8080
ADD /target/manager.jar manager.jar
ENTRYPOINT ["java","-jar","manager.jar"]
Then, I entered to the directory of the project issued mvn clean followed by mvn install. Next, issued docker build -f Dockerfile -t manager . followed by docker tag 9c6b1e3f1d5e myuser/manager:latest (the id is correct). Finally, I edited my existing docker-compose.yml file to look like this:
version: '2'
services:
web:
image: myuser/manager:latest
ports:
- 8080:8080
depends_on:
- db
db:
container_name: sample_db
image: postgres:9.5
volumes:
- sample_db:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=sample
- POSTGRES_USER=sample
- POSTGRES_DB=sample
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- 5432:5432
volumes:
sample_db: {}
But, now if I issue sudo docker-compose -f docker-compose.yml up command, the database again starts correctly, but I get errors and exit code 1 for the web app part. The problem is the connection string. I believe I have to change it to something else, but I don't know what it should be. I get the following error messages:
web_1 | 2017-06-27 22:11:54.418 ERROR 1 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
web_1 |
web_1 | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
Any ideas?
Each container has its own network interface with its own localhost. So change how Java points to Postgres:
spring.datasource.url=jdbc:postgresql://localhost:5432/sample
To:
spring.datasource.url=jdbc:postgresql://db:5432/sample
db will resolve to the proper Postgres IP.
Bonus. With docker-compose you don't need to build your image by hand. So change:
web:
image: myuser/manager:latest
To:
web:
build: .
I had the same problem and I lost some time to understand and solve this problem:
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
I show all the properties so that everyone understands.
application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.hibernate.ddl-auto=update
docker-compose.yml:
version: "3"
services:
springapp:
build: .
container_name: springapp
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb
ports:
- 8000:8080
restart: always
depends_on:
- db
db:
image: postgres
container_name: db
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=testdb
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- 5000:5432
volumes:
- pgdata:/var/lib/postgresql/data
restart: always
volumes:
pgdata:
For start spring application with local database we use url localhost.
For connect to container with database we need change 'localhost' on your database service, in my case 'localhost' to 'db'.
Solution: add SPRING_DATASOURCE_URL environment in docker-compose.yml wich rewrite spring.datasource.url value for connect:
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb
I hope this helps someone save his time.
You can use this.
version: "2"
services:
sample_db-postgresql:
image: postgres:9.5
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=sample
- POSTGRES_USER=sample
- POSTGRES_DB=sample
volumes:
- sample_db:/var/lib/postgresql/data
volumes:
sample_db:
You can use ENV variable to change the db address in your docker-compose.
Dockerfile:
FROM java:8
EXPOSE 8080
ENV POSTGRES localhost
ADD /target/manager.jar manager.jar
ENTRYPOINT exec java $JAVA_OPTS -jar manager.jar --spring.datasource.url=jdbc:postgresql://$POSTGRES:5432/sample
docker-compose:
`
container_name: springapp
environment:
- POSTGRES=db`

Spring Boot Application not able to connect with MySQL in Docker

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

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 )