0

After search on stackoverflow feels like it is common problem, but I didn't found the decision.

Without docker the program works on localhost. The docker file:

FROM openjdk:8
ADD target/DockerMyBlog.jar DockerMyBlog.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "DockerMyBlog.jar"]

The application.properties (did attempt to replace "localhost" over here for "dockermyblog"):

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/javastudy
jdbc.username=hey
jdbc.password=hey

Creating container by command in terminal:

docker build -t dockermyblog .

Trying to launch it by:

docker run -p 8080:8080 dockermyblog

Receive an error:

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.
        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:829)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:449)
......................

Tried to set (without understanding what's that):

jdbc:mysql://localhost:3306/javastudy?enabledTLSProtocols=TLSv1.2

or

jdbc.url=jdbc:mysql://dockermyblog:3306/javastudy?autoReconnect=true&useSSL=false

Tried to put in terminal:

docker inspect dockermyblog | grep IPAddress

Terminal does not know "grep"

What should I do to launch it locally

  • Is you Mysql DB available at `localhost:3306`? Are you able to connect to it with any DB client? – dekkard May 18 '22 at 08:23
  • yes. If I launch it as usual from IDEA, the program works in browser http://localhost:8080 and receives data from DB – surleversant May 18 '22 at 08:27
  • I think that MySQL listen localhost only and it see docker container dockermyblog has another server. May be do a docker-compose with 2 containers : mysql and dockermyblog – Mr_Thorynque May 18 '22 at 08:31
  • could you give a link with instructions how to do it? I'm very-very newbie in all that. – surleversant May 18 '22 at 08:48
  • 1
    Is your MySQL dockerized? – Nikolai Shevchenko May 18 '22 at 09:31
  • Found some explanations over here https://www.youtube.com/watch?v=ck6xQqSOlpw As it is explained up there, I've created image "mysql" (...feels like it should automatically grab my DB..?). Now I have 2 images: mysql and dockermyblog. Do I correctly understand that I need now create "docker-compose.yml" where it will be joined in one container? That is the way? – surleversant May 18 '22 at 09:54
  • The linked question has some complete examples of running a Spring application and a database in a single Compose file. In Docker, `localhost` will generally refer to your application container and not the database. If you want to run the two containers side-by-side without Compose, also see [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname). – David Maze May 18 '22 at 10:26
  • OK. Thank you. Feels like we can close this question and I need to go and learn some more about "docker-compose.yml" Guy's, thank you to all for directing me!!!!! – surleversant May 18 '22 at 10:55

1 Answers1

1

When you use localhost in the Java application like this, it resolves to the address on the container and no on the host (so your computer). Since there is no MySQL server running in the same container it will fail.

If you change you url to jdbc.url=jdbc:mysql://host.docker.internal:3306/javastudy your container Java application can access the MySQL running on the host. See this post for a thorough explanation.

However, to make it work both running locally, so from Intellij and from the container, you can use different spring-boot profiles. Let's say application-local.proeprties which only contains jdbc.url=jdbc:mysql://localhost:3306/javastudy which will override the url if you use that profile

Hans-Christian
  • 542
  • 4
  • 6