Spring Boot无法读取Docker中的application.properties [英] Spring Boot can't read application.properties in Docker
问题描述
在Docker容器中运行时,Spring Boot不会读取应用程序属性.
Spring Boot doesn't read application properties when running in Docker container.
我的 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(host),并使应用程序在 http://localhost:18080
I'm using IntelliJ Docker plugin that automatically map ports 8080(docker):18080(host) and makes app available on http://localhost:18080
Docker
- server.context-path属性未应用.该应用程序仍然可以通过/使用,而不能通过/mail/ 使用
- custom.greeting属性不会从属性文件中读取,并且控制器返回 {"greeting":null} 而不是 {"greeting":"Hello YML"} >
- server.context-path property isn't applied. App still available with / and not with /mail/
- custom.greeting property not reads from properties file and controller returns {"greeting":null} instead {"greeting":"Hello YML"}
没有Docker
-
上下文路径已正确应用
context-path applied properly
custom.greeting属性由控制器正确返回
custom.greeting property returns by controller properly
推荐答案
您必须在docker/app/目录中添加application.properties文件.您的docker目录结构将为
You have to add the application.properties file in the docker /app/ directory. Ur docker directory structure will be
app
-main.jar
-application.properties
您可以使用ADD /ur/local/location/application.properties /app/application.properties
那么最好将此命令写入您的docker文件中
Then better write this command in your docker file
ENTRYPOINT ["java" ,"-Djava.security.egd=file:/dev/./urandom --spring.config.location=classpath:file:/app/application-properties","-jar","/app/main.jar"]
您的整个dockerFile应该如下所示:
Your whole dockerFile should look like this:
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
这篇关于Spring Boot无法读取Docker中的application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!