javajsondockergradlecloudant

How to access app resource files stored in 'src/main/resources/someFolder' within a Docker container?


I am working on a gradle based service that should run in a docker container in a kubernetes cluster. Locally, I can access the resources, some json files used for CouchDb initialisation. Once I build and deploy the service in Docker, the files are no longer accessible and I get file not found exception and can't start the service. I can see that my folder is in the generated jar but I can't access the files. I am using it like follows:

DesignDocument design = DesignDocumentManager.fromFile("design-documents/documents.json");

which comes from the IBM Cloudant Java Client API. I have tried different versions from the path, i.e. with a leading slash or the absolute path from the project.


Solution

  • The API does not have an alternative to File, which is an operating system File. So use a temporary file.

    InputStream in = getClass().getResourceAsStream(
            "/design-documents/documents.json");
    Path tempPath = Files.createTempFile("desdoc-", ".json");
    Files.copy(in, tempPath);
    DesignDocument design = DesignDocumentManager.fromFile(tempPath.toFile());
    Files.delete(tempPath);
    

    Note the leading / for Class.getResource(AsStream).