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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With