skip to Main Content

I have a spring boot application, which picks up a series of properties from a file, and which path is set as an environment variable. So when I start the spring boot application, I pass the following variable:

--other.properties.path=external/directories/path/otherpropertiesfile.properties

The reason why we use a separate properties file, other than the application properties, is because the spring boot application relies on an underlying (different) application, with a separate properties file parser.

Now that we have moved the spring boot application to a docker image, we would like to pass this path in the environment variables of the docker container. What is currently working, is setting the path at the creation of the container, and then copying the necessary files into the container. After restarting the container, the property file is found and the application runs properly.

I am wondering if there is a better solution, making the external properties file (which is thus not the application.properties) file, available to the docker container, and reading the properties files at startup.

Note: it doesn’t make sense to pack the external properties file at the creation of the image. The properties file needs to be changed easily and regularly, and the different properties included in it are each time different (e.g. containing an encryption key). The best that we could do is copy a template.

Thx a lot!

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to egeorge I got it working.

    How I implemented it was the following:

    #create properties volume
    docker volume create propertiesvolume
    
    #copy propertiesfile and other relevant files to propertiesvolume volume with the help of a temporary container named alpine
    docker run -d --name alpine --mount type=volume,source=propertiesvolume,target=/data alpine
    docker cp ./local-properties-directory alpine:/data
    docker stop alpine
    docker rm alpine
    
    #run container with propertiesvolume volume
    docker run -d 
            --name containername 
            --mount type=volume,source=propertiesvolume,target=/home/properties 
            -e external.properties.path=/home/properties/propertiesvolume/external.properties 
            -e spring.profiles.active=ssl 
            -p8443:8443 
            image:1.0.0
    

  2. This is a common thing when you start working heavily with SpringBoot

    Here is an example of how I would do it.

    ./src/main/java/com/example/dockerconfig/DockerConfigApplication.java

    package com.example.dockerconfig;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import java.nio.file.Files;
    import java.nio.file.Path;
    
    @SpringBootApplication
    public class DockerConfigApplication implements CommandLineRunner {
    
        @Value("${other.properties.path}")
        private String propPath;
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Properties Path = " + propPath);
            var externalProps = Files.readString(Path.of(propPath));
            System.out.println("******************");
            System.out.println(externalProps);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(DockerConfigApplication.class, args);
        }
    }
    

    ./src/main/resources/application.properties

    other.properties.path=default.properties
    

    ./default.properties

    prop.for.external.app=foo
    

    ./Dockerfile

    FROM openjdk:19-alpine
    VOLUME /tmp
    COPY build/libs/*.jar app.jar
    COPY default.properties .
    ENTRYPOINT ["java","-jar","/app.jar"]
    

    Build image

    $ docker build -t docker-config .
    

    Run Image with default config

    ~/work/docker-config >  docker run --rm -it docker-config
    
      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |___, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::                (v3.0.3)
    
    2023-03-01T22:47:00.378Z  INFO 1 --- [           main] c.e.d.DockerConfigApplication            : Starting DockerConfigApplication v0.1 using Java 19-ea with PID 1 (/app.jar started by root in /)
    2023-03-01T22:47:00.381Z  INFO 1 --- [           main] c.e.d.DockerConfigApplication            : No active profile set, falling back to 1 default profile: "default"
    2023-03-01T22:47:01.090Z  INFO 1 --- [           main] c.e.d.DockerConfigApplication            : Started DockerConfigApplication in 1.256 seconds (process running for 1.705)
    Properties Path = default.properties
    ******************
    prop.for.external.app=foo
    
    ~/work/docker-config >
    

    Override with new file

    ./extern.properties

    prop.for.external.app=somethingdifferent
    

    To override with this file, we have to mount the file into the container and also override the config property.

    -v "$(pwd)"/extern.properties:/extern.properties makes the file available in the container.

    -e other.properties.path=extern.properties overrides the config value.

    ~/work/docker-config >  docker run --rm -it -v "$(pwd)"/extern.properties:/extern.properties -e other.properties.path=extern.properties  docker-config
    
      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |___, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::                (v3.0.3)
    
    2023-03-01T22:55:57.578Z  INFO 1 --- [           main] c.e.d.DockerConfigApplication            : Starting DockerConfigApplication v0.1 using Java 19-ea with PID 1 (/app.jar started by root in /)
    2023-03-01T22:55:57.580Z  INFO 1 --- [           main] c.e.d.DockerConfigApplication            : No active profile set, falling back to 1 default profile: "default"
    2023-03-01T22:55:58.298Z  INFO 1 --- [           main] c.e.d.DockerConfigApplication            : Started DockerConfigApplication in 1.281 seconds (process running for 1.752)
    Properties Path = extern.properties
    ******************
    prop.for.external.app=somethingdifferent
    
    ~/work/docker-config >
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search