3

I am trying to connect to mysql server running on host machine (localhost) from a spring app running inside a docker container

this is my Dockerfile

FROM openjdk:11
ADD build/libs/demo-0.0.1-SNAPSHOT.jar demo.jar
EXPOSE 8000
RUN mkdir -p tmp/scripts
RUN mkdir -p tmp/logs
ENTRYPOINT ["java","-jar","/demo.jar"]

I am using Docker desktop for mac v2.3.0.3 with engine version 19.03.8

This is may hikariCP datasource connection properties

spring.datasource.jdbcUrl=jdbc:mysql://**host.docker.internal:3306**/freshid
spring.datasource.username=sa
spring.datasource.password=test

When I build docker build -f Dockerfile -t demo . and run the image as docker run -p 8000:8000 demo

I get the following error on application start up

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:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554)
    ... 64 common frames omitted
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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    ... 72 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 75 common frames omitted
7
  • jdbc:mysql://host.docker.internal:3306/db - this is my connection string Commented Jun 2, 2020 at 14:14
  • Note that the special hostname host.docker.internal has long been a feature that only worked on the macOS version of Docker. Make sure that you are not using an old version of Docker, and that this feature is actually supported on the operating system you are using. Windows: this page says: "This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows." Commented Jun 2, 2020 at 14:19
  • As mentioned, am using Docker desktop for MAC v2.3.0.3 with engine version 19.03.8 Commented Jun 2, 2020 at 14:22
  • Did you also configure MySQL to allow remote connections? Because by default it only allows connections from localhost (and your Docker container looks like an external system to MySQL). Commented Jun 2, 2020 at 14:25
  • Please check docs.docker.com/docker-for-mac/networking/… Commented Jun 2, 2020 at 14:31

2 Answers 2

2

I uninstalled and re-installed the docker (19.03.8) and it worked. The final connection string is jdbc:mysql://host.docker.internal:3306/db. This does not conclude any technical correctness but it worked. Thanks.

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest a simple solution.

Step 1: Turboshooting: Try to run springboot appliction in you machine, outside container. If works fine, then there is no issue your application. (Make sure while testing locally you change your DB connection string from host.docker.internal to localhost)

Step 2: Please expose your DB port, that is 3306 by default in case of mysql when you run Docker Container with imperitive command

docker run -p 8000:8000 -p 3306:3306 demo

This should resolve your issue 😎😎

1 Comment

The application isn't listening on port 3306, and you don't need a -p option to make outbound connections. (Indeed, if the host is running a MySQL instance, this can cause the container to fail to start up.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.