I am running a JAR in a Docker container. Functionality of my applications is to connect to a DB and get records and output it to a .csv file inside a folder called reports.
Both my src folder and reports folder are in same directory. Which means I can write to the file like below.
String csvFile = "." + File.separator + "reports" + File.separator + "VAS_Report_" + subscriberType +
df.format(new Date()) + ".csv";
//db connection and result extraction logic
........
CSVWriter csvWriter = new CSVWriter(new FileWriter(csvFile));
csvWriter.writeAll(resultSet, true);
This works fine when I run the program locally. I build the project and bundle it as a JAR file.
I create a dockerfile with other relevant steps and following step (to create a folder named reports in the folder where my JAR will be copied)
RUN mkdir -p /apps/Consumer/
COPY My-App-1.0.jar /apps/Consumer/
RUN mkdir -p /apps/Consumer/reports
RUN chmod ugo+w /apps/Consumer/reports #giving write permission
Docker image builds successfully, db connection success and during run time when it tries to write to csv file, the application throws exceptions stating that it cannot find specified folder/file (FileNotFound Exception).
What am I doing wrong here?
Following are additional questions which came across while searching for a solution.
Does bundling an application preserve project structure? (Since I needed to manually create a folder named reports in docker container) Do I need to provide permissions for created folder (reports) in any way? (Which I did here)
In place of:
RUN mkdir -p /apps/Consumer/
...
RUN mkdir -p /apps/Consumer/reports
Use WORKDIR as:
WORKDIR /apps/Consumer/
COPY My-App-1.0.jar .
RUN chmod ugo+w /apps/Consumer/reports #giving write permission