Docker run Spring Boot application with Env Var for @PropertySource: java.io.FileNotFoundException: class path resource

40 Views Asked by At

I have Spring Boot application that I am trying to build a Docker image for, but it has a @PropertySource that is an environment variable and I am having trouble getting docker run to run the jar with the file location as an environment variable.

for local development, the file is located at ./config/props.properties and I can just add the env var PROP_LOCATION=config/prop.properties

I'm trying to build a docker image that can use an env var to specify the file location but I just keep getting:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.MyApp]; nested exception is java.io.FileNotFoundException: class path resource [prop.properties] cannot be opened because it does not exist

Here is the code (shorted):

Maven Shade Plugin (for executable jar):

...
           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <id>shade-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <transformers>
                                <transformer implementation=
                                                     "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.example.MyApp</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
...

Java

@SpringBootApplication
@PropertySource("${PROP_LOCATION}")
public class MyApp extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

Docker:

FROM openjdk:8

COPY ./config/prop.properties /app/config/prop.properties
COPY .target/myapp-0.0.0-SNAPSHOT-shaded.jar /app/app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/app/app.jar"]

when I run this via:

docker build -t foo . && docker run --env PROP_LOCATION=app/config/prop.properties foo

I can open up docker desktop and see that the prop file is inside the app/config folder, but no matter what I do, I can't figure out how to get the fatjar to look there.

What am I missing here?

0

There are 0 best solutions below