该应用程序在不使用 docker 运行时连接得很好mvn spring-boot:run
。使用mvn docker:remove docker:stop clean package docker:build docker:start
以下错误运行时抛出
错误:
[main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.DatabaseException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)
应用程序.yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/db?user=root&createDatabaseIfNotExist=true
username: root
password: new_password
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
connection-timeout: 20000
idle-timeout: 10000
liquibase:
enabled: true
pom.xml
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>docker-build</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>prepare-database</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>remove-database</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<images>
<image>
<name>mysql</name>
<alias>database</alias>
<run>
<log>
<prefix>mysql > </prefix>
<enabled>true</enabled>
<color>green</color>
</log>
<env>
<MYSQL_ROOT_PASSWORD>${db.password}</MYSQL_ROOT_PASSWORD>
<MYSQL_DATABASE>${db.name}</MYSQL_DATABASE>
<MYSQL_PASSWORD>${db.password}</MYSQL_PASSWORD>
</env>
<ports>
<port>3306:3306</port>
</ports>
<wait>
<log>(?s)ready for connections</log>
<time>30000</time>
</wait>
</run>
</image>
<image>
<alias>service</alias>
<name>service</name>
<build>
<from>java:8-jre</from>
<cmd>
<shell>java -jar /maven/mysql-producer-consumer-1.0-SNAPSHOT.jar</shell>
</cmd>
<assembly>
<descriptorRef>artifact-with-dependencies</descriptorRef>
</assembly>
</build>
<run>
<log>
<prefix>service > </prefix>
<enabled>true</enabled>
<color>black</color>
</log>
<wait>
<shutdown>500</shutdown>
<time>20000</time>
</wait>
<dependsOn>
<container>mysql</container>
</dependsOn>
</run>
</image>
</images>
</configuration>
</plugin>
更改datasource
url
为以下值无效:
jdbc:mysql://mysql/db?user=root&createDatabaseIfNotExist=true
jdbc:mysql://database/db?user=root&createDatabaseIfNotExist=true
jdbc:mysql://localhost/db?user=root&createDatabaseIfNotExist=true