1

I want load data from a CSV file into my program on startup. On my local machine it works just fine, but when I try to Dockerize the spring boot application, my CSV file cannot be found.

private final static String GIFTS_CSV = "gifts.csv";
public final static String PATH = "src/main/resources/static/";

public static Map<Integer, Gift> getGifts() throws IOException {
    String line;
    HashMap<Integer, Gift> gifts = new HashMap<>();

    BufferedReader br = new BufferedReader(new FileReader(PATH + GIFTS_CSV));
    br.readLine();
    while ((line = br.readLine()) != null) {
        String[] giftStr = line.split(CVS_SPLIT_BY);
        Gift gift = new Gift(Integer.parseInt(giftStr[0]), 
                         new Point(Double.parseDouble(giftStr[1]),
                Double.parseDouble(giftStr[2])), Double.parseDouble(giftStr[3]));
        gifts.put(gift.getId(), gift);
    }
    return gifts;
}

Is there a way to get this data in both environments? Or what would be the path on the docker image?

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Paul Fournel
  • 10,807
  • 9
  • 40
  • 68

1 Answers1

0

The entire point of Docker is to isolate the executing process from any dependencies on the execution environment. In order for such a process to act upon data like files you need to include these within the container.

One method is build a container that includes the CSV file (using the Dockerfile), but it would be more natural to use a docker volume. I suggest you read the following documentation:

https://docs.docker.com/engine/userguide/dockervolumes/

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • See also http://stackoverflow.com/questions/34441797/how-do-write-a-csv-file-in-a-docker-container-volume-with-java-8/34452791#34452791 – Mark O'Connor Dec 24 '15 at 12:45