Spring Boot无法读取Docker中的application.properties

在Docker容器中运行时,
Spring Boot不读取应用程序属性.

我的application.yml

server:
  port: 8080
  context-path: /mail
custom:
  greeting: Hello YML

Dockerfile

FROM java:8-jre
VOLUME /tmp
COPY ./mail.jar /app/mail.jar
RUN sh -c 'touch /app/mail.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app/mail.jar" ]
EXPOSE 8080

还有一个简单的ApplicationController

@RestController
public class ApplicationController {

  private final Environment environment;

  @Autowired
  public ApplicationController(Environment environment) {
    this.environment = environment;
  }

  @RequestMapping(path = "/")
  public Hello hello() {
    final Hello hello = new Hello();
    hello.setGreeting(environment.getProperty("custom.greeting"));
    return hello;
  }
}

我正在使用IntelliJ Docker插件自动映射端口8080(docker):18080(主机)并在http:// localhost:18080上提供应用程序

搬运工人

>未应用server.context-path属性.应用仍然可用/而不是/ mail /
> custom.greeting属性不从属性文件读取,控制器返回{“greeting”:null}而不是{“greeting”:“Hello YML”}

没有Docker

>正确应用了context-path
> custom.greeting属性由控制器正确返回

最佳答案 您必须在docker / app /目录中添加application.properties文件. Ur docker目录结构将是

app
   -main.jar
   -application.properties

您可以使用ADD /ur/local/location/application.properties /app/application.properties来完成此操作

然后在docker文件中更好地编写此命令

ENTRYPOINT ["java" ,"-Djava.security.egd=file:/dev/./urandom --spring.config.location=classpath:file:/app/application-properties","-jar","/app/main.jar"]

你的整个dockerFile应该是这样的:

FROM java:8-jre
VOLUME /tmp
COPY ./mail.jar /app/mail.jar
ADD /ur/local/location/application.properties /app/application.properties
ENTRYPOINT ["java" ,"-Djava.security.egd=file:/dev/./urandom --spring.config.location=classpath:file:/app/application-properties","-jar","/app/main.jar"]
EXPOSE 8080
点赞