Home > Mobile >  Confused by Docker directory structure (file not found error)
Confused by Docker directory structure (file not found error)

Time:10-06

Good Morning,

I am getting a file not found error and even though I manually create the file in the docker container it still reports as not found. Solving this is of course complicated by me being new to docker and learning how everything in the docker world works.

Using docker desktop with a .net core application.

In the .Net application I am looking for the file to use as an email template. All of this works when I run outside a docker container but inside docker it fails with file not found.

public async Task SendEmailAsyncFromTemplate(...)
{
   ...snipped for brevity
        
        string path = Path.Combine(Environment.CurrentDirectory, @$"Infrastructure\Email\{keyString}\{keyString}.cshtml");

        _logger.LogInformation("path: "   path);

        //I added this line because when I connect to docker container the root 
        //appears to start with infrastructure so I chopped the app part of
        var fileTemplatePath = path.Replace(@"/app/", "");

        _logger.LogInformation("filePath: "   fileTemplatePath);

The container log for the above is

[12:40:09 INF] path: /app/Infrastructure\Email\ConfirmUser\ConfirmUser.cshtml
[12:40:09 INF] filePath: Infrastructure\Email\ConfirmUser\ConfirmUser.cshtml

As mentioned in the comments I did this because when I connect to the container the root shows Infrastructure as the first folder.

enter image description here

So naturally I browse into Infrastructure and the Email folder is missing. I have asked a separate SO question here about why my folders aren't copying.

OK my Email files and folders under Infrastructure are missing. So to test this out I manually created the directory structure and create the cshtml file using this command:

docker exec -i addaeda2130d sh -c "cat > Infrastructure/Email/ConfirmUser/ConfirmUser.cshtml" < ConfirmUser.cshtml

I chmod the file permissions to 777 just to make sure the application has write access and then added this debugging code.

_logger.LogInformation("ViewRender: "   filename);

try
{
   _logger.LogInformation("Before FileOpen");
   var fileExista = File.Exists(filename);

   _logger.LogInformation("File exists: "   fileExista);

   var x = File.OpenRead(filename);

  _logger.LogInformation("After FileOpen:", x.Name);

As you can see from the logs it reports the file does NOT exist even though I just created it.

[12:40:09 INF] ViewRender: Infrastructure\Email\ConfirmUser\ConfirmUser.cshtml
[12:40:09 INF] Before FileOpen
[12:40:09 INF] File exists: False

Well the only logical conclusion to this is I don't know / understand what is going on which is why I am reaching out for help.

I have also noted that if I stop the container (not recreate just stop) and then start it all my directories and files I created are gone.

So...are these directories / files in memory and not on "disk" and I need to commit the changes somehow?

It would seem to make sense as the application code is looking for the files on disk and if they are in memory then they wouldn't be found but in Googling, Pluralsight courses etc. I can't find any mention of this.

Any help or direction where to look in figuring this out would be greatly appreciated.

CodePudding user response:

Front slash '/' in path is different than ''. Just change direction of your slashes and it'll work.

I tried this program in my docker container and it worked fine.

using System;
using System.IO;

// forward slash don't work
// string path = Path.Combine(Environment.CurrentDirectory, @"files\hello\hello.txt");

string path = Path.Combine(Environment.CurrentDirectory, @"files/hello/hello.txt");

Console.WriteLine($"Path: {path}");

string text = System.IO.File.ReadAllText(path);

Console.WriteLine(text);
  • Related