Below is my application.properties file
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
Below is my docker-compose.yml file
version: '3'
services:
docker-mysql:
image: mysql:latest
ports:
- 3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test
- MYSQL_PASSWORD=root
spring-boot-jpa-docker-webapp:
image: springboot_docker
depends_on:
- docker-mysql
ports:
- 8080:8080
environment:
- DATABASE_HOST=docker-mysql
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=test
- DATABASE_PORT=3306
Application is exited with
Caused by: java.net.ConnectException: Connection refused (Connection
refused)
Caused by: java.sql.SQLNonTransientConnectionException: Could not
create connection to database server. Attempted reconnect 3 times.
Giving up.
Caused by: java.net.ConnectException: Connection refused (Connection
refused)
git_spring-boot-jpa-docker-webapp_1 exited with code 1
Update 1: New error after correction to docker-compose
docker-mysql_1 | Initializing database
docker-mysql_1 | 2019-02-13T20:34:44.351044Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
docker-mysql_1 | 2019-02-13T20:34:44.351123Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.15) initializing of server in progress as process 29
docker-mysql_1 | 2019-02-13T20:34:44.352587Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
docker-mysql_1 | 2019-02-13T20:34:44.352592Z 0 [ERROR] [MY-013236] [Server] Newly created data directory /var/lib/mysql/ is unusable. You can safely remove it.
docker-mysql_1 | 2019-02-13T20:34:44.352638Z 0 [ERROR] [MY-010119] [Server] Aborting
docker-mysql_1 | 2019-02-13T20:34:44.353948Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.15) MySQL Community Server - GPL.
spring-boot-jpa-docker-webapp_1 |
spring-boot-jpa-docker-webapp_1 | 2019-02-13 21:02:22.562 WARN 1 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 08001
spring-boot-jpa-docker-webapp_1 | 2019-02-13 21:02:22.562 ERROR 1 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Could not create connection to database server. Attempted reconnect 3 times. Giving up.
spring-boot-jpa-docker-webapp_1 | 2019-02-13 21:02:22.569 WARN 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1 | 2019-02-13 21:02:22.573 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
spring-boot-jpa-docker-webapp_1 | 2019-02-13 21:02:22.596 INFO 1 --- [ main] ConditionEvaluationReportLoggingListener :
spring-boot-jpa-docker-webapp_1 |
spring-boot-jpa-docker-webapp_1 | Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
spring-boot-jpa-docker-webapp_1 | 2019-02-13 21:02:22.601 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
spring-boot-jpa-docker-webapp_1 |
spring-boot-jpa-docker-webapp_1 | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1 | at com.github.Application.main(Application.java:15) ~[classes!/:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1 | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1 | Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1 | at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:402) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | ... 22 common frames omitted
spring-boot-jpa-docker-webapp_1 | Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1 | at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:69) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.jdbcStatement(GenerationTargetToDatabase.java:77) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:53) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:241) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:144) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1 | at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1 | ... 26 common frames omitted
spring-boot-jpa-docker-webapp_1 | Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:905) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:830) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_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]
spring-boot-jpa-docker-webapp_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]
spring-boot-jpa-docker-webapp_1 | ... 42 common frames omitted
spring-boot-jpa-docker-webapp_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
spring-boot-jpa-docker-webapp_1 |
spring-boot-jpa-docker-webapp_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
spring-boot-jpa-docker-webapp_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:849) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | ... 56 common frames omitted
spring-boot-jpa-docker-webapp_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
spring-boot-jpa-docker-webapp_1 | at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1 | ... 58 common frames omitted
You are passing the DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER and DATABASE_PASSWORD but you are not using it inside your app.
Update your properties like this. (Better if your create a profile so that you app runs outside of docker)
spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?autoReconnect=true
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}
If you really want to use localhost as your db_url. You can also use network_mode: "service:[service name]" property of docker compose. Only downside is that this property cannot be used with port property.
version: '3'
services:
docker-mysql:
image: mysql:latest
network_mode: "service:spring-boot-jpa-docker-webapp"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test
- MYSQL_PASSWORD=root
spring-boot-jpa-docker-webapp:
image: springboot_docker
depends_on:
- docker-mysql
ports:
- 8080:8080
- 3306 #Add this only if you want to expose the mysql to outer world.
environment:
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=test
And your properties file could look like
spring.datasource.url=jdbc:mysql://localhost:3306/${DATABASE_NAME}?autoReconnect=true
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}
Containers in compose services can connect to other containers by using name, e.g. try to ping database container from spring-boot container.
Check if you can ping database container
docker-compose exec spring-boot-jpa-docker-webapp /bin/bash
once you are in the shell (You may have to install ping in spring-boot container for testing only apt-get install iputils-ping or yum install depending on image container is based on)
ping docker-mysql
if ping is successful then it means you can connect to database
replace database url in application.properties with
spring.datasource.url=jdbc:mysql://docker-mysql:3306/test?autoReconnect=true
Minimal Example for Mysql
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- "./volumes/database:/var/lib/mysql"
Try following docker-compose
version: "3.7"
services:
docker-mysql:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test
volumes:
- "./volumes/database:/var/lib/mysql"
networks:
- spring_net
spring-boot-jpa-docker-webapp:
image: springboot_docker
ports:
- "8080:8080"
networks:
- spring_net
networks:
spring_net:
Related
i have a problem with docker compose when i try to run docker-compose every time i get the following error i tried many solutions but nothing helps. Please help.
I will add that I created the docker image using Cloud Native Buildpacks. I used the command:
mvn spring-boot: build-image -Dspring-boot.build-image.imageName = jakuczczubak/infrastock
and then uploaded the image to dockerhub
dockerhub: dockerhub
github: github
docker-compose.yml
services:
mysql:
image: mysql:5.7
container_name: test-mysql-docker
environment:
- MYSQL_ROOT_PASSWORD=mysqlpw
- MYSQL_USER=infrastock
- MYSQL_DATABASE=infrastock
- MYSQL_PASSWORD=infrastock123
ports:
- 33067:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
depends_on:
- mysql
api:
image: czubakjakub/infrastock:v1.0.0
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://root:mysqlpw#test-mysql-docker:3306/infrastock?useSSL=false
- SPRING_JPA_HIBERNATE_DDL-AUTO=update
ports:
- 9090:9090
depends_on:
- mysql
application.properties
server.port=9090
spring.datasource.url=jdbc:mysql://root:mysqlpw#host.docker.internal:33067/infrastock
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
error:
- Network docker-compose_default Created 2.6s
- Container test-mysql-docker Created 0.6s
- Container docker-compose-api-1 Created 0.6s
- Container docker-compose-adminer-1 Created 0.5s
Attaching to docker-compose-adminer-1, docker-compose-api-1, test-mysql-docker
test-mysql-docker | 2022-06-22 18:55:25+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.38-1debian10 started.
test-mysql-docker | 2022-06-22 18:55:25+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
test-mysql-docker | 2022-06-22 18:55:25+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.38-1debian10 started.
test-mysql-docker | 2022-06-22 18:55:25+00:00 [Note] [Entrypoint]: Initializing database files
test-mysql-docker | 2022-06-22T18:55:25.410856Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
test-mysql-docker | 2022-06-22T18:55:27.139862Z 0 [Warning] InnoDB: New log files created, LSN=45790
test-mysql-docker | 2022-06-22T18:55:27.489120Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
test-mysql-docker | 2022-06-22T18:55:27.590997Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e1816692-f25c-11ec-951c-0242ac130002.
test-mysql-docker | 2022-06-22T18:55:27.604367Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
test-mysql-docker | 2022-06-22T18:55:28.037561Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
test-mysql-docker | 2022-06-22T18:55:28.037615Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
test-mysql-docker | 2022-06-22T18:55:28.038212Z 0 [Warning] CA certificate ca.pem is self signed.
test-mysql-docker | 2022-06-22T18:55:28.080678Z 1 [Warning] root#localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
docker-compose-adminer-1 | [Wed Jun 22 18:55:29 2022] PHP 7.4.30 Development Server (http://[::]:8080) started
docker-compose-api-1 | Setting Active Processor Count to 8
docker-compose-api-1 | Calculating JVM memory based on 7046808K available memory
docker-compose-api-1 | `For more information on this calculation, see https://paketo.io/docs/reference/java-reference/#memory-calculator
docker-compose-api-1 | Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx6417274K -XX:MaxMetaspaceSize=117533K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 7046808K, Thread Count: 250, Loaded Class Count: 18337, Headroom: 0%)
docker-compose-api-1 | Enabling Java Native Memory Tracking
docker-compose-api-1 | Adding 127 container CA certificates to JVM truststore
docker-compose-api-1 | Spring Cloud Bindings Enabled
docker-compose-api-1 | Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -XX:+ExitOnOutOfMemoryError -XX:ActiveProcessorCount=8 -XX:MaxDirectMemorySize=10M -Xmx6417274K -XX:MaxMetaspaceSize=117533K -XX:ReservedCodeCacheSize=240M -Xss1M -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics -Dorg.springframework.cloud.bindings.boot.enable=true
docker-compose-api-1 |
docker-compose-api-1 | . ____ _ __ _ _
docker-compose-api-1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
docker-compose-api-1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
docker-compose-api-1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
docker-compose-api-1 | ' |____| .__|_| |_|_| |_\__, | / / / /
docker-compose-api-1 | =========|_|==============|___/=/_/_/_/
docker-compose-api-1 | :: Spring Boot :: (v2.6.3)
docker-compose-api-1 |
docker-compose-api-1 | 2022-06-22 18:55:31.261 INFO 1 --- [ main] c.e.infrastock.InfraStockApplication : Starting InfraStockApplication v0.0.1-SNAPSHOT using Java 11.0.15.1 on a109f4fe4312 with PID 1 (/workspace/BOOT-INF/classes started by cnb in /workspace)
docker-compose-api-1 | 2022-06-22 18:55:31.270 INFO 1 --- [ main] c.e.infrastock.InfraStockApplication : No active profile set, falling back to default profiles: default
docker-compose-api-1 | 2022-06-22 18:55:32.883 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
docker-compose-api-1 | 2022-06-22 18:55:33.021 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 124 ms. Found 4 JPA repository interfaces.
docker-compose-api-1 | 2022-06-22 18:55:34.554 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9090 (http)
docker-compose-api-1 | 2022-06-22 18:55:34.583 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
docker-compose-api-1 | 2022-06-22 18:55:34.584 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
docker-compose-api-1 | 2022-06-22 18:55:34.777 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
docker-compose-api-1 | 2022-06-22 18:55:34.777 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3377 ms
docker-compose-api-1 | Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
docker-compose-api-1 | 2022-06-22 18:55:35.258 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
docker-compose-api-1 | 2022-06-22 18:55:35.367 INFO 1 --- [ main] org.hibernate.Version
: HHH000412: Hibernate ORM core version 5.6.4.Final
docker-compose-api-1 | 2022-06-22 18:55:35.583 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
docker-compose-api-1 | 2022-06-22 18:55:35.753 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
docker-compose-api-1 | 2022-06-22 18:55:35.772 WARN 1 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
docker-compose-api-1 | 2022-06-22 18:55:36.976 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
docker-compose-api-1 |
docker-compose-api-1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
docker-compose-api-1 |
docker-compose-api-1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
docker-compose-api-1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.28.jar:8.0.28]
docker-compose-api-1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.28.jar:8.0.28]
docker-compose-api-1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:829) ~[mysql-connector-java-8.0.28.jar:8.0.28]
docker-compose-api-1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:449) ~[mysql-connector-java-8.0.28.jar:8.0.28]
docker-compose-api-1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:242) ~[mysql-connector-java-8.0.28.jar:8.0.28]
docker-compose-api-1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.28.jar:8.0.28]
docker-compose-api-1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121) ~[HikariCP-4.0.3.jar:na]
docker-compose-api-1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar:na]
docker-compose-api-1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar:na]
docker-compose-api-1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar:na]
docker-compose-api-1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar:na]
docker-compose-api-1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar:na]
docker-compose-api-1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar:na]
docker-compose-api-1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:181) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:175) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:173) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
docker-compose-api-1 | at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.15.jar:5.3.15]
docker-compose-api-1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]
Full stack trace: https://pastebin.com/mLPfHNbJ
Full docker-compose.yml logslogs
I run sprinboot app, front (in react), database and rabbitmq in docker-compose. When I run it everything works except rabbitmq. I had an error - "Connection refused".
docker-compose:
version: '3'
services:
server:
build: ./mlogger
ports:
- "8000:8080"
depends_on:
- database
restart: always
web:
build: ./web
ports:
- "4000:3000"
tty: true
depends_on:
- server
database:
container_name: 'mysql'
image: mysql
environment:
MYSQL_ROOT_PASSWORD: jitsi1234
MYSQL_USER: jitsiteam
MYSQL_DATABASE: testmlogger
ports:
- "3306:3306"
restart: always
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
command: sh -c "rabbitmq-plugins enable rabbitmq_auth_backend_ldap"
ports:
- "5672:5672"
- "15672:15672"
restart: always
error:
server_1 | 2020-09-05 16:14:42.110 INFO 6 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
server_1 | 2020-09-05 16:14:42.117 ERROR 6 --- [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
server_1 |
server_1 | org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
server_1 | at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:524) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2089) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2062) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2042) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1836) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1817) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1349) [spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1195) [spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]
server_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
server_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_265]
server_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_265]
server_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_265]
server_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_265]
server_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_265]
server_1 | at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_265]
server_1 | at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.9.0.jar!/:5.9.0]
server_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1137) ~[amqp-client-5.9.0.jar!/:5.9.0]
server_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1087) ~[amqp-client-5.9.0.jar!/:5.9.0]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:560) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:533) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:487) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | ... 12 common frames omitted
Spring boot properties:
spring.rabbitmq.address=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
I'm using ldap also, and I want to add users to rabbitmq on start. I can do it by adding commands to docker-compose?
Spring boot ldap config:
#Configuration
#EnableLdapRepositories
public class LdapConfig {
#Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://localhost:18889");
contextSource.setBase("dc=example,dc=com");
return contextSource;
}
#Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
}
I figured it out, in the docker compose part responsible for the spring application, I had to specify the network mode:
server:
build: ./mlogger
ports:
- "8000:8080"
depends_on:
- database
- rabbitmq
network_mode: "host"
Inside a container, localhost is the container itself, not the host it is running on. Components are visible to each other with their service names. Insead of connecting to localhost:5672, connect to rabbitmq:5672, and it should work. Do the same for the database as well.
I am trying to connection MariaDB database using docker-compose, but I get the following error where I run containers using the command 'docker-compose up'
application.properties
spring.datasource.hikari.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.hikari.jdbc-url=jdbc:log4jdbc:mariadb://kipit_db:${DATABASE_PORT}/${DATABASE_NAME}?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
spring.datasource.hikari.username=${DATABASE_USER}
spring.datasource.hikari.password=${DATABASE_PASSWORD}
Dockerfile
FROM openjdk:8-jdk
VOLUME /tmp
EXPOSE 8080
ARG JAR_FILE=build/libs/kipit-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} kipit.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/kipit.jar"]
docker-compose.yml
version: '3'
services:
kipit_db:
image: mariadb:10.4
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test
- MYSQL_PASSWORD=root
kipit:
image: kipit
depends_on:
- kipit_db
ports:
- 8080:8080
- 3308:3306
environment:
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=test
- DATABASE_PORT=3306
Set up a spring boot application that depends on a MariaDB called in docker-compose. After issuing docker-compose up I'm getting:
Error Log
Creating network "kipit_default" with the default driver
Creating kipit_kipit_db_1 ... done
Creating kipit_kipit_1 ... done
Attaching to kipit_kipit_db_1, kipit_kipit_1
kipit_db_1 | 2020-05-29 05:50:39+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.13+maria~bionic started.
kipit_db_1 | 2020-05-29 05:50:40+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
kipit_db_1 | 2020-05-29 05:50:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.13+maria~bionic started.
kipit_db_1 | 2020-05-29 05:50:40+00:00 [Note] [Entrypoint]: Initializing database files
kipit_1 |
kipit_1 | . ____ _ __ _ _
kipit_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
kipit_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
kipit_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
kipit_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
kipit_1 | =========|_|==============|___/=/_/_/_/
kipit_1 | :: Spring Boot :: (v2.2.6.RELEASE)
kipit_1 |
kipit_1 | 2020-05-29 05:50:42 INFO [ main ] c.p.k.KipitApplication : Starting KipitApplication on 0fe8f9293838 with PID 1 (/kipit.jar started by root in /)
kipit_1 | 2020-05-29 05:50:42 INFO [ main ] c.p.k.KipitApplication : The following profiles are active: oauth
kipit_db_1 |
kipit_db_1 |
kipit_db_1 | PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
kipit_db_1 | To do so, start the server, then issue the following commands:
kipit_db_1 |
kipit_db_1 | '/usr/bin/mysqladmin' -u root password 'new-password'
kipit_db_1 | '/usr/bin/mysqladmin' -u root -h password 'new-password'
kipit_db_1 |
kipit_db_1 | Alternatively you can run:
kipit_db_1 | '/usr/bin/mysql_secure_installation'
kipit_db_1 |
kipit_db_1 | which will also give you the option of removing the test
kipit_db_1 | databases and anonymous user created by default. This is
kipit_db_1 | strongly recommended for production servers.
kipit_db_1 |
kipit_db_1 | See the MariaDB Knowledgebase at http://mariadb.com/kb or the
kipit_db_1 | MySQL manual for more instructions.
kipit_db_1 |
kipit_db_1 | Please report any problems at http://mariadb.org/jira
kipit_db_1 |
kipit_db_1 | The latest information about MariaDB is available at http://mariadb.org/.
kipit_db_1 | You can find additional information about the MySQL part at:
kipit_db_1 | http://dev.mysql.com
kipit_db_1 | Consider joining MariaDB's strong and vibrant community:
kipit_db_1 | https://mariadb.org/get-involved/
kipit_db_1 |
kipit_db_1 | 2020-05-29 05:50:43+00:00 [Note] [Entrypoint]: Database files initialized
kipit_db_1 | 2020-05-29 05:50:43+00:00 [Note] [Entrypoint]: Starting temporary server
kipit_db_1 | 2020-05-29 05:50:43+00:00 [Note] [Entrypoint]: Waiting for server startup
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] mysqld (mysqld 10.4.13-MariaDB-1:10.4.13+maria~bionic) starting as process 122 ...
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Using Linux native AIO
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Uses event mutexes
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Number of pools: 1
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Using SSE2 crc32 instructions
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Completed initialization of buffer pool
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Creating shared tablespace for temporary tables
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Waiting for purge to start
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: 10.4.13 started; log sequence number 60972; transaction id 21
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] Plugin 'FEEDBACK' is disabled.
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
kipit_db_1 | 2020-05-29 5:50:44 0 [Warning] 'user' entry 'root#3787a6d6386a' ignored in --skip-name-resolve mode.
kipit_db_1 | 2020-05-29 5:50:44 0 [Warning] 'user' entry '#3787a6d6386a' ignored in --skip-name-resolve mode.
kipit_db_1 | 2020-05-29 5:50:44 0 [Warning] 'proxies_priv' entry '#% root#3787a6d6386a' ignored in --skip-name-resolve mode.
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] InnoDB: Buffer pool(s) load completed at 200529 5:50:44
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] Reading of all Master_info entries succeeded
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] Added new Master_info '' to hash table
kipit_db_1 | 2020-05-29 5:50:44 0 [Note] mysqld: ready for connections.
kipit_db_1 | Version: '10.4.13-MariaDB-1:10.4.13+maria~bionic' socket: '/var/run/mysqld/mysqld.sock' port: 0 mariadb.org binary distribution
kipit_db_1 | 2020-05-29 05:50:45+00:00 [Note] [Entrypoint]: Temporary server started.
kipit_1 | 2020-05-29 05:50:45 INFO [ main ] o.s.b.w.e.t.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
kipit_1 | 2020-05-29 05:50:45 INFO [ main ] o.a.c.h.Http11NioProtocol : Initializing ProtocolHandler ["http-nio-8080"]
kipit_1 | 2020-05-29 05:50:45 INFO [ main ] o.a.c.c.StandardService : Starting service [Tomcat]
kipit_1 | 2020-05-29 05:50:45 INFO [ main ] o.a.c.c.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
kipit_1 | 2020-05-29 05:50:45 INFO [ main ] o.a.c.c.C.[.[.[/] : Initializing Spring embedded WebApplicationContext
kipit_1 | 2020-05-29 05:50:45 INFO [ main ] o.s.w.c.ContextLoader : Root WebApplicationContext: initialization completed in 2707 ms
kipit_1 | 2020-05-29 05:50:45 INFO [ main ] c.z.h.HikariDataSource : HikariPool-1 - Starting...
kipit_1 | 2020-05-29 05:50:47 ERROR[ main ] c.z.h.p.HikariPool : HikariPool-1 - Exception during pool initialization.
kipit_1 | java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=kipit_db)(port=3306)(type=master) : Socket fail to connect to host:kipit_db, port:3306. Conne
ction refused (Connection refused)
kipit_1 | at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:240)
kipit_1 | at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1192)
kipit_1 | at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:620)
kipit_1 | at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:175)
kipit_1 | at org.mariadb.jdbc.Driver.connect(Driver.java:92)
kipit_1 | at net.sf.log4jdbc.sql.jdbcapi.DriverSpy.connect(DriverSpy.java:401)
kipit_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
kipit_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354)
kipit_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202)
kipit_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473)
kipit_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554)
kipit_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
kipit_1 | at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
kipit_1 | at com.project.kipit.config.DatabaseConfig.dataSource(DatabaseConfig.java:35)
kipit_1 | at com.project.kipit.config.DatabaseConfig$$EnhancerBySpringCGLIB$$37c12367.CGLIB$dataSource$1(<generated>)
kipit_1 | at com.project.kipit.config.DatabaseConfig$$EnhancerBySpringCGLIB$$37c12367$$FastClassBySpringCGLIB$$3aa2a57.invoke(<generated>)
kipit_1 | at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
kipit_1 | at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
kipit_1 | at com.project.kipit.config.DatabaseConfig$$EnhancerBySpringCGLIB$$37c12367.dataSource(<generated>)
kipit_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
kipit_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
kipit_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
kipit_1 | at java.lang.reflect.Method.invoke(Method.java:498)
kipit_1 | at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
kipit_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
kipit_1 | at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
kipit_1 | at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290)
kipit_1 | at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
kipit_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
kipit_1 | at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
kipit_1 | at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290)
kipit_1 | at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1511)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1406)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
kipit_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
kipit_1 | at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
kipit_1 | at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290)
kipit_1 | at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
kipit_1 | at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
kipit_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
kipit_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
kipit_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
kipit_1 | at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882)
kipit_1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
kipit_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
kipit_1 | at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
kipit_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
kipit_1 | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
kipit_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
kipit_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
kipit_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
kipit_1 | at com.project.kipit.KipitApplication.main(KipitApplication.java:20)
kipit_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
kipit_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
kipit_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
kipit_1 | at java.lang.reflect.Method.invoke(Method.java:498)
kipit_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
kipit_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
kipit_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
kipit_1 | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
kipit_1 | Caused by: java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:kipit_db, port:3306. Connection refused (Connection refused)
kipit_1 | at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:240)
kipit_1 | at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.connException(ExceptionMapper.java:101)
kipit_1 | at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createSocket(AbstractConnectProtocol.java:468)
kipit_1 | at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createConnection(AbstractConnectProtocol.java:358)
kipit_1 | at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1187)
kipit_1 | ... 89 common frames omitted
kipit_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
kipit_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
kipit_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
kipit_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
kipit_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
kipit_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
kipit_1 | at java.net.Socket.connect(Socket.java:607)
kipit_1 | at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createSocket(AbstractConnectProtocol.java:463)
What i'am missing now? Thank you
I'm just trying to setup a starter project based on Spring Boot and MySQL using Docker. So I tried to run this github project on the Docker. But I am unable to establish connection between Java and MySQL.
I'm running Ubuntu 16.04.
I'm getting "Failed to create/setup connection: Communications link failure...." error while executing "docker-compose up" command.
Also this error doesn't show up if I use Docker Client. Like when I do as:
# 1. docker run -d -p 6033:3306 --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=book_manager" mysql
# 2. docker build -t springio/gs-spring-boot-docker .
# 3. docker run -t --link docker-mysql:mysql -p 10222:10222 springio/gs-spring-boot-docker
I tried to setup same environment through docker-compose but somehow its not working.
Below are the configuration details:
application.properties
logging.level.org.springframework=INFO
logging.level.com.mkyong=INFO
logging.level.com.zaxxer=DEBUG
logging.level.root=ERROR
logging.pattern.console=%-5level %logger{36} - %msg%n
## MySQL
spring.datasource.url=jdbc:mysql://docker-mysql:3306/book_manager
spring.datasource.username=root
spring.datasource.password=root
server.port=10222
connection.driver.class = com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#`hibernate_sequence' doesn't exist
spring.jpa.hibernate.use-new-id-generator-mappings=false
hibernate.show_sql=false
#drop n create table again, good for testing, comment this in production
spring.jpa.hibernate.ddl-auto=create
Dockerfile for the java application container (service)
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} spring-data-jpa-mysql-1.0.jar
ENTRYPOINT ["java","-jar","/spring-data-jpa-mysql-1.0.jar"]
docker-compose.yml
version: '3'
services:
docker-mysql:
restart: always
container_name: docker-mysql
image: mysql
environment:
MYSQL_DATABASE: book_manager
MYSQL_ROOT_PASSWORD: root
ports:
- "6033:3306"
networks:
- spring-boot-mysql-network
book-manager-app:
build:
context: ./
dockerfile: Dockerfile
expose:
- "10222"
ports:
- "10222:10222"
depends_on:
- "docker-mysql"
networks:
- spring-boot-mysql-network
networks:
spring-boot-mysql-network:
driver: bridge
The following is the error log that I'm getting:
$/spring-data-jpa-mysql-compose$ docker-compose up
Creating network "spring-data-jpa-mysql-compose_spring-boot-mysql-network" with driver "bridge"
Creating docker-mysql ... done
Creating spring-data-jpa-mysql-compose_book-manager-app_1 ... done
Attaching to docker-mysql, spring-data-jpa-mysql-compose_book-manager-app_1
docker-mysql | 2020-04-07 13:44:39+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
docker-mysql | 2020-04-07 13:44:39+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
docker-mysql | 2020-04-07 13:44:39+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
docker-mysql | 2020-04-07 13:44:39+00:00 [Note] [Entrypoint]: Initializing database files
docker-mysql | 2020-04-07T13:44:39.855069Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
docker-mysql | 2020-04-07T13:44:39.855190Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 42
book-manager-app_1 |
book-manager-app_1 | . ____ _ __ _ _
book-manager-app_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
book-manager-app_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
book-manager-app_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
book-manager-app_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
book-manager-app_1 | =========|_|==============|___/=/_/_/_/
book-manager-app_1 | :: Spring Boot :: (v2.1.2.RELEASE)
book-manager-app_1 |
book-manager-app_1 | INFO com.mkyong.StartApplication - Starting StartApplication v1.0 on 3638e84181ae with PID 1 (/spring-data-jpa-mysql-1.0.jar started by root in /)
book-manager-app_1 | INFO com.mkyong.StartApplication - No active profile set, falling back to default profiles: default
book-manager-app_1 | INFO o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode.
book-manager-app_1 | INFO o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 134ms. Found 1 repository interfaces.
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader org.springframework.boot.loader.LaunchedURLClassLoader#5d099f62
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - HikariPool-1 - configuration:
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - allowPoolSuspension.............false
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - autoCommit......................true
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - catalog.........................none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - connectionInitSql...............none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - connectionTestQuery.............none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - connectionTimeout...............30000
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - dataSource......................none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - dataSourceClassName.............none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - dataSourceJNDI..................none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - dataSourceProperties............{password=<masked>}
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - driverClassName................."com.mysql.cj.jdbc.Driver"
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - healthCheckProperties...........{}
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - healthCheckRegistry.............none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - idleTimeout.....................600000
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - initializationFailTimeout.......1
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - isolateInternalQueries..........false
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - jdbcUrl.........................jdbc:mysql://docker-mysql:3306/book_manager
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - leakDetectionThreshold..........0
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - maxLifetime.....................1800000
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - maximumPoolSize.................10
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - metricRegistry..................none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - metricsTrackerFactory...........none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - minimumIdle.....................10
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - password........................<masked>
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - poolName........................"HikariPool-1"
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - readOnly........................false
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - registerMbeans..................false
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - scheduledExecutor...............none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - schema..........................none
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - threadFactory...................internal
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - transactionIsolation............default
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - username........................"root"
book-manager-app_1 | DEBUG com.zaxxer.hikari.HikariConfig - validationTimeout...............5000
book-manager-app_1 | INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
book-manager-app_1 | DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Failed to create/setup connection: Communications link failure
book-manager-app_1 |
book-manager-app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
book-manager-app_1 | DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Cannot acquire connection from data source
book-manager-app_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
book-manager-app_1 |
book-manager-app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
book-manager-app_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
book-manager-app_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
book-manager-app_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
book-manager-app_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
book-manager-app_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
book-manager-app_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
book-manager-app_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
book-manager-app_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
book-manager-app_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
book-manager-app_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
book-manager-app_1 | at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)
book-manager-app_1 | at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
book-manager-app_1 | at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
book-manager-app_1 | at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:319)
book-manager-app_1 | at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:356)
book-manager-app_1 | at org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup.getDatabase(DatabaseLookup.java:73)
book-manager-app_1 | at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties.java:142)
book-manager-app_1 | at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter(JpaBaseConfiguration.java:112)
book-manager-app_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$ff868e63.CGLIB$jpaVendorAdapter$8(<generated>)
book-manager-app_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$ff868e63$$FastClassBySpringCGLIB$$5875e117.invoke(<generated>)
book-manager-app_1 | at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
book-manager-app_1 | at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
book-manager-app_1 | at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$ff868e63.jpaVendorAdapter(<generated>)
book-manager-app_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
book-manager-app_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
book-manager-app_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
book-manager-app_1 | at java.lang.reflect.Method.invoke(Method.java:498)
book-manager-app_1 | at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
book-manager-app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
book-manager-app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
book-manager-app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
book-manager-app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
book-manager-app_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
book-manager-app_1 | ... 83 common frames omitted
book-manager-app_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
book-manager-app_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
book-manager-app_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
book-manager-app_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
book-manager-app_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
book-manager-app_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
book-manager-app_1 | at java.net.Socket.connect(Socket.java:589)
book-manager-app_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
book-manager-app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
book-manager-app_1 | ... 86 common frames omitted
book-manager-app_1 | ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
book-manager-app_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
book-manager-app_1 |
book-manager-app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
book-manager-app_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
book-manager-app_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
book-manager-app_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
book-manager-app_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
book-manager-app_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
book-manager-app_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
book-manager-app_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
book-manager-app_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
book-manager-app_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
book-manager-app_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
book-manager-app_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
book-manager-app_1 | at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)
book-manager-app_1 | at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
book-manager-app_1 | at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
book-manager-app_1 | at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:319)
book-manager-app_1 | at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:356)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
book-manager-app_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
book-manager-app_1 | at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
book-manager-app_1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853)
book-manager-app_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
book-manager-app_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
book-manager-app_1 | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
book-manager-app_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
book-manager-app_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
book-manager-app_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
book-manager-app_1 | at com.mkyong.StartApplication.main(StartApplication.java:19)
book-manager-app_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
book-manager-app_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
book-manager-app_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
book-manager-app_1 | at java.lang.reflect.Method.invoke(Method.java:498)
book-manager-app_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
book-manager-app_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
book-manager-app_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
book-manager-app_1 | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
book-manager-app_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
book-org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
book-manager-app_1 | at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
book-manager-app_1 | at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMertiesSet(AbstractEntityManagerFactoryBean.java:377)
book-manager-app_1 | at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
book-manager-app_1 | at java.lang.reflect.Method.invoke(Method.java:498)
book-manager-app_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
book-manager-app_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
book-manager-app_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
book-manager-app_1 | at com.cketImpl.java:392)
book-manager-app_1 | at java.net.Socket.connect(Socket.java:589)
book-manager-app_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
book-manager-app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
book-manager-app_1 | ... 59 common frames omitted
book-manager-app_1 | WARN o.s.c.a.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
book-manager-app_1 | INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
book-manager-app_1 |
book-manager-app_1 | Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
book-manager-app_1 | ERROR o.s.boot.SpringApplication - Application run failed
book-manager-app_1 | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(Abstractorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853)
book-manager-app_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
book-manager-app_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
book-manager-app_1 | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
book-manager-app_1 | Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
book-manager-app_1 | at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentityManagerFactoryBuilderImpl.java:904)
book-manager-app_1 | at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
book-manager-app_1 | at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityMaAbstractEntityManagerFactoryBean.java:377)
book-manager-app_1 | at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
book-manager-app_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)
book-manager-app_1 | ... 23 common frames omitted
book-manager-app_1 | Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
book-manager-app_1 | at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImporg.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
book-manager-app_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
book-manager-app_1 | ... 40 common frames omitted
spring-data-jpa-mysql-compose_book-manager-app_1 exited with code 1
I'm working on project for studies. Now I am at the stage of moving project to docker containers. I have placet tomcat with my website and mysql with database do docker-compose.yml. Unfortunately classes from tomcat can't connect with database
Java code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Waliduj {
String url="jdbc:mysql://localhost:3306/uczelnia?useTimezone=true&serverTimezone=UTC";
String username="root";
String password="121212";
public boolean Waliduje(String uname,String pass) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,username,password);
PreparedStatement st=con.prepareStatement("SELECT email,haslo FROM logowanie WHERE email=? AND haslo=?");
st.setString(1,uname);
st.setString(2,pass);
ResultSet rs = st.executeQuery();
if(rs.next()) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException se) {
// TODO Auto-generated catch block
se.printStackTrace();
}
return false;
}
}
Dockerfile code
FROM tomcat:8.0.41-jre8
COPY ./webapp /usr/local/tomcat/webapps/webapp
CMD ["catalina.sh", "run"]
docker-compose.yml code
version: '3'
services:
web:
build: .
ports:
- "8080:8080"
links:
- "db:uczelnia"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: 121212
MYSQL_DATABASE: uczelnia
ports:
- "3306:3306"
PhpMyAdmin:
image: nazarpc/phpmyadmin
ports:
- "8090:80"
links:
- db:mysql
Error code
web_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
web_1 |
web_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
web_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
web_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
web_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
web_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
web_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
web_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
web_1 | at java.sql.DriverManager.getConnection(DriverManager.java:664)
web_1 | at java.sql.DriverManager.getConnection(DriverManager.java:247)
web_1 | at com.login.walidacja.Waliduj.Waliduje(Waliduj.java:20)
web_1 | at com.login.Login.doPost(Login.java:27)
web_1 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
web_1 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
web_1 | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
web_1 | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
web_1 | at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
web_1 | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
web_1 | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
web_1 | at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
web_1 | at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
web_1 | at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
web_1 | at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
web_1 | at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
web_1 | at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
web_1 | at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
web_1 | at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
web_1 | at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
web_1 | at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
web_1 | at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
web_1 | at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
web_1 | at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
web_1 | at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
web_1 | at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
web_1 | at java.lang.Thread.run(Thread.java:745)
web_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
web_1 |
web_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
web_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
web_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
web_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
web_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
web_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
web_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
web_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
web_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
web_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
web_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
web_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
web_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
web_1 | ... 30 more
web_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
web_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
web_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
web_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
web_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
web_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
web_1 | at java.net.Socket.connect(Socket.java:589)
web_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
web_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)
web_1 | ... 33 more
Additional informations:
I'm working on windows 10 so docker is on windows too.
I apologize for spelling errors but I'm from poland so english is not
my
Via the compose file, your database service will be referencable on the network via db. The links keyword isn't necessary unless you simply need a different name for your container. If you change your java code to connect to jdbc:mysql://db:3306/uczelnia?useTimezone=true&serverTimezone=UTC it should work as expected.
https://docs.docker.com/compose/networking/