0

I have tried at least 20-25 combination on the Dockerfile, docker-compose.yml and application.yml but still get the same error.

I think the problem may be related to Java version, because I use Java 11 and the proper jdk on Dockerfile, but the error still indicates Java8 (maybe JRE related, but not sure).

Here is the error message when I run docker-compose up command:

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
movies-app |    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
movies-app |    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
movies-app |    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
movies-app |    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
movies-app |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    ... 133 common frames omitted
movies-app | Caused by: java.net.ConnectException: Connection refused (Connection refused)
movies-app |    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
movies-app |    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]
movies-app |    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
movies-app |    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
movies-app |    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
movies-app |    at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
movies-app |    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
movies-app |    ... 136 common frames omitted
movies-app |
movies-app exited with code 1

And here are:

Dockerfile:

#FROM openjdk:8
FROM maven:3.8.1-openjdk-11
FROM openjdk:11

COPY target/*.jar app.jar
CMD mvn clean install -DskipTests

EXPOSE 8088
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=docker", "app.jar"]

docker-compose.yml:

version: '3.8'

services:
  server:
    container_name: movies-app
    build: .
    restart: always
    ports:
      - "8088:8080"
    networks:
      - app-network

  mysqldb:
    container_name: mysqldb
    image: mysql:8.0.28
    restart: always
    ports:
      - "3307:3306"
    environment:
      MYSQL_DATABASE: moviesdb
      MYSQL_USER: user
      MYSQL_PASSWORD: pass
      MYSQL_ROOT_USER: root
      MYSQL_ROOT_PASSWORD: pass
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - app-network
      
networks:
  app-network:
    driver: bridge

volumes:
  dbdata:
    driver: local

application.yml: here is the db connection details part:

  datasource:
    password: pass
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    # url: jdbc:mysql://localhost:3306/movies-db?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false
    url: jdbc:mysql://mysqldb:3307/moviesdb?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false

So, what is the cause of the problem?

Jack
  • 1
  • 21
  • 118
  • 236
  • can you please show `mysql` connection related properties in `application.yml`? – rok Nov 09 '22 at 15:10
  • I also tried with amazoncorretto version and update Dockerfile with ``FROM amazoncorretto:11`, still teh same error. Any idea? – Jack Nov 09 '22 at 15:25

1 Answers1

1

Are you trying to connect to port 3307 of mysqldb?

Ports defined in docker-compose.yml are only mapped outside of your docker bridge network. Try to connect to 3306.

Your url in application.yaml should be mysqldb:3306.

Jesse
  • 11
  • 3
  • Good point, I can connect via Dbeaver on port 3307, but the tables are not creates (I think still a problem between app and database). Any idea? – Jack Nov 09 '22 at 15:23
  • Is your app using port 3306? – Jesse Nov 09 '22 at 15:26
  • App using 3307 and I think it uses application.yml (but maybe if I set port in docker-compose it would also be effective). – Jack Nov 09 '22 at 15:28
  • Change your address in application.yaml to mysqldb:3306 – Jesse Nov 09 '22 at 15:32
  • and also docker-compose file, right? – Jack Nov 09 '22 at 15:37
  • one of the part of error is "createCommunicationsException(SQLError.java:174) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]" – Jack Nov 09 '22 at 15:37
  • I will try with , but the answer of Janne seems to be raleated to the problem on https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach/24326540#24326540 Any idea? I am also trying with ` extra_hosts: - "host.docker.internal:host-gateway"` property now – Jack Nov 09 '22 at 15:43
  • **Finally** the problem was solved anyhow, but probably related to port **3306**. Thanks a lot Jesse, saved my day. – Jack Nov 09 '22 at 15:54
  • Ports in `docker-compose.yaml` doesn't matter, they are only defining which ports are available public. Your java app can access `mysqldb` directly. `127.0.0.1` would only work if you specify `network_mode: host` – Jesse Nov 09 '22 at 15:54
  • Dear Jesse, could you please post a proper Dockerfile and docker-compose file as an answer so that I can follow the useful parts? It would really be useful for me :) – Jack Nov 09 '22 at 15:56
  • And what about the files that I need to provide to the person who will run the application? I think I should provide: **jar** file, Dockerfile and docker-compose file. What about application.yml file? And what else? – Jack Nov 09 '22 at 15:57