hkutluay
hkutluay

Reputation: 6944

Docker run volume parameter Specify --help for a list of available options and commands

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

Upvotes: 0

Views: 912

Answers (3)

hkutluay
hkutluay

Reputation: 6944

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();

Upvotes: 1

Carlos Jafet Neto
Carlos Jafet Neto

Reputation: 891

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

Upvotes: 1

Eran
Eran

Reputation: 729

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

Upvotes: 0

Related Questions