MongoDB error trying to execute 'docker build' of my Spring Boot application (but as standalone app it works)

The following are my application files.

Model:

@Document("users")
public class User {

    @Id
    private ObjectId _id;

...

}

Repository:

public interface UserRepository extends CrudRepository<User, ObjectId> {

}

Controller:

@RestController
public class UserController {

    @Autowired
    UserRepository repository;

    @GetMapping(path="/getAllUsers")
    public Iterable<User> getUsers() {
        return repository.findAll();
    }

    @PostMapping(path="/addUser", consumes={"application/JSON"}, produces="application/json")
    public User createUser(@RequestBody User u) {
        return repository.save(u);
    }

}

When I run my application from IDE with the following application.properties:

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=...
spring.data.mongodb.password=...
spring.data.mongodb.database=mydb
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost

everything works.

However, my goal is to deploy my app and MongoDB with Docker/Kubernetes, so I use the following application.properties:

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=${MONGO_INITDB_ROOT_USERNAME}
spring.data.mongodb.password=${MONGO_INITDB_ROOT_PASSWORD}
spring.data.mongodb.database=${MONGO_INITDB_DATABASE}
spring.data.mongodb.port=27017
spring.data.mongodb.host=${MONGODB_HOSTNAME}

(where the env variables are the values that I will set in Kubernetes YAML files).
But, in this case, when I call ‘docker build -t user-service:v1 ./user-service’ I get many errors! I posted the starting point of the error as a screenshot:

Without MongoDB integration the command works, so I think that it is something related to it.

Sorry if I was imprecise, but I’m still a beginner. Thanks in advance.

Hi @Rino_Di_Paola and welcome to the MongoDb community forum!!

As quoted in the spring documentation:

If a @Configuration class is marked with @Profile, all of the @Bean methods and @Import annotations associated with that class will be bypassed unless one or more of the specified profiles are active.

Therefore, because of @Profile, the class is not instantiated by Spring.
The recommendation would be to set spring.profiles.active property to “container” while running the application.

I believe, you might have missed a configuration for the class like,
@EnableAutoConfiguration
@Configuration
@SpringBootApplication
which are required to enable the auto-configuration of Spring ApplicationContext by scanning the classpath components and registering the beans.

You can refer to the documentation for Spring on Kubernetes for better understanding.

I tried to replicate the issue in my local environment with Java version 19 and Gradle version 7.6 and the application runs fine with the docker enabled.

Having said that, to me this sounds like a Java/Spring issue, so I believe you would receive better help and expertise in a Spring-specific forum or a general QA forum like StackOverflow for generic concerns.

Please let us know if you have any further questions.

Best Regards
Aasawari

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.