Reputation: 51
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"]
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
Upvotes: 0
Views: 562
Reputation: 208
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.
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
Upvotes: 1