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

The following are my application files.


public class User {

    private ObjectId _id;




public interface UserRepository extends CrudRepository<User, ObjectId> {



public class UserController {

    UserRepository repository;

    public Iterable<User> getUsers() {
        return repository.findAll();

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


When I run my application from IDE with the following

everything works.

However, my goal is to deploy my app and MongoDB with Docker/Kubernetes, so I use the following${MONGO_INITDB_ROOT_USERNAME}${MONGO_INITDB_ROOT_PASSWORD}${MONGO_INITDB_DATABASE}${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 property to “container” while running the application.

I believe, you might have missed a configuration for the class like,
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

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