Spring Boot cannot read application.properties in Docker
Spring Boot does not read application properties when running in a Docker container.
My 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
And a simple 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;
}
}
I am using IntelliJ Docker plugin which automatically maps ports 8080 (docker): 18080 (host) and makes the application available at http: // localhost: 18080
Docker
- the server.context-path property does not apply. The app is still available from / , not from / mail /
- The custom.greeting property is not read from the properties file, and the controller returns {"greeting": null} instead of {"greeting": "Hello YML"}
No docker
-
context path being applied correctly
-
custom.greeting returns properties to the controller correctly
source to share
You must add the application.properties file to the docker / app / directory. Ur dock directory structure will be
app
-main.jar
-application.properties
You can do this using ADD /ur/local/location/application.properties /app/application.properties
Then you better write this command in your dockerfile
ENTRYPOINT ["java" ,"-Djava.security.egd=file:/dev/./urandom --spring.config.location=classpath:file:/app/application-properties","-jar","/app/main.jar"]
Your entire 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
source to share