I am new using docker and I am trying to create an image using docker build and finally the image is create successfully. But when I try to run the image throws me the next error. Someone could help me to understand what is happening with my app. (When I run the app in local I don’t hace any problem, even executing the jar everything is ok) the problem is only in docker.
This is my Dockerfile
FROM openjdk:17-jdk-buster AS build
WORKDIR /app
COPY build.gradle settings.gradle gradlew /app/
COPY gradle /app/gradle
COPY src /app/src
RUN chmod +x gradlew
RUN ./gradlew build --no-daemon --info
FROM openjdk:17-jdk-buster
WORKDIR /app
COPY --from=build /app/build/libs/*.jar /app/application.jar
COPY --from=build /app/build/resources/main/cassandra_truststore.jks /app/cassandra_truststore.jks
EXPOSE 8000
ENTRYPOINT ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseContainerSupport", "-Djava.security.egd=file:/dev/./urandom","-jar","/app/application.jar"]
This is my Interface
public interface VisitorsRepository extends CassandraRepository<VisitorEntity, String> {}
this is my service, I created the bean manually so I don’t need @Service
@RequiredArgsConstructor
@Slf4j
public class VisitorService {
@Autowired
private VisitorsRepository repository;
public ApiResponse execute(VisitorDto dto){
return Mono.just(repository.save(VisitorMapper.dtoToEntity(dto)))
.map(visitorEntity ->
ResponseMapper.buildResponse(true, HttpStatus.CREATED.value(), HttpStatus.CREATED.getReasonPhrase(), HttpStatus.CREATED.value(), HttpStatus.CREATED.getReasonPhrase())
)
.doOnSuccess(m -> log.info("entity saved"))
.doOnError(error -> log.error("Error saving entity: " + error.getMessage()))
.onErrorResume(error -> Mono.error(new RuntimeException(error.getMessage()))).block();
}
}
this is the @Bean
@Configuration
public class ServiceConfiguration {
@Bean
public VisitorService getVisitorService(){
return new VisitorService();
}
}
this is my controller
@RestController
@CrossOrigin(origins = "*")
@RequestMapping(value = "/api")
@AllArgsConstructor
@Validated
@Slf4j
public class VisitorController {
private final VisitorService service;
@PostMapping("/create-visitor")
public ResponseEntity<ApiResponse> execute(@RequestBody @Valid VisitorDto dto){
try{
ApiResponse apiResponse = service.execute(dto);
return new ResponseEntity<ApiResponse>(apiResponse,HttpStatus.OK);
}catch (Exception e){
e.getStackTrace();
throw new BadRequestException(e.getMessage());
}
}
}
And this is the error
ConfigServletWebServerApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘visitorController’ defined in URL [jar:nested:/app/application.jar/!BOOT-INF/classes/!/ipservice/controller/VisitorController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name ‘getVisitorService’: Unsatisfied dependency expressed through field ‘repository’: Error creating bean with name ‘visitorsRepository’ defined in ipservice.repository.VisitorsRepository defined in @EnableCassandraRepositories declared on CassandraConfig: Cannot resolve reference to bean ‘cassandraTemplate’ while setting bean property ‘cassandraTemplate’