I am currently creating a Java program that uses a MongoDB database and I am storing the connection information in a properties file.
But my project is opensource on GitHub and I cannot store the connection information in the properties file.
And so I wanted to ask you if it is possible to give the login information from docker run.
example : docker run registry/image -args db.password=psw db.username=user
I have seen solutions in stackoverflow but all solutions use Spring features, but my project does not use Spring framework.
3
Answers
We have multiple solutions for this:
Secret Docker
Create a file with the properties syntax:
With this file create a docker secret in your docker :
And use this with the java library docker-secrets in your java program :
For more example, look here.
Environment variables
Set the environment variables in the docker with
-e
argument :And use these variables with
System::getenv
in your java program :VM Arguments
This solution depends on your base image that was used to create your Docker container.
Give VM Arguments to the docker run command :
And use these variables with
System::getProperty
in your java program :Program arguments
Give arguments to docker run command :
It is important to give the arguments after declaring the image.
And use these arguments with main method in your java program :
For better handling of arguments, you can use the Apache's commons-cli java library or use a another library.
if you are hosting this application on AWS an option is using Secrets Manager which has an integration start project for Spring – How to integrate AWS Secret Manager with Spring Boot Application
You can:
Use a .properties file for your properties
Add it to .gitignore so that you don’t push it to GitHub
Add a .properties_example file that contains placeholders for others to override in their local repo, e.g: