I am using spring-boot to run my microservice on docker which is using MySQL, but my app is not able to make the connection with MySQL
The exception is :
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
app_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
app_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
app_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
app_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
app_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
app_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
app_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
app_1 | ... 69 more
app_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
app_1 |
app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
app_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
app_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
app_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
app_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
app_1 | ... 77 more
app_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
app_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
app_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
app_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
app_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
app_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
app_1 | at java.net.Socket.connect(Socket.java:589)
app_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
app_1 | ... 80 more
the docker-compose file is :
version: '3'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=inward
# So you don't have to use root, but you can if you like
- MYSQL_USER=root
# You can use whatever password you like
- MYSQL_PASSWORD=unroot
# Password for root access
- MYSQL_ROOT_PASSWORD=unroot
ports:
# <Port exposed> : < MySQL Port running inside container>
- 3307:3306
app:
build:
context: ./
dockerfile: Dockerfile
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
depends_on:
- db
working_dir: /app
command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=dev -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
ports:
- "8080:8080"
volumes:
- "${HOME}/.m2:/root/.m2"
Dockerfiule
FROM maven:3.5-jdk-8-alpine
COPY . /app
and application.properties file is :
jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
dataSource.user=root
dataSource.password=unroot
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
driverClassName=com.mysql.jdbc.Driver
dataSource.driverClassName=com.mysql.jdbc.Driver
I have tried the solution mentioned in the link :
Communications link failure , Spring Boot + MySql +Docker + Hibernate
any help will be higly appriciable.
I could solve this problem with docker-compose file
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=inward
# So you don't have to use root, but you can if you like
- MYSQL_USER=root
# You can use whatever password you like
- MYSQL_PASSWORD=unroot
# Password for root access
- MYSQL_ROOT_PASSWORD=unroot
ports:
# <Port exposed> : < MySQL Port running inside container>
- 3307:3306
app:
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
working_dir: /app
command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=local -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
ports:
- 8080:8080
volumes:
- "${HOME}/.m2:/root/.m2"
and apllication-local.properties file
jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
dataSource.user=root
dataSource.password=unroot
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
driverClassName=com.mysql.jdbc.Driver
dataSource.driverClassName=com.mysql.jdbc.Driver
and docker file
FROM maven:3.5-jdk-8-alpine
COPY . /app
And please make sure every time you change anything in code, remove the old docker image, and create a new one(This is also I was missing in my approach).
Related
When I trying to run my spring-boot application on docker , it is not able to connect to the Mongodb container running in docker.
Application.properties
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
Dockerfile for spring application
FROM openjdk
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yaml
version: '3'
services:
mongo:
image: mongo:latest
container_name: docker-mongo
ports:
- "27017:27017"
wacaj_docker:
image: wacaj
depends_on:
- mongo
container_name: wacaj
build: .
ports:
- "8080:8080"
I am able to connect to the mongodb container from my local system , but the spring-application container is not able to connect to mongo container.
And also if I run the spring application in local it is able to connect to the mongo runneing in docker container.
I am unable to troubleshoot the problem.
I tried changing hostname to both servicename i.e. mongo
and container name i.e. docker-mongo, but it is still not working.
2023-01-07 08:50:03.146 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
wacaj |
wacaj | com.mongodb.MongoSocketOpenException: Exception opening socket
wacaj | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:157) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
wacaj | Caused by: java.net.ConnectException: Connection refused
wacaj | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
wacaj | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
wacaj | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:539) ~[na:na]
wacaj | at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:594) ~[na:na]
wacaj | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
wacaj | at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
wacaj | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.6.1.jar!/:na]
You need to change the hostname:
spring.data.mongodb.host=mongo
Docker makes every service in a docker compose setup available under its service name, in your case mongo and wacaj_docker.
Please refer to the docs for further details.
I am using docker compose to start a mysql da and a springboot app.
This is my docker-compose.yml
version: '3'
services:
sql:
image: mysql
environment:
MYSQL_DATABASE: people
MYSQL_USER: root
MYSQL_PASSWORD: my-secret-pw
MYSQL_ROOT_PASSWORD: my-secret-pw
ports:
- "3307:3306"
appointment-service:
depends_on:
- sql # This service depends on mysql. Start that first.
environment: # Pass environment variables to the service
- DATABASE_HOST=sql
- DATABASE_USER=root
- DATABASE_PASSWORD=my-secret-pw
- DATABASE_NAME=people
- DATABASE_PORT=3306
links:
- "sql:localhost"
build: .
ports:
- "8081:8081"
This is my application.properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/people?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=my-secret-pw
url.organizations = https://hr.apografi.gov.gr/api/public/organizations
url.db = jdbc:mysql://localhost:3306/people?serverTimezone=UTC
url.username = root
url.password = my-secret-pw
and here I create the jdbi:
private Jdbi jdbi = Jdbi.create("jdbc:mysql://localhost:3306/people?useSSL=false&allowPublicKeyRetrieval=true", "root", "my-secret-pw");
no matter what i tried i could not make the containers talk to each-other inside the docker.
If I start the sql alone, I can see the connection in my host machine (in datagrip).
If I run the java program from container, it sees the database in my host machine.
Even if I run my docker-compose file, both the containers are created and I can access them from my host machine. I know that inside the DockerVM the database exists with the name sql/ I have tried both with sql name and the localhost name (from the links property). But no matter what I tried I was not able to access it from my java program inside the Docker.
Here is the stackTrace
docker-compose up --build
Building appointment-service
Step 1/4 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/4 : ARG JAR_FILE=target/*.jar
---> Using cache
---> 603b2c59103f
Step 3/4 : COPY ${JAR_FILE} app.jar
---> Using cache
---> 4783d891166c
Step 4/4 : ENTRYPOINT ["java","-jar","/app.jar"]
---> Using cache
---> 24deebb3147a
Successfully built 24deebb3147a
Successfully tagged appointments-service_appointment-service:latest
Starting appointments-service_sql_1 ... done
Starting appointments-service_appointment-service_1 ... done
Attaching to appointments-service_sql_1, appointments-service_appointment-service_1
sql_1 | 2020-09-11 16:47:27+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
sql_1 | 2020-09-11 16:47:27+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
sql_1 | 2020-09-11 16:47:27+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
sql_1 | 2020-09-11T16:47:28.258555Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 1
sql_1 | 2020-09-11T16:47:28.275621Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
sql_1 | 2020-09-11T16:47:28.641173Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
sql_1 | 2020-09-11T16:47:28.823110Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
sql_1 | 2020-09-11T16:47:28.988711Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
sql_1 | 2020-09-11T16:47:28.989000Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
sql_1 | 2020-09-11T16:47:28.999907Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
sql_1 | 2020-09-11T16:47:29.056993Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
appointment-service_1 |
appointment-service_1 | . ____ _ __ _ _
appointment-service_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
appointment-service_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
appointment-service_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
appointment-service_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
appointment-service_1 | =========|_|==============|___/=/_/_/_/
appointment-service_1 | :: Spring Boot :: (v2.3.2.RELEASE)
appointment-service_1 |
appointment-service_1 | 2020-09-11 16:47:30.628 INFO 1 --- [ main] c.a.service.ServiceApplication : Starting ServiceApplication v0.0.1-SNAPSHOT on 5bbe74d3ddb0 with PID 1 (/app.jar started by root in /)
appointment-service_1 | 2020-09-11 16:47:30.635 INFO 1 --- [ main] c.a.service.ServiceApplication : No active profile set, falling back to default profiles: default
appointment-service_1 | 2020-09-11 16:47:32.822 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
appointment-service_1 | 2020-09-11 16:47:32.876 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33ms. Found 0 JPA repository interfaces.
appointment-service_1 | 2020-09-11 16:47:34.254 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
appointment-service_1 | 2020-09-11 16:47:34.300 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
appointment-service_1 | 2020-09-11 16:47:34.302 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
appointment-service_1 | 2020-09-11 16:47:34.472 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
appointment-service_1 | 2020-09-11 16:47:34.473 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3697 ms
appointment-service_1 | 2020-09-11 16:47:35.055 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
appointment-service_1 | 2020-09-11 16:47:35.100 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
appointment-service_1 | 2020-09-11 16:47:36.338 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
appointment-service_1 |
appointment-service_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
appointment-service_1 |
appointment-service_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
appointment-service_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar!/:na]
appointment-service_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar!/:na]
appointment-service_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar!/:na]
appointment-service_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) [HikariCP-3.4.5.jar!/:na]
appointment-service_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) [HikariCP-3.4.5.jar!/:na]
appointment-service_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.5.jar!/:na]
appointment-service_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.5.jar!/:na]
appointment-service_1 | at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:324) [spring-jdbc-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:120) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(HibernateDefaultDdlAutoProvider.java:42) [spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.lambda$getVendorProperties$1(HibernateJpaConfiguration.java:130) [spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings.getDdlAuto(HibernateSettings.java:41) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineDdlAuto(HibernateProperties.java:136) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.getAdditionalProperties(HibernateProperties.java:102) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineHibernateProperties(HibernateProperties.java:94) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getVendorProperties(HibernateJpaConfiguration.java:132) [spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:133) ~[spring-boot-autoconfigure-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
appointment-service_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
appointment-service_1 | at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
appointment-service_1 | at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
appointment-service_1 | at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
appointment-service_1 | at com.appointments.service.ServiceApplication.main(ServiceApplication.java:10) ~[classes!/:0.0.1-SNAPSHOT]
appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
appointment-service_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
appointment-service_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
appointment-service_1 | at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
appointment-service_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[app.jar:0.0.1-SNAPSHOT]
appointment-service_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:109) ~[app.jar:0.0.1-SNAPSHOT]
appointment-service_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[app.jar:0.0.1-SNAPSHOT]
appointment-service_1 | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[app.jar:0.0.1-SNAPSHOT]
appointment-service_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
appointment-service_1 |
appointment-service_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
appointment-service_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
appointment-service_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
appointment-service_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
appointment-service_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | ... 57 common frames omitted
appointment-service_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
appointment-service_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
appointment-service_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
appointment-service_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
appointment-service_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
appointment-service_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
appointment-service_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
appointment-service_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
appointment-service_1 | ... 60 common frames omitted
appointment-service_1
|
links: is deprecated and useless in this case - just delete that line.
localhost used inside the java container will point to the container itself. You must use the service name that you gave for your sql container(sql in your case). The service name will be resolved to the IP of the SQL container.
depends_on: will make the java containers wait for the SQL containers to start, but this does not guarantee that the SQL is up and running, ready to accept connections. The are several ways to solve this problem(wait-for-it, restart: on-failure, this post etc.)
you don't need to specify the MYSQL_USER: root nor the MYSQL_PASSWORD: my-secret-pw if your intent is to use just the root user.(this has nothing to do with your current problem)
version: '3'
services:
sql:
image: mysql
environment:
MYSQL_DATABASE: people
MYSQL_ROOT_PASSWORD: my-secret-pw
ports:
- "3307:3306"
appointment-service:
depends_on:
- sql # This service depends on mysql. Start that first.
restart: on-failure
environment: # Pass environment variables to the service
- DATABASE_HOST=sql
- DATABASE_USER=root
- DATABASE_PASSWORD=my-secret-pw
- DATABASE_NAME=people
- DATABASE_PORT=3306
build: .
ports:
- "8081:8081"
server.port=8081
spring.datasource.url=jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=my-secret-pw
url.organizations = https://hr.apografi.gov.gr/api/public/organizations
url.db = jdbc:mysql://sql:3306/people?serverTimezone=UTC
url.username = root
url.password = my-secret-pw
private Jdbi jdbi = Jdbi.create("jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true", "root", "my-secret-pw");
localhost always points to the loopback interface (either 127.0.0.1 or ::1). You cannot, and should not attempt to, override this.
When using docker-compose, you can user service names as hostnames, so the proper JDBC URL for your database would be:
jdbc:mysql://sql:3306/people?useSSL=false&allowPublicKeyRetrieval=true
I'm sitting now and looking for solution over 6 hours. Please help me. First of all I will try explain my problem. I created MySql database using image (in dcoker), but when I want create another container (Sprig Boot) and connect to the Database I cannot. I tried created only image of MySql and conntect from intellij and everything is correct. Only when I try creat project in container is problem...
My docer-compose
version: '3'
services:
api:
build: ./springboot-api-docker
ports:
- "9090:9090"
depends_on:
- database
database:
container_name: 'localhost'
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: password
MYSQL_USER: user
MYSQL_DATABASE: animaldb
ports:
- "3307:3306"
restart: always
application properties
server.port = 9090
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3307/animaldb?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=password
and DocerFile
FROM openjdk:8u201-jdk-alpine3.9
ADD target/springboot-api-docker-0.0.1-SNAPSHOT.jar .
EXPOSE 9090
CMD java -jar springboot-api-docker-0.0.1-SNAPSHOT.jar
It's funny because... first I wantet change name of container "localhost" to another. But when I was changing that I couldn't run Spring Bot Application even from intellij. I created image of MySql in docker and I was trying to conntect in intellij. It was warking but when I want use SpringBoot Application in caontainer then is problem. I didn't find solve for me yet. I will be very thankful for each help.
My errors here.
java.sql.SQLNonTransientConnectionException: Could not create
connection to database server. Attempted reconnect 3 times. Giving up.
And all text from console. Maybe you will find it faster.
Caused by: org.hibernate.service.spi.ServiceException: Unable to
create requested service
[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] api_1 | at
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
Caused by: com.mysql.cj.exceptions.CJCommunicationsException:
Communications link failure api_1 | api_1 | The last
packet sent successfully to the server was 0 milliseconds ago. The
driver has not received any packets from the server. api_1 |
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method) ~[na:1.8.0_201] api_1 | at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
~[na:1.8.0_201] api_1 | at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
~[na:1.8.0_201] api_1 | at
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
~[na:1.8.0_201] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.NativeSession.connect(NativeSession.java:144)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:850)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | ... 58
common frames omitted api_1 | Caused by:
java.net.ConnectException: Connection refused (Connection refused)
api_1 | at java.net.PlainSocketImpl.socketConnect(Native
Method) ~[na:1.8.0_201] api_1 | at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
~[na:1.8.0_201] api_1 | at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
~[na:1.8.0_201] api_1 | at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
~[na:1.8.0_201] api_1 | at
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
~[na:1.8.0_201] api_1 | at
java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_201] api_1 |
at
com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | ... 60
common frames omitted
It's not good to use localhost as you docker container name.
Change localhost to database like it.
localhost is always your local machine or a local docker, not external resource.
docker-compose
version: '3'
services:
api:
build: ./springboot-api-docker
ports:
- "9090:9090"
depends_on:
- database
links:
- database
database:
container_name: 'database'
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: password
MYSQL_USER: user
MYSQL_DATABASE: animaldb
ports:
- "3307:3306"
restart: always
application.properties:
server.port = 9090
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://database: 3306/animaldb?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=password
I know it's late after 2 years, but you should create network first and add two containers to the network.
docker network create network name
docker run yourcontainter --net network name
I have spring boot application which connects to mysql database. If I run it locally in a classic way it works fine. But when I try do dockerise it - database starts successfully but the application doesn't see the DB.
My Dockerfile:
FROM openjdk:11-jdk-slim
ADD start-local.sh /start-local.sh
RUN chmod +x start-local.sh
ENTRYPOINT [ "sh", "-c", "./start-local.sh" ]
My start-local.sh (I added it in order my application waited for database start):
#!/bin/sh
echo "Waiting for the database server to start"
echo "********************************************************"
apt-get update && apt-get install -y netcat
while ! nc -z mysql 3306; do sleep 3; done
echo "******** Database Server has started"
echo "Starting account service"
java -jar /mnt/service-2.4.0.jar
And finally my docker-compose.yml file:
version: '2'
services:
mysql:
image: mysql:5.7.23
environment:
- MYSQL_ROOT_PASSWORD=root
entrypoint:
sh -c "echo 'CREATE DATABASE IF NOT EXISTS books; CREATE DATABASE IF NOT EXISTS notifications' > /docker-entrypoint-initdb.d/init.sql;/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --sql_mode="""
ports:
- "3306:3306"
account_app:
image: service_img
environment:
- "SPRING_PROFILES_ACTIVE=local"
ports:
- "8083:8083"
volumes:
- "/build/libs/:/mnt"
What I execute:
first of all, I build my service_img
then I execute docker-compose up.
In logs, I see database starting logs, after my application starting logs and then error:
| 2019-09-23 09:51:14.006 DEBUG [account-service,,,] 286 --- [
main] c.m.l.c.c.d.p.DatasourceConfiguration : Creating datasource
account_app_1 | 2019-09-23 09:51:14.014 INFO [account-service,,,]
286 --- [ main] com.zaxxer.hikari.HikariDataSource :
HikariPool-1 - Starting... account_app_1 | 2019-09-23 09:51:15.222
ERROR [account-service,,,] 286 --- [ main]
com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception
during pool initialization. account_app_1 | account_app_1 |
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications
link failure account_app_1 | account_app_1 | The last packet sent
successfully to the server was 0 milliseconds ago. The driver has not
received any packets from the server. account_app_1 | at
com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] The last packet sent
successfully to the server was 0 milliseconds ago. The driver has not
received any packets from the server. account_app_1 | at
com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
~[HikariCP-3.2.0.jar!/:na] account_app_1 | at
com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
~[HikariCP-3.2.0.jar!/:na] account_app_1 | at
com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
~[HikariCP-3.2.0.jar!/:na] account_app_1 | at
com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
~[HikariCP-3.2.0.jar!/:na] account_app_1 | at
com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
~[HikariCP-3.2.0.jar!/:na] account_app_1 | ... 206 common
frames omitted account_app_1 | Caused by:
com.mysql.cj.exceptions.CJCommunicationsException: Communications link
failure account_app_1 | account_app_1 | The last packet sent
successfully to the server was 0 milliseconds ago. The driver has not
received any packets from the server. account_app_1 | at
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method) ~[na:na] account_app_1 | at
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
~[na:na] account_app_1 | at
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
~[na:na] account_app_1 | at
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
~[na:na] account_app_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.NativeSession.connect(NativeSession.java:152)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 |
... 214 common frames omitted account_app_1 | Caused by:
java.net.ConnectException: Connection refused (Connection refused)
account_app_1 | at
java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
~[na:na] account_app_1 | at
java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
~[na:na] account_app_1 | at
java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
~[na:na] account_app_1 | at
java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
~[na:na] account_app_1 | at
java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
~[na:na] account_app_1 | at
java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
account_app_1 | at
com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 |
... 217 common frames omitted account_app_1 |
Any suggestions?
When running a compose "stack", docker uses its own DNS mechanism.
This means that if you perform a DNS query inside of the account_app container for the hostname mysql, the query would return the IP address of the container named mysql (possibly prefixed with the stack name)
In short: Configure your Spring application to connect to a mysql db not on localhost but on mysql and you are good to go.
There probably are some environmental variables which you could set to configure this.
I think you use "localhost" as mysql server hostname,
jdbc:mysql://localhost:3306
try changing it to
jdbc:mysql://host.docker.internal:3306
You cannot localhost inside a docker container.
Best way to do this is by passing jdbc url as a environment variable.
SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/code_master
Please check the last 5 lines of this sh file, https://github.com/keaz/code_master/blob/develop/build_docker.sh
There is a misconception on port mapping: you're passing all your traffic from host port 3306 to container port 3306, but that's not bidirectional.
To manage your situation, if you're on Linux (it does not works in Windows and Mac) you can add in your docker-compose.yml this configuration, allowing the container to use host services.
network_mode: "host"
Otherwise you could setup your local MySql to accept connection from external IPs and point container database to your IP address.
Please note that this solution would work fine only in local development machine, I strongly suggest to avoid this in any production environment.
I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. Initially, I created a docker-compose.yml file as given below:
version: '3'
services:
db:
container_name: sample_db
image: postgres
volumes:
- sample_db:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_DB="employee"
- POSTGRES_USER="user"
- POSTGRES_PASSWORD="password"
- PGDATA=/var/lib/postgresql/data/pgdata
web:
build: .
expose:
- "8080"
depends_on:
- db
volumes:
sample_db: {}
Then, in my Spring Boot app, inside the application.properties file I defined the following properties.
spring.datasource.url=jdbc:postgresql://db:5432/employee
spring.datasource.username=user
spring.datasource.password=password
along with this,I created a Dockerfile in my project directory, which looks like this:
FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/employee-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} employee-demo.jar
ENTRYPOINT ["java","-jar","employee-demo.jar"]
I issued these commands and ended up in the error as given below
mvn clean package
docker-compose up
The problem is the connection string. I believe I have to change it to something else, but I don't know what it should be. I get the following error messages:
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
web_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.Driver.makeConnection(Driver.java:454) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.Driver.connect(Driver.java:256) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1 | at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1 | ... 44 common frames omitted
web_1 | Caused by: java.net.UnknownHostException: sample_db
web_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_181]
web_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
web_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
web_1 | at org.postgresql.core.PGStream.<init>(PGStream.java:70) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | ... 58 common frames omitted
Am new to docker, If any issues please inform
Your service name is db, which is what you'll need to reference as the hostname from your web container. The container name, sample_db, is not resolvable from the web container because you have not specified a network, and thus are using the default bridge network.
According to the docs, the default bridge network doesn't provide name resolution between containers, only IP. Using docker compose, you should be able to resolve using the service name.
https://docs.docker.com/network/bridge/
https://docs.docker.com/compose/networking/
version: '3'
services:
db:
container_name: sample_db
image: postgres
volumes:
- sample_db:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_DB="employee"
- POSTGRES_USER="user"
- POSTGRES_PASSWORD="password"
- PGDATA=/var/lib/postgresql/data/pgdata
networks:
- my_network
web:
build: .
expose:
- "8080"
depends_on:
- db
networks:
- my_network
volumes:
sample_db: {}
networks:
my_network:
driver: bridge
A link should also work instead of defining your own custom bridge network:
web:
build: .
expose:
- "8080"
depends_on:
- db
links:
- "db:sample_db"
It was because of the " " given mistakenly. I changed it to
version: '3'
services:
db:
restart: always
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_DB=employee
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
web:
build: .
ports:
- 8080:8080
Now it is working fine. Thank you for your valuable time.