Spring Boot App容器无法连接到MySQl容器

时间:2019-07-16 20:37:20

标签: spring-boot docker docker-compose dockerfile

我正在尝试将Spring Boot应用容器化以与多种环境(生产和开发)一起使用。在我的开发环境中,我使用docker-compose启动一个MySQL服务器实例,当我的Spring Boot应用程序在容器外部运行时,我的Spring Boot应用程序可以连接到该实例,但是当我的Spring Boot应用程序在容器中运行时,它无法连接连接到MySQL服务器。

这是我运行docker-compose up时遇到的错误。

app_1  | 19:48:07.708 ERROR -- [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'sessionRepositoryFilterRegistration' defined in class path resource [org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]: Unsatisfied dependency expressed through method 'sessionRepositoryFilterRegistration' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration': Unsatisfied dependency expressed through method 'setTransactionManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: 
app_1  | Unable to obtain connection from database (jdbc:mysql://localhost:3306/app) for user 'root': Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app_1  | SQL State  : 08S01
app_1  | Error Code : 0
app_1  | Message    : Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | 
app_1  | 19:48:07.736  INFO -- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
app_1  | 19:48:07.740  WARN -- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
app_1  | 19:48:07.751  INFO -- [           main] ConditionEvaluationReportLoggingListener : 
app_1  | 
app_1  | Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
app_1  | 19:48:07.757 ERROR -- [           main] o.s.boot.SpringApplication               : Application run failed
app_1  | 
app_1  | org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
app_1  |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157)
app_1  |    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
app_1  |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
app_1  |    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
app_1  |    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
app_1  |    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
app_1  |    at com.brand.app.AppApp.main(AppApp.java:28)
app_1  |    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
app_1  |    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
app_1  |    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
app_1  |    at java.lang.reflect.Method.invoke(Method.java:498)
app_1  |    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
app_1  |    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
app_1  |    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
app_1  |    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
app_1  | Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
app_1  |    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:125)
app_1  |    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86)
app_1  |    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:427)
app_1  |    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:180)
app_1  |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:181)
app_1  |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
app_1  |    ... 14 common frames omitted
app_1  | Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionRepositoryFilterRegistration' defined in class path resource [org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]: Unsatisfied dependency expressed through method 'sessionRepositoryFilterRegistration' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration': Unsatisfied dependency expressed through method 'setTransactionManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: 
app_1  | Unable to obtain connection from database (jdbc:mysql://localhost:3306/app) for user 'root': Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app_1  | SQL State  : 08S01
app_1  | Error Code : 0
app_1  | Message    : Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | 
app_1  |    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
app_1  |    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
app_1  |    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
app_1  |    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:235)
app_1  |    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:226)
app_1  |    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addServletContextInitializerBeans(ServletContextInitializerBeans.java:101)
app_1  |    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:88)
app_1  |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:261)
app_1  |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:234)
app_1  |    at org.springframework.boot.web.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:54)
app_1  |    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5139)
app_1  |    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
app_1  |    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377)
app_1  |    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1367)
app_1  |    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
app_1  |    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
app_1  |    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
app_1  |    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:902)
app_1  |    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:831)
app_1  |    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
app_1  |    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377)
app_1  |    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1367)
app_1  |    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
app_1  |    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
app_1  |    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
app_1  |    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:902)
app_1  |    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
app_1  |    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
app_1  |    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:423)
app_1  |    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
app_1  |    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:928)
app_1  |    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
app_1  |    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:455)
app_1  |    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:106)
app_1  |    ... 19 common frames omitted
app_1  | Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration': Unsatisfied dependency expressed through method 'setTransactionManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: 
app_1  | Unable to obtain connection from database (jdbc:mysql://localhost:3306/app) for user 'root': Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app_1  | SQL State  : 08S01
app_1  | Error Code : 0
app_1  | Message    : Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | 
app_1  |    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:676)
app_1  |    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
app_1  |    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
app_1  |    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
app_1  |    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:392)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
app_1  |    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
app_1  |    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
app_1  |    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
app_1  |    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
app_1  |    ... 59 common frames omitted
app_1  | Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: 
app_1  | Unable to obtain connection from database (jdbc:mysql://localhost:3306/app) for user 'root': Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app_1  | SQL State  : 08S01
app_1  | Error Code : 0
app_1  | Message    : Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | 
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
app_1  |    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
app_1  |    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
app_1  |    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:668)
app_1  |    ... 82 common frames omitted
app_1  | Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: 
app_1  | Unable to obtain connection from database (jdbc:mysql://localhost:3306/app) for user 'root': Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app_1  | SQL State  : 08S01
app_1  | Error Code : 0
app_1  | Message    : Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | 
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
app_1  |    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307)
app_1  |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1115)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:407)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:341)
app_1  |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
app_1  |    at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:120)
app_1  |    at org.springframework.orm.jpa.JpaTransactionManager.setBeanFactory(JpaTransactionManager.java:313)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1800)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1765)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
app_1  |    ... 91 common frames omitted
app_1  | Caused by: org.flywaydb.core.internal.exception.FlywaySqlException: 
app_1  | Unable to obtain connection from database (jdbc:mysql://localhost:3306/app) for user 'root': Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app_1  | SQL State  : 08S01
app_1  | Error Code : 0
app_1  | Message    : Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  | 
app_1  |    at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:60)
app_1  |    at org.flywaydb.core.internal.database.DatabaseFactory.createDatabase(DatabaseFactory.java:72)
app_1  |    at org.flywaydb.core.Flyway.execute(Flyway.java:1670)
app_1  |    at org.flywaydb.core.Flyway.migrate(Flyway.java:1356)
app_1  |    at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837)
app_1  |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774)
app_1  |    ... 108 common frames omitted
app_1  | Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  |    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
app_1  |    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)

以下是其中一些文件。

  • docker-compose.yml-启动MySQL服务器并使用Dockerfile构建
  • Dockerfile-复制必要的文件并运行entrypoint.sh
  • entrypoint.sh-启动Spring Boot应用jar
  • application.properties
  • application-dev.properties

docker-compose.yml

version: "3"

services:
  app:
    build:
      context: ..
      dockerfile: Dockerfile
      args:
        SPRING_PROFILES_ACTIVE: dev
    image: app:dev
    depends_on:
      - mysql
  mysql:
    image: mysql:5.6
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: app
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - mysql_data:/var/lib/mysql/data
volumes:
  mysql_data:

Dockerfile

FROM openjdk:8-jdk-alpine

ARG SPRING_PROFILES_ACTIVE=""
ENV SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE}

COPY target/app.jar /usr/share/
COPY script/entrypoint.sh /bin/entrypoint.sh

ENTRYPOINT /bin/entrypoint.sh

EXPOSE 5050

entrypoint.sh

#!/bin/sh

sleep 15

exec /usr/bin/java \
     -jar /usr/share/app.jar \
     --spring.profiles.active=${SPRING_PROFILES_ACTIVE}

有人知道为什么Spring Boot无法连接到MySQL服务器吗?两个容器都应位于同一默认网络上。

2 个答案:

答案 0 :(得分:0)

Spring Boot应用正在尝试连接到jdbc:mysql://localhost:3306/app,当该应用不在容器中但在容器中时,localhost无效。 MySQL服务器服务的名称为mysql(在docker-compose.yml中定义),因此将localhost替换为mysql将解决此问题。

答案 1 :(得分:0)

要从docker服务连接到另一个docker服务(例如数据库服务),必须在docker-machine中设置本地IP,在这种状态下,不知道docker service本地IP,我们可以使用服务名称代替服务IP进行调用。因此,在内部docker-machine网络中,用于更改与mysql数据库或作为docker服务的任何数据库的连接时,应将URL连接更改为docker服务IP或服务名称

例如

数据库URL连接

jdbc:mysql://your-mysql-service-name:3306/app

docker-compose声明服务

version: '3.1'
services:

  your-mysql-service-name:
    container_name: your-mysql-service-name
    image: mysql:5.6
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    deploy:
      replicas: 1
    ports:
    - 3306:3306
    environment:
      MYSQL_DATABASE: "db_name"
      MYSQL_ROOT_PASSWORD: "your-password"