Overview
I've been writing myself a fun little Angular/C# advent calendar for my advent of code answers. The aim is to teach myself how to host a site using docker containers, with the aim of being able to load the site on my phone once I've hosted everything.
However, I've hit a snag:
Issue
I'm reading in the advent of code input from the filesystem using the following code:
public static IEnumerable<string> ReadStringInput(string inputPath)
{
string inputFilePath = "C:\\work\\VisualAdventOfCode\\APIAdventOfCode\\Puzzles\\" inputPath;
return File.ReadLines(inputFilePath);
}
(The file at input path is just a .txt
file with my puzzle input)
However, once the Docker container is running this code, I get the following error:
System.IO.FileNotFoundException: Could not find file '/app/C:\work\VisualAdventOfCode\APIAdventOfCode\Puzzles_2022\Input\OneInput.txt'.
(i.e. it can no longer read the file directly).
I thought I could just make the filepath relative depending on the environment, but reading up, it sounds like, by design, Docker isn't able to read files from the solution.
Potential solutions
I'd like to find a better way to read in my puzzle input. I've got some ideas for hacky ways, but I'd like to do this "properly" if I'm using it as a learning exercise.
My best guess is:
- Copy the files into dist
- Find a way to reference them from there if live
But I'm honestly a bit lost. Can anyone point me towards any answers/documentation about how best to do this?
Thanks so much!
CodePudding user response:
You can't read files on the host from inside the container unless you specifically allow it when you run the container. It also looks like your container is a Linux container and your file path is Windows.
To let the container read the files, you'll map the puzzles directory on Windows to a path in the container file system. If you map it to /puzzles
you'd add -v C:\work\VisualAdventOfCode\APIAdventOfCode\Puzzles\:/puzzles
to your docker run
command (before the image name).
If you have a file in the puzzles directory called day1.txt
, that will then be available as /puzzles/day1.txt
in the container. Note that unlike the Windows file system, the Linux file system is case sensitive, so make sure you use the exact file names.
Another option is to add the puzzle files to your solution as embedded resources. Then they'll be included in the DLL and you can read them as resources.
A third option is to COPY
the puzzle files into the image in your Dockerfile. Then they'll be included in the image, so you can read them.