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.

hkutluay
  • 6,794
  • 2
  • 33
  • 53
  • 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` – Eran Feb 04 '19 at 14:43
  • You re right, i mispasted command to queson. I have updated – hkutluay Feb 04 '19 at 14:59
  • That "change one letter and it almost works" thing smells a lot like file access conflict. Can you share your Dockerfile? – Eran Feb 04 '19 at 15:53
  • Some of the examples under docker for windows and [sharing drives](https://docs.docker.com/docker-for-windows/#shared-drives) seems to substitute forward slashes, for back slashes. Maybe that'll be of use for Windows paths for the --mount parameter too. – Mr Moose Feb 04 '19 at 15:56
  • @Eran question updated – hkutluay Feb 04 '19 at 16:08
  • 1
    It's not the Dockerfile, so maybe the `ConfigurationBuilder` API is keeping the file exclusively open. Perhaps if you used the [`reloadOnChange`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.jsonconfigurationextensions.addjsonfile?view=aspnetcore-2.2#Microsoft_Extensions_Configuration_JsonConfigurationExtensions_AddJsonFile_Microsoft_Extensions_Configuration_IConfigurationBuilder_System_String_System_Boolean_System_Boolean_) flag it would work? Or use a custom file provider? – Eran Feb 04 '19 at 16:28
  • I got same error in every path or without path when `realoadOnChange = true` – hkutluay Feb 04 '19 at 17:26
  • Thanks @Eran, using custom profiler solved my problem. – hkutluay Feb 06 '19 at 06:31
  • @hkutluay thanks for posting the solution. Probably a typo there and should be provider instead of profiler, no? – Eran Feb 06 '19 at 08:29
  • @Eran, yes you are right. Answer updated, thanks for your help! – hkutluay Feb 06 '19 at 08:35

3 Answers3

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).

  • 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'. – hkutluay Feb 04 '19 at 15:41
  • Are you using Power Shell or Git Bash ? – Carlos Jafet Neto Feb 04 '19 at 15:46
  • Same result that in question on PowerShell (Specify --help for a list of.. ) – hkutluay Feb 04 '19 at 16:03
  • Sorry. I made a typo. Is $(pwd):/home/processorconfig and not ${PWD}. pwd with parentheses. I will edit that. – Carlos Jafet Neto Feb 04 '19 at 16:13
  • 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. – hkutluay Feb 04 '19 at 16:19
  • I think you are missing the workdir in your dockerfile. WORKDIR /Processor. This will set the context where you are running your application from. – Carlos Jafet Neto Feb 04 '19 at 16:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187875/discussion-between-carlos-jafet-neto-and-hkutluay). – Carlos Jafet Neto Feb 04 '19 at 16:33
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();
hkutluay
  • 6,794
  • 2
  • 33
  • 53
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
hkutluay
  • 6,794
  • 2
  • 33
  • 53
Eran
  • 719
  • 4
  • 13