Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Connection in Jhipster Application while using Docker

Tags:

jhipster

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.

like image 639
Soumya Avatar asked Sep 19 '25 23:09

Soumya


1 Answers

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.

like image 127
duderoot Avatar answered Sep 23 '25 10:09

duderoot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!