In order to connect to the database I use a network socket, which is specified inside my hibernate.cfg.xml as:
<property name="connection.url">jdbc:postgresql://localhost:PORT/DBNAME</property>
along with username, password:
<property name="connection.username">user</property>
<property name="connection.password">pass</property>
I want to use docker for deployment.
My problem is that currently I have to change the source code (e.g hibernate.cfg.xml), and reproduce a new .war file, each time I want to re-deploy.
Is there a way to avoid this? Can I use the same .war file and link dynamically to a database from docker?
I suppose I could have a configuration file (something like .env) that would contain the db credentials and have both java and docker read this, but I don't now to configure docker to do so
I believe you have the following options:
<property name="connection.username">${db.user}</property>hibernate.cfg.xml files using maven profiles and filters to pick the right one for each environmentThe problem with system properties is that to use them you'll have to set them in your app server, use properties files or pass them through command line, as in -Ddb.user=foo. To set them in the app server, you'd have to make the container use a CLI, REST API, or maybe another configuration file (this time for the server) to set it up. It will depend on what you're deploying your .war to. Passing those properties through the command line has the problem of database credentials being there in plain text for anyone who can list that process.
Maintaining multiple configuration files, or using properties files is also a pain and it adds an overhead to your build process that I don't think it's necessary, at least not to set up simple credentials.
I believe you best bet is to use programmatic configuration. Then all you'd have to do is set the right environment variables inside the container. In your application it would be just a matter of:
Configuration cfg = new Configuration()
.setProperty("connection.username", System.getenv("DB_USER"))
// ...
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