How to define hostname into docker-compose.yml for MongoDB and Spring Boot

Nikolay Hristov :

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?

Ebrahim Pasbani :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135251&siteId=1