0

.Net core console app reads config file on main like below.

static void Main(string[] args)
{

     var configBuilder = new ConfigurationBuilder()
                      .AddJsonFile("//home/processorconfig/appsettings.json");

     var configuration = configBuilder.Build();
     ...
}

When I run docker image with -v parameter

docker run -v C:\Configs\:/home/processorconfig/ -it  858a565b2069

output is:

Specify --help for a list of available options and commands.

When I change just a letter in volume parameter it runs container but app gets exception

docker run -v C:\Configs\:/home/processorconfg/ -it  858a565b2069

Output:

Unhandled Exception: System.IO.FileNotFoundException: The configuration file 'processorconfig/appsettings.json' was not found and is not optional. The physical path is '/home/processorconfig/appsettings.json'. at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()

When I change console app AddJsonFile path to another, and then add volume to the path on run, I got same output.

Any help would be appreciated.

Same result with --mount parameter

Update: Found a clue that when I delete appsetings.json in C:\Configs it runs container and get exception message.

10
  • You wrote that you change a letter in the volume parameter but pasted the same line. Either way, seems like your missing an 'i' in your command line argument /home/processorconfg Commented Feb 4, 2019 at 14:43
  • You re right, i mispasted command to queson. I have updated Commented Feb 4, 2019 at 14:59
  • That "change one letter and it almost works" thing smells a lot like file access conflict. Can you share your Dockerfile? Commented Feb 4, 2019 at 15:53
  • Some of the examples under docker for windows and sharing drives seems to substitute forward slashes, for back slashes. Maybe that'll be of use for Windows paths for the --mount parameter too. Commented Feb 4, 2019 at 15:56
  • 1
    It's not the Dockerfile, so maybe the ConfigurationBuilder API is keeping the file exclusively open. Perhaps if you used the reloadOnChange flag it would work? Or use a custom file provider? Commented Feb 4, 2019 at 16:28

3 Answers 3

1

Try like this from the directory that you want to map to the container:

docker run -v $(pwd):/home/processorconfig/ -it  858a565b2069

This will get the path of your current directory (print working directory).

Sign up to request clarification or add additional context in comments.

7 Comments

docker: Error response from daemon: create ${PWD}: "${PWD}" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path. See 'docker run --help'.
Are you using Power Shell or Git Bash ?
Same result that in question on PowerShell (Specify --help for a list of.. )
Sorry. I made a typo. Is $(pwd):/home/processorconfig and not ${PWD}. pwd with parentheses. I will edit that.
Now error is : invalid reference format. I think that ${PWD} command was correct syntax. Error possibly related with reading file, please find update on question.
|
1

The problem is possibly related with file system watch does not work with mounted volumes on Docker. (Details on aspnet GitHub and Docker Forum).

Reading file with File.ReadAllText command and pass string to InMemoryFileProvider saved my day.

var jsonString = File.ReadAllText("//home/processorconfig/appsettings.json");
var memoryFileProvider = new InMemoryFileProvider(jsonString);
var configuration = new ConfigurationBuilder()
    .AddJsonFile(memoryFileProvider, "appsettings.json", false, false)
    .Build();

Comments

0

The colon in the path may be problematic. I'm not on a windows machine so can't validate. Try using the --mount syntax:

docker run --mount type=bind,source=C:\Configs\,target=/home/processorconfig/ -it  858a565b2069

1 Comment

same result, screenshot added to question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.