Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
Java hidden properties without Spring

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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

We have multiple solutions for this:

Secret Docker

Create a file with the properties syntax:

//secret-file.txt
db.password=psw
db.username=user

With this file create a docker secret in your docker :

$ docker secret create test-secret secret-file.txt

And use this with the java library docker-secrets in your java program :

Map<String, String> secrets = DockerSecrets.loadFromFile("test-secret");
System.out.println(secrets.get("db.password")) // readonly

For more example, look here.


Environment variables

Set the environment variables in the docker with -e argument :

$ docker run -e DB_PASSWORD=pwd -e DB_USERNAME=user registry/image:tag

And use these variables with System::getenv in your java program :

System.out.println(System.getenv("DB_PASSWORD"))

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 :

$ docker run -e JAVA_OPTS="-Ddb.password=pwd -Ddb.username=user" registry/image:tag

And use these variables with System::getProperty in your java program :

System.out.println(System.getProperty("db.password"))

Program arguments

Give arguments to docker run command :

It is important to give the arguments after declaring the image.

$ docker run registry/image:tag pwd user

And use these arguments with main method in your java program :

public static void main(String[] args) {
    System.out.println("The password: " + args[0]);
    System.out.println("The username: " + args[1]);
}

For better handling of arguments, you can use the Apache's commons-cli java library or use a another library.

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda