Cannot connect Spring App docker container to MySQL DB docker container - mysql

I am having difficulty setting up two containers. One MySQL instance, and another a Spring Application (built with Maven) which wants to talk to the MySQL instance.
I am not sure if there is something wrong with how the ports are matching up, but I have created a network "shape-shop-network", on which both the application "shapeshop" and the database "shape-shop-db-container" should talk to each other through.
Both my "docker run" specify the correct network.
These are the steps I have taken :
create a network "shape-shop-network" for both containers.
docker network create shape-shop-network
Run MySQL container setting port to 3306, making sure to specify the "shape-shop-network"
docker run -d -p 3306:3306 --name=shape-shop-db-container --network shape-shop-network --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=shapeshop" mysql
...followed by adding some schema and initial data :
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < SCHEMA_AND_INIT_DATA.sql
Specify my application properties in my Spring application
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shapeshop?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.platform=mysql
spring.datasource.initialization-mode=always
Do a Maven Package (to create new jar files which are put in target)
Do a Docker Build. My Docker file looks like this :
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
The command I run is
docker build -t shapeshop:1.0 .
Then I run my image like so (on port 8080):
docker run -p 8080:8080 shapeshop:1.0 --network shape-shop-network
The exception i get is :
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_212]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
... 103 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
Strangely I can run my application through my IDE (IntelliJ) where it connects correctly to the container.
My IDE settings look like this :

Related

How can I identify the issue preventing a connection between my web application Docker container and the associated MySQL Docker container? [duplicate]

This question already has answers here:
Spring Boot + docker-compose + MySQL: Connection refused
(3 answers)
Closed 5 months ago.
I am trying to deploy a small personal website (and backing MySQL) to Docker using Docker Compose. While I have managed to get the web application running on Docker, I cannot get it to connect to the database. This project is the first time I have been using Docker, so I believe I cannot spot the issue due to my inexperience. I wonder if anyone more experienced could see what I am doing wrong.
I have been going at it for days now. However, none of the solutions to similar questions has worked for me. So far, I understand that the 'Communications link failure' error I keep getting is very generic. Thus, it is difficult to pinpoint the exact problem.
I have provided the contents of my Dockerfile, docker-compose.yml, the database configurations in my application.properties, and part of the output log. I have also provided a Pastebin URL for the entire log.
Many Thanks.
Complete Log Output: https://pastebin.com/aYjWnck8
Dockerfile
FROM openjdk:17
COPY target/personal-website-0.0.1-SNAPSHOT.jar personal-website-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "personal-website-0.0.1-SNAPSHOT.jar"]
docker-compose.yml
version: "3.8"
services:
mysqldb:
container_name: personal-website-database
image: mysql:8.0.29
restart: always
ports:
- 3307:3306
environment:
MYSQL_DATABASE: skills
MYSQL_USER: chizzy
MYSQL_PASSWORD: AbcXyz
MYSQL_ROOT_PASSWORD: AbcXyz
server:
build: .
container_name: personal-website
restart: always
ports:
- 8081:8080
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysqldb:3306/skills?useSSL=false&allowPublicKeyRetrieval=true
depends_on:
- mysqldb
application.properties (Database Configuration Excerpt)
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/skills
spring.datasource.username=root
spring.datasource.password=AbcXyz
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Log (Partial)
personal-website | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
personal-website |
personal-website | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
personal-website | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
personal-website | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
personal-website | at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
personal-website | at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
personal-website | at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | ... 57 common frames omitted
personal-website | Caused by: java.net.ConnectException: Connection refused
personal-website | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
personal-website | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
personal-website | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
personal-website | at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
personal-website | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
personal-website | at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
personal-website | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | ... 60 common frames omitted
When you start services with docker-compose, they start at the same time. Even if you use depends_on that only insures that the service it depends on receive the start signal, not that it is actually ready to receive connections.
A java application starts quicker than a database. Unfortunately java also tries to connect to the database during startup and doesn't retry if that fails.
You should have a retry mechanism in your application or a waiting loop in your docker-compose to fix this. The waiting loop would delay the starting of the java application until the database is actually ready to receive requests. The retry mechanism in the Java application would try to connect to the database everytime it fails because a connection is not readily available.
I suggest the wait loop and you can find a very good example here: https://docs.docker.com/compose/startup-order/
I could paste that code here but it would be a good practice for you to actually read the article there and implement it. You only need to change the command of your java application and add the wait-for-it functionality (you need the script as well in the docker image ;) )

How do I connect to a mysql db outside the minikube cluster

I have a mysql database server running on my computer (localhost:3306). I also have several pods running in my minikube cluter.
I want to connect to the mysql database using jdbc connection from within the pod. I understand that localhost would not work in this case because it points to the pod's host. I tried using the IP address of my computer to connect to the database but it did not work. My jdbc url looks like jdbc:mysql://<my-ip>:3306/new_db.
The following error is seen when trying to connect to the db.
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:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:739)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:671)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
at org.wso2.diagnose.databaseresponsetiming.DBTester.afterPropertiesSet(DBTester.java:41)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
at org.wso2.diagnose.databaseresponsetiming.DatabaseResponseTimingApplication.main(DatabaseResponseTimingApplication.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467)
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:89)
at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
... 37 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:392)
at java.base/java.net.Socket.connect(Socket.java:608)
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
... 40 common frames omitted
2021-07-09 05:39:04 - Communications link failure
Any idea on how to resolve this issue?
Create a Service with type NodePort for your MySQL database. Then, run the following command to get the URL to access your database from local machine.
minikube service --url <service-name>
Checkout this Minikube official guides for Accessing apps to explore other options.
I think what you need is to create Service with manually mapped Endpoint to your DB IP address and port. It's described here https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors Also look here https://kubernetes.io/docs/concepts/services-networking/service/#externalname

How to connect spring-boot and mysql using docker?

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.
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
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.

Docker Micro service image Cant connect to local MySQL server

This is how I have configured MySQL in application.properties file
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/roaming
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.initialization-mode=always
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
These are my images inside docker. There are three images. When I try to run the roaming image it cannot connect with my local machine MySQL server. Currently it run using Xampp server
D:\Dialog\Applications\Roming\deployment>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
roaming v1 b7cd90208bcb 17 hours ago 695MB
java 8 d23bdf5b1b1b 2 years ago 643MB
I have an micro service image running inside a docker container and when I try following command
docker run -t roaming:v1 it gives the following error
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) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.3.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:353) ~[HikariCP-3.3.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-3.3.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) ~[HikariCP-3.3.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:562) ~[HikariCP-3.3.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.3.1.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.3.1.jar!/:na]
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.2.Final.jar!/:5.4.2.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.2.Final.jar!/:5.4.2.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.4.2.Final.jar!/:5.4.2.Final]
... 43 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 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
and my Docker file as follows
FROM java:8
ADD roaminglistener-0.0.1-SNAPSHOT.jar roaminglistener-0.0.1-SNAPSHOT.jar
WORKDIR /
EXPOSE 8080
ENTRYPOINT ["java","-Dspring.profiles.active=dev","-jar","roaminglistener-0.0.1-SNAPSHOT.jar"]
If you want to connect from a container to host you have to run a container with the --network host options. More details here.
use host.docker.internal instead of localhost in spring.datasource.url
In container:
127.0.0.1 is mean local container. if you want to connect MySQL server in host machine. (outside container but same host with container )
Should configure:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/roaming
change to :
spring.datasource.url=jdbc:mysql://172.17.0.1:3306/roaming
172.17.0.1 is default ip gateway of container network.

Unable to migrate db using dropwizard and mysql docker container

I have a dropwizard application and a docker container running my mysql database.
I want to create a docker container with my dropwizard project and migrate the db using the config.yml file.
I created my own network - mynet.
First, I run the mysql container using this command:
docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wrapper --network mynet mysql:latest
Then, on the host I run mvn clean install to create the .jar and I start a container with the jar file and the config.yml files copied.
I start the dropwizard container:
docker run -it --network mynet -v $(pwd)/config.yml:/ideaDB/config.yml -p 9004:9004 -p 9005:9005 dropwizard-ideadb sh
and run:
java -jar ideadb-SNAPSHOT.jar db migrate config.yml
but I get the following error:
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)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2247)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:739)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:671)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
at io.dropwizard.migrations.AbstractLiquibaseCommand.createDatabase(AbstractLiquibaseCommand.java:94)
at io.dropwizard.migrations.AbstractLiquibaseCommand.openLiquibase(AbstractLiquibaseCommand.java:79)
at io.dropwizard.migrations.AbstractLiquibaseCommand.run(AbstractLiquibaseCommand.java:67)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
at io.dropwizard.cli.Cli.run(Cli.java:78)
at io.dropwizard.Application.run(Application.java:93)
at org.tmms.ideadb.IdeadbserviceApplication.main(IdeadbserviceApplication.java:80)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
... 28 more
ERROR [2019-03-26 10:57:50,654] org.apache.tomcat.jdbc.pool.ConnectionPool: Unable to create initial connections of pool.
! java.net.ConnectException: Connection refused (Connection refused)
! at java.net.PlainSocketImpl.socketConnect(Native Method)
! at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
! at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
! at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
! at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
! at java.net.Socket.connect(Socket.java:589)
! at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214)
! at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
! ... 28 common frames omitted
! Causing: 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)
! at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
! at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
! at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
! at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
! at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
! at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
! at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2247)
! at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280)
! at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079)
! at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
! at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
! at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
! at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
! at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
! at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
! at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
! at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
! at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:739)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:671)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
! at io.dropwizard.migrations.AbstractLiquibaseCommand.createDatabase(AbstractLiquibaseCommand.java:94)
! at io.dropwizard.migrations.AbstractLiquibaseCommand.openLiquibase(AbstractLiquibaseCommand.java:79)
! at io.dropwizard.migrations.AbstractLiquibaseCommand.run(AbstractLiquibaseCommand.java:67)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
! at io.dropwizard.cli.Cli.run(Cli.java:78)
! at io.dropwizard.Application.run(Application.java:93)
! at org.tmms.ideadb.IdeadbserviceApplication.main(IdeadbserviceApplication.java:80)
This is my config.yml file:
defaultName: Stranger
defaultEmptyResponse: No Data Received
server:
applicationConnectors:
- type: http
port: 9004
adminConnectors:
- type: http
port: 9005
# Database settings.
database:
# the name of the JDBC driver
driverClass: com.mysql.jdbc.Driver
# the username
user: root
# the password
password: root
# the JDBC URL; the database is called tmms
url: jdbc:mysql://mysql:3306/wrapper
swagger:
resourcePackage: ideadb
In the url, mysql is the name of my running mysql container.
What should be the correct url in the config.yml file to connect to mysql?