Docker镜像无法读取Spring Boot应用程序中的application.yml文件。

huangapple go评论71阅读模式
英文:

Docker image can't read application.yml file on Spring Boot Application

问题

以下是你提供的内容的翻译部分:


我目前正在尝试将我的Spring Boot应用程序进行Docker化,其中包含两个配置文件:

application-dev.ymlapplication-prod.yml

当我在我的计算机上运行该应用程序时,它是正常工作的,但是当我尝试进行Docker化时,它无法读取这些文件。它无法将变量加载到@Value注解中,甚至服务端口也是错误的。

19:30:24.437 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basicContractService': Injection of autowired dependencies failed; nested exception is
 java.lang.IllegalArgumentException: Could not resolve placeholder 'service.msisdn.address' in value "${service.msisdn.address}"
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
        ...(后续堆栈信息省略)

以下是我的Dockerfile:

FROM maven:3-jdk-11 AS builder
RUN mkdir -p /usr/local/backend
RUN chmod 777 /usr/local/backend/
ADD settings.xml /root/.m2/
COPY settings.xml /usr/share/maven/ref/
COPY pom.xml /usr/share/maven/ref/repository
COPY . /usr/local/backend
WORKDIR /usr/local/backend
RUN mvn clean generate-sources package
FROM openjdk:11
COPY --from=builder /usr/local/backend/target/service-userinfosearch.jar /usr/local/backend/
RUN chmod 777 /usr/local/backend/
WORKDIR /usr/local/backend
ENTRYPOINT ["java", "-jar", "service-userinfosearch.jar","-Dspring.profiles.active=dev"]

我使用以下命令进行构建:

docker build . -t userinfosearch/v1

并使用以下命令运行:

docker run --rm --name version1 -p 8082:8082 userinfosearch/v1

英文:

I'm currently trying to dockerize my spring boot app that has 2 configuration files:

application-dev.yml and application-prod.yml

When I run the app on my machine it works, but when I try to dockerize, it can't read from said files. It doesnt load variables to the @Value annotations, even the service port is wrong.

19:30:24.437 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basicContractService': Injection of autowired dependencies failed; nested exception is
 java.lang.IllegalArgumentException: Could not resolve placeholder 'service.msisdn.address' in value "${service.msisdn.address}"
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
        at br.com.customer.service.userinfosearch.Swagger2SpringBoot.main(Swagger2SpringBoot.java:24)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'service.msisdn.address' in value "${service.msisdn.address}"
        at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
        at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
        at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
        at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        ... 23 common frames omitted

Here's my dockerfile:

FROM maven:3-jdk-11 AS builder
RUN mkdir -p /usr/local/backend
RUN chmod 777 /usr/local/backend/
ADD settings.xml /root/.m2/
COPY settings.xml /usr/share/maven/ref/
COPY pom.xml /usr/share/maven/ref/repository
COPY . /usr/local/backend
WORKDIR /usr/local/backend
RUN mvn clean generate-sources package
FROM openjdk:11
COPY --from=builder /usr/local/backend/target/service-userinfosearch.jar /usr/local/backend/
RUN chmod 777 /usr/local/backend/
WORKDIR /usr/local/backend
ENTRYPOINT ["java", "-jar", "service-userinfosearch.jar","-Dspring.profiles.active=dev"]

I'm building using this command:

docker build . -t userinfosearch/v1

And running with this one:

docker run --rm --name version1 -p 8082:8082 userinfosearch/v1

答案1

得分: 0

不用介意,问题已解决。

将以下内容添加到运行命令中即可:

-e "SPRING_PROFILES_ACTIVE=dev"

英文:

Neverming, solved.

Adding the following to the run command did it:

-e "SPRING_PROFILES_ACTIVE=dev"

huangapple
  • 本文由 发表于 2020年10月10日 05:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64287599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定