2
votes

I am new to Docker. Trying to understand the concepts with a small example. Currently, I have SpringBoot REST JPA(8080 port) + MySQL server (3306 port). Working perfectly fine(without docker).

I want to deploy Spring Boot REST JPA in a Docker container and MySQL should remain outside Docker engine, it should remain running in same localhost:3306 port.

As a beginning, created a simple HelloWorld app without JPA, able to containerize. And able to get the output from RESTful URL. Now the tricky part started MySQL server 3306 and added Database credentials to Hello World code, built and run the docker once again. I guess it's unable to find localhost: 3306? Please suggest how to make this work? I don't want to containerize MySQL server.

SpringBoot application.properties file:

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Dokerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} demo-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/demo-0.0.1-SNAPSHOT.jar"]

error:

2018-05-28 08:24:28.897  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-05-28 08:24:28.898  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2962 ms
2018-05-28 08:24:29.250  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-05-28 08:24:29.260  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-28 08:24:29.263  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-28 08:24:29.263  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-28 08:24:29.263  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-28 08:24:29.587  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-05-28 08:24:30.811 ERROR 1 --- [           main] 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) ~[na:1.8.0_151]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_151]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_151]

Thanks.

1

1 Answers

0
votes

A docker ontainer is commonly run in a separate network namespace. That means that e.g. localhost refers to the container for processes running in it.

To resolve the issue you need will need to update the connection string:

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false

So that it yields a valid connection. Additionally there are several networking options that you can use for various scenarios. https://docs.docker.com/network/

If this is only intended for local development the simplest might be to use a host network, but this is probably not the setup you want in a production environment.