英文:
Not able to build an image in Spring Boot 2.3.3
问题
我有一个相当基本的`Spring Boot`应用程序,它连接到一个经过docker化的`MySql`数据库并暴露了一个端点。
数据库已经通过网络进行了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
我想将该应用程序容器化,并与上述docker化的`MySql`数据库连接在一起,尽管指定了配置文件`tst`,但无法工作。当构建镜像时,我看到选择了`tst`配置文件,但它执行了测试,这就是它在连接到数据库时失败的地方。
只要跳过测试,镜像就会成功构建,然后从镜像中启动应用程序的新容器就可以成功连接到数据库。
**工作,镜像已构建,并在浏览器中运行容器会输出“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.
...
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
...
**TradestarApplicationTests.java**
@SpringBootTest
class TradestarApplicationTests {
@Test
void contextLoads() {
}
}
**application-tst.properties**
spring.datasource.url=jdbc:mysql://mysql:3306/tradestar?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=tradestar
spring.datasource.password=tradestar
...
**MyController.java**
@RestController
public class MyController {
@GetMapping("/")
public String greet() {
return "Hello World!";
}
}
**pom.xml**
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...>
...
</project>
英文:
I have a pretty basic Spring Boot
application that connects to a dockerized MySql
DB and exposes just one endpoint.
DB is already dockerized and using a network.
docker run -d -v mysql-volume:/var/lib/mysql -p3306:3306 --network app-db-network --name mysql -e "MYSQL_ROOT_PASSWORD=root" mysql:latest
I would like to containerize the application and connect it with dockerized MySql
DB above which is not working despite specifying profile tst
. When the image gets built I see that the tst
profile gets picked up but it executes tests and that's there it fails while connecting to DB.
As soon as I skip the tests the image gets built correctly and then spinning up a new container of the application from the image works and is able to connect to DB successfully.
WORKS AND THE IMAGE GETS BUILT AND RUNNING THE CONTAINER GIVES OUTPUT "Hello Tradestar" IN THE BROWSER !!!
./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
DOES NOT WORK, GIVES FOLLOWING ERROR !!!
./mvnw spring-boot:build-image -Dspring-boot.run.profiles=tst
Error Log:
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() {
}
}
application-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>
答案1
得分: 2
如果您运行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://mysql容器的ip地址:3306/
英文:
If you run docker-compose and use mysql
as connection string. it will work.
But you are using docker run
command line for each container so change mysql
to:
172.17.0.1
is an IP address gateway of container network.
it is : jdbc:mysql://172.17.0.1:3306/
OR: docker inspect mysql_container_id
to get IP address of mysql container.
Change mysql
to : jdbc:mysql://ip_address_of_mysql_container:3306/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论