我有一个非常基本的Spring Boot应用程序,该应用程序连接到dockerized的MySql DB,并且仅公开一个端点。
数据库已经被docker化并正在使用网络。

docker run -d -v mysql-volume:/var/lib/mysql -p3306:3306 --network app-db-network --name mysql -e "MYSQL_ROOT_PASSWORD=root" mysql:latest
我想将应用程序容器化,并将其与上面指定了MySql的dockerized tst DB结合使用。构建镜像后,我看到tst配置文件被拾取,但是它执行测试,并且在那里连接数据库失败。
一旦我跳过测试,就可以正确构建镜像,然后从镜像开始旋转应用程序的新容器,并且能够成功连接到DB。
工作原理和图像构建和运行容器使浏览器中的输出“Hello Tradestar”发出!
./mvnw spring-boot:build-image -Dspring-boot.run.profiles=tst -DskipTests 

docker run -d -p8080:8080 -e "SPRING_PROFILES_ACTIVE=tst" --network app-db-network --name tradestar tradestar:1.0.1-SNAPSHOT
不起作用,出现以下错误!
./mvnw spring-boot:build-image -Dspring-boot.run.profiles=tst 
错误日志:
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        ... 137 common frames omitted
Caused by: java.net.UnknownHostException: mysql: Name or service not known
        at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
        at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929) ~[na:na]
        at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515) ~[na:na]
        at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848) ~[na:na]
        at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505) ~[na:na]
        at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364) ~[na:na]
        at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298) ~[na:na]
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.21.jar:8.0.21]
        ... 140 common frames omitted

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.381 s <<< FAILURE! - in com.kanaarigroup.finance.tradestar.TradestarApplicationTests
[ERROR] contextLoads  Time elapsed: 0.009 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   TradestarApplicationTests.contextLoads » IllegalState Failed to load Applicati...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.066 s
[INFO] Finished at: 2020-10-12T23:21:01Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project tradestar: There are test failures.
TradestarApplicationTests.java
@SpringBootTest
class TradestarApplicationTests {

    @Test
    void contextLoads() {
    }

}
应用程序-tst.properties
spring.datasource.url=jdbc:mysql://mysql:3306/tradestar?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=tradestar
spring.datasource.password=tradestar
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
## Hibernate Properties
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
# Show or not log for each sql query
spring.jpa.show-sql=true
# drop n create table, good for testing, comment this in production
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
# Naming strategy
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
#control the initialization of datasource with available DDL and DML scripts
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:sql/schema.sql
spring.datasource.data=classpath:sql/data.sql
MyController.java
@RestController
public class MyController {

    @GetMapping("/")
    public String greet() {
        return "Hello World!";
    }

}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kanaarigroup.finance</groupId>
    <artifactId>tradestar</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <name>tradestar</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <!-- DB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    ...
</project>      

最佳答案

如果您运行docker-compose并使用mysql作为连接字符串。它会工作。
但是,您正在为每个容器使用docker run命令行,因此请将mysql更改为:172.17.0.1是容器网络的IP地址网关。
它是:jdbc:mysql://172.17.0.1:3306/或:docker inspect mysql_container_id获取mysql容器的IP地址。
mysql更改为:jdbc:mysql://ip_address_of_mysql_container:3306/

08-04 16:16