4

I am building an application using Jhipster. My sample application-prod.yml looks like below as provided by Jhipster

spring:
    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:mysql://localhost:3306/MyModule?useUnicode=true&characterEncoding=utf8&useSSL=false
        name:
        username: hello
        password: hello
        hikari:
            data-source-properties:
                ...
    jpa:
        database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
        database: MYSQL
        show-sql: false
        org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
        ...

When I run the application without docker I get a mysql error if the username/password is incorrect which is normal.

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

But if I am running the same application using docker image and provide the db properties in the docker compose file, the properties in the application-prod.yml file seems to get ignored. That is, even if the database properties in the application properties file is incorrect but the correct values are provided in the docker compose file, the application seems to work fine when run using docker image and can connect to the database.

The entries in the docker file is given below

version: '2'
services:
    mymodule-mysql:
        container_name: mymodule-mysql
        image: mysql:5.7.13
        environment:
            - MYSQL_USER=root
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_ALLOW_EMPTY_PASSWORD=no
            - MYSQL_DATABASE=mymodule
        ports:
            - 3306:3306
        command: mysqld --lower_case_table_names=1 --skip-ssl

It seems that the environment variables in the docker compose file is overriding the properties application-dev.yml file. Is my thought correct ?

It will be good if someone can explain in details how this works in jhipster.

1 Answer 1

3

your observation is correct: the values specified over environment variables are overriding the ones specified in the yml file in the jar. This behavior has nothing to do with JHipster. This is pure spring-boot. Here is a short overview of the order how properties are override (from the spring doc)

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. @SpringBootTest#properties annotation attribute on your tests.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that only has properties in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. @PropertySource annotations on your @Configuration classes.
  17. Default properties (specified using SpringApplication.setDefaultProperties).

the entries in the yml file for mysql docker that you post here are the credential to the root user of RDMS mysql database which is startting as a docker service. This dose not mean that your application will use those credential. It can be that you have the same credential also in the application-prod.yml file which has been add to your war during packaging phase and then this war was put into your docker.

In the app.yml file which is used for starting docker-compose you should have also some environment varaible, e.g.

environment:
        - SPRING_PROFILES_ACTIVE=prod
        - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/myDataBase?useUnicode=true&characterEncoding=utf8&useSSL=false
        - JHIPSTER_SLEEP=10 # gives time for the database to boot before the application

for spring which are overriding your application-prod.yml files. Also is importat that the mysql container is known to your app container.

Sign up to request clarification or add additional context in comments.

4 Comments

If I understand correctly, 1) entries in environment variables of app.yml are overriding application-prod.yml properties. So, username & password in application-prod.yml are not being used. 2) Credentials in mysql.yml file are used to start docker service & provide root user credentials. So is the application using the default root ceredentials for connecting to the db ?
But if I want my application to use a different set of credentials (other than root) where and should I mention it. I cannot mention it in the application-prod.yml as the mysql properties are being overridden. In my case the docker image seemed to start even if wrong username/password is provided in the application-prod.yml which indicates that root credentials of mysql.yml might be used. Is it so ?
1) correct 2) correct I don't think that your application is using the default root credential. Can be that your application is running in dev mode and the it use the in memory h2 database?
you can add your credential in the application-prod.yml. Those are gone be ignored only if credentials over the environment variables are specified, i.e. is not mandatory to be specified. If you have a look at the doc or my answer you can see that the settings over environment variable are at the 10 position when looking up for credentials. On the other hand the variable in application-prod.yml in your jar file are checked later at the 15 position.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.