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

784
Visualizações
How to define hostname into docker-compose.yml for MongoDB and Spring Boot

I'm trying to create docker-compose.yml with MongoDB and Spring boot. The problem is that I don't want to hardcode any database connections into the Spring's application.properties.

From this article I saw that you can start a mongodb container with predefined hostname. As I understan all services defined into the compose yml file are in the same network but they has different ips, right? So the main question is how to define mongodb hostname into the docker-compose.yml and use that name into the spring boot application.properties file.

I will try to give as much information as I can but if something is unclear let me know.

Here is some information about the Spring Boot appication.

Dependencies that I'm using into the spring project are:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

The appication has interfaces that extends MongoRepository:

import org.springframework.data.mongodb.repository.MongoRepository;

public interface ServiceLoggerRepository extends MongoRepository<ExampleEntity, String> {
}

and it is using @Document annotated entities

import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "example")
public class ExampleEntity {

    @Id
    private String _id;
    private String name;
}

The database connection is defined into the application.properties file:

#here is the problem - this the vm ip
spring.data.mongodb.uri=mongodb://192.168.0.51 
spring.data.mongodb.database=mongo-test

Information about the Spring Boot's Dockerfile:

FROM openjdk:8-jre
COPY ./mongo-test.jar ./usr/mongo-test.jar
EXPOSE 8080
RUN sh -c 'touch /usr/mongo-test.jar'
ENTRYPOINT ["java","-jar","/usr/mongo-test.jar"]

Information about docker-compose: It has 2 services - MongoDB and Spring Boot application.

version: "3"
services:
  mongodb:
    restart: always
    image: mongo
    container_name: "mongodb-test"
    ports:
    - 27017:27017
    command: --smallfiles
  mongo-logger:
    restart: always
    build: ./mongo-test
    container_name: "mongo-test"
    ports:
    - 8080:8080
    depends_on:
    - mongodb
    links:
    - mongodb

When the container is created and ran it works. But the problem is that I have no idea where and how define spring.data.mongodb.uri to use something static like spring.data.mongodb.uri=mongodb://mongo-hostname for example.

Is it possible and are there any king solutions or good practices for doing this?

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

0

To access to mongodb you need to use mongodb service name defined in docker-compose.yml.

And you can set that in application properties using env vars like below:

//aplication.properties
...
spring.data.mongodb.uri=${MONGODB_URI}

and

//docker-compose.yml
version: "3"
services:
  mongodb:
    image: mongo
    container_name: "mongodb-test"
    ports:
    - 27017:27017
  mongo-logger:
    build: ./mongo-test
    container_name: "mongo-test"
    ports:
    - 8080:8080
    environment:
    - MONGODB_URI=mongodb://mongodb
    depends_on:
    - mongodb
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