2

I am new to this Dockerization part. I want to Dockerize the existing project. In this project we have multiple application.properties and Default.properties files which are not available in src/main/resources folder. While creating image it is working fine , but while running image it is throwing a error message like below. I tried by adding COPY option as well for application.properties file.

Please find below docker file data and error message i am receiving and let me know if i want to add more details.

@UPdating question

As i am moving properties file from local to /myapp/ I want to update my script to read external prop file. Here i am using below code , can someone guide me how to read file from docker contains using Java.

File[] files = null;
            files = Paths.get(environment.getProperty("spring.config.location")).getParent().resolve(DATABASES).toFile()
                    .listFiles();
  • List of files.

    /usr/app # ls
    env-prop services-testingtools.jar
    /usr/app # cd env-prop/
    
    /usr/app/env-prop # ls
    local
    /usr/app/env-prop # cd local/Jlo
    
    /usr/app/env-prop/local # ls
    application.properties databases
    /usr/app/env-prop/local # cd databases/
    
    /usr/app/env-prop/local/databases # ls
    DEFAULT.properties
    
    
  • Docker file :

    
    FROM openjdk:8-jdk-alpine
    EXPOSE 3434
    RUN mkdir /myapp
    ADD target/services-testingtools.jar /myapp/services-testingtools.jar
    ADD /env-prop/local/databases/DEFAULT.properties /myapp/DEFAULT.properties
    ADD /env-prop/local/application.properties /myapp/application.properties
    ENV spring.config.location=file:///myapp/DEFAULT-properties
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom --spring.config.location=file:///myapp/application-properties","-jar","/myapp/services-testingtools.jar"]
    
    
  • Error message.

    2020-12-09 09:21:59.595 ERROR 1 --- [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'tokenAuthorizationOncePerRequestFilter': Unsatisfied dependency expressed through field 'tokenUtil'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tokenUtil': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jwt.signing.key.secret' in value "${jwt.signing.key.secret}"
    2020-12-09 09:21:59.627  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    2020-12-09 09:21:59.677  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    2020-12-09 09:21:59.695  INFO 1 --- [           main] ConditionEvaluationReportLoggingListener :
    
    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2020-12-09 09:21:59.703 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed
    
    org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
            at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
    
Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59
Mani
  • 75
  • 2
  • 10

1 Answers1

2

Everything looks fine with your configuration. The problem is that you are not setting the spring.config.location in a right way.

Please, try:

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom --spring.config.location=file:///myapp/application.properties","-jar","/myapp/services-testingtools.jar"]

Basically, remove the classpath prefix and adjust the file protocol URI.

Instead of the ENTRYPOINT, you can also define the config file location by defining the following environment variable in your Dockerfile:

ENV SPRING_CONFIG_LOCATION=file:///myapp/application.properties

You do not need any specific code to process the file, with these settings you are instructing Spring to apply that configuration.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • "-Djava.security.egd=file:/dev/./urandom" I am not sure what is the exact use of this..i got this from outside and i am using this. Do i need to update anything here or it is predefined path to set up this ? – Mani Dec 09 '20 at 13:20
  • It has to do with random number generation, typically in old OSes. Please, see [this](https://stackoverflow.com/questions/58991966/what-java-security-egd-option-is-for) related SO question and answer. – jccampanero Dec 09 '20 at 13:22
  • Still i am getting same error. It seems still it is not able to identify the properties file. Update docker file code above. Do i need to update code to read file from config folder ? I provided the code which i am using to read pro files. – Mani Dec 09 '20 at 13:39
  • Please, instead of `file:///myapp/application-properties` use `file:///myapp/application.properties`, note the difference between the dash and the dot. Please, can you try again? Please, also use the `ENV` setting or the `--spring.config.location` in the `ENTRYPOINT`, not both. Also, the `ENV` setting should look like `ENV SPRING_CONFIG_LOCATION=file:///myapp/application.properties`. Please, modify the Dockerfile as appropriate – jccampanero Dec 09 '20 at 13:46
  • We have two properties file..in DEFAULT properties file we are storing DB related information, So i have to use both properties files – Mani Dec 09 '20 at 13:48
  • I realized that. The second properties should be processed as a `PropertySource` in your configuration classes, something like `@PropertySource("file:///myapp/DEFAULT.properties")` – jccampanero Dec 09 '20 at 13:54
  • In this scenario i am using normal java code to read file from project directory...If i keep this in do i need to update the code for read data from docker container (file:///myapp/DEFAULT.properties) – Mani Dec 09 '20 at 13:59
  • No, you can use in your java code and process it as you need, you do not need Spring for that. Please, probably it will be better to use a `Properties` class. – jccampanero Dec 09 '20 at 14:07
  • If i proceed with same code we are getting error. mework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.hsbc.testingtools.TtServicesApplication]; nested exception is java.io.FileNotFoundException: /myapp/databases/DEFAULT.properties (No such file or directory) – Mani Dec 09 '20 at 14:12
  • Please, try `/myapp/DEFAULT.properties` instead of `/myapp/databases/DEFAULT.properties`, that is the location you are `ADD`ing it in your `Dockerfile`, or `ADD` the file in the right directory – jccampanero Dec 09 '20 at 14:14
  • No i updated that for default properties ADD /env-prop/local/databases/DEFAULT.properties /myapp/databases/DEFAULT.properties – Mani Dec 09 '20 at 14:15
  • Please, can you try `@PropertySource("file:/myapp/databases/DEFAULT.properties")`? – jccampanero Dec 09 '20 at 17:19
  • Tried this as well ...... getting nested exception is java.io.FileNotFoundException: /myapp/databases/DEFAULT.properties (No such file or directory) – Mani Dec 10 '20 at 04:37
  • Please,can you verify if the file is actually in the indicated path? You can do it by running your container and perform ssh into it. Do you know how to do that? – jccampanero Dec 10 '20 at 07:26
  • All file are available , I updated the question with the result. But it seems i need to update file read functionality. I provided the code with path, Please check the code for that also. For file read do i need to update the code, because it is searching for config file path and databases folder. – Mani Dec 10 '20 at 08:43
  • Issue resolved....I hardcoded the path of the Default properties file after copy into the docker container. Thanks for your support , thanks a lot – Mani Dec 10 '20 at 09:00
  • Thank you very much! Please if you need further help,do not hesitate to contact me – jccampanero Dec 10 '20 at 09:21