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.