我使用Jib生成Spring Boot 映像并将其推送到DockerHub
mvn compile jib:build
pom包含参考:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>languages.json</include>
</includes>
</resource>
</resources>
然后我使用 docker-compose differentes 容器打包并运行
javaws:
container_name: javaws
image: myJavaWS:latest
environment:
- SPRING_PROFILES_ACTIVE=${ENV}
- SPRING_CONFIG_LOCATION=${JAVA_WS_ENV_PROP_FILE}
expose:
- ${JAVA_WS_PORT}
ports:
- 9000:9000
links:
- my-db
depends_on:
- my-db
volumes:
- "/root/DOCKER/config:/config"
- ${FS_FILE_PATH}:/root/path
在 docker-compose 中,我也设置了application-.yml和 Spring 活动配置文件以使用
JAVA_WS_ENV_PROP_FILE=file:/config/application-WSTEST.yml
.jar 还有一个application.properties文件;泊坞窗图像也是;
info.app.description=My WS
info.app.name=My java WS
info.app.version=@project.version@
从DockerHub下载图像后,我会在图像内部验证该文件是否存在!
应用程序运行失败,因为 application.properties 中的属性尚未解析。
2020-11-13 14:23:05,673 ERROR org.springframework.boot.SpringApplication : Application run failed
smrt-java-ws | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springShopOpenAPI' defined in class path resource [ch/smartravel/config/SwaggerOASConfig.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'info.app.name' in value "${info.app.name}"
smrt-java-ws | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529)
smrt-java-ws | at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
smrt-java-ws | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
smrt-java-ws | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
控制器端点是:
@Configuration
public class SwaggerOASConfig {
@Bean
public OpenAPI springShopOpenAPI(
@Value("${info.app.name}") String appName,
@Value("${info.app.version:unknow}") String appVersion) {
return new OpenAPI()