0

I tried to make some rest API using spring-boot and MySQL. And I finished it in my eclipse environment. The next step is to create a docker image. However, when I run the MySQL server and my rest API in docker, it gives me a connection refused error.

I found other basic spring-boot and MySQL tutorials. But I can't solve this problem.

This is mysql setting of spring-boot project.

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/users?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = root

#spring.ldap.embedded.base-dn=dc=example,dc=org

#spring.ldap.base=dc=example,dc=org
#spring.ldap.password=admin
#spring.ldap.username=cn=admin,dc=example,dc=org
#spring.ldap.urls=ldap://localhost:389

##Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

#Open session in View
#spring.jpa.open-in-view=true

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate.ddl-auto=update

This is command to run mysql in the docker.

docker urn -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root --name mysql mysql

This is my docker file to make docker image for rest API.

FROM java:8
VOLUME /tmp
EXPOSE 8080
ADD target/userManageWithRest-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

This is error message.

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.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        ... 56 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111]
        at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111]
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
        ... 59 common frames omitted

I already tried to modify spring.datasource.url to jdbc:mysql://mysql:3306/users?.... How can I solve this problem? Please help me.

Rect
  • 141
  • 5
  • 20

2 Answers2

0

As your MySQL database and Spring Boot app are running in a separate Docker container, access localhost or 127.0.0.1 within a Docker container isn't referring to the localhost of your host machine.

I would suggest to use docker-compose to link the Spring Boot application to your MySQL container and make it accessible via mysql as hostname as you have already tried it (jdbc:mysql://mysql:3306/users?...):

version: "3"  
services:
  backend:
    build: .
    depends_on:
      - "mysql"
    links:
      - mysql
    ports:
      - "8080:8080"
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root

Save this in the folder where your Spring Boot Dockerfile is located as docker-compose.yml and the run docker-compose build && docker-compose run.

With the links attribute to give access to your MySQL container via the container name and as they are sharing the same Docker network, the mysql hostname is resolved correctly

rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • It occurs same error.. (`Caused by: java.net.ConnectException: Connection refused (Connection refused)`, `Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure backend_1 | backend_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.`) – Rect Jul 16 '19 at 06:00
  • You need to change your datasource url to SPRING_DATASOURCE_URL = jdbc:mysql://mysql:3306/users?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=trueuseLegacyDatetimeCode=false&serverTimezone=UTC https://github.com/spring-guides/gs-spring-boot-docker/issues/37 – Aritz Jul 16 '19 at 06:06
0

Even though this question is a bit old, I believe this explanation might be useful.

The reason for your issue is that you can control the order of service startup and shutdown with the depends_on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on, links...

However, for startup Compose does not wait until a container is “ready” (whatever that means for your particular application) - only until it’s running.

That may cause some issues, e.g. your spring-boot app starts very fast and tries to connect to MySQL, but even though MySQL container has started - MySQL inside is not ready to accept connections yet.

There are some workarounds for this. If you are interested in them, you might want to check this question.

ikos23
  • 4,879
  • 10
  • 41
  • 60