.Net核心控制台应用程序读取主机上的配置文件,如下所示。
static void Main(string[] args)
{
var configBuilder = new ConfigurationBuilder()
.AddJsonFile("//home/processorconfig/appsettings.json");
var configuration = configBuilder.Build();
...
}
当我使用-v参数运行docker image
docker run -v C:\Configs\:/home/processorconfig/ -it 858a565b2069
输出为:
指定--help以获得可用选项和命令的列表。
当我只更改volume参数中的一个字母时,它将运行容器,但应用程序会出现异常
docker run -v C:\Configs\:/home/processorconfg/ -it 858a565b2069
输出:
未处理的异常:System.IO.FileNotFoundException: 找不到配置文件'processorconfig / appsettings.json' 并且不是可选的。物理路径是 '/home/processorconfig/appsettings.json'。在 Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(布尔 重新加载) Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 供应商) Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
当我将控制台应用程序AddJsonFile路径更改为另一个路径,然后在运行时将卷添加到该路径时,我得到了相同的输出。
任何帮助将不胜感激。
与--mount
参数相同的结果
更新:找到了一个线索,当我在C:\ Configs中删除appsetings.json时,它将运行容器并获取异常消息。
答案 0 :(得分:1)
该问题可能与文件系统监视不适用于Docker上的已装载卷有关。 (有关aspnet GitHub和Docker Forum的详细信息)。
使用File.ReadAllText
命令读取文件并将字符串传递给InMemoryFileProvider挽救了我的生活。
var jsonString = File.ReadAllText("//home/processorconfig/appsettings.json");
var memoryFileProvider = new InMemoryFileProvider(jsonString);
var configuration = new ConfigurationBuilder()
.AddJsonFile(memoryFileProvider, "appsettings.json", false, false)
.Build();
答案 1 :(得分:0)
从您要映射到容器的目录中尝试以下操作:
docker run -v $(pwd):/home/processorconfig/ -it 858a565b2069
这将获取当前目录(打印工作目录)的路径。
答案 2 :(得分:0)
路径中的冒号可能有问题。我不在Windows计算机上,因此无法验证。尝试使用--mount
语法:
docker run --mount type=bind,source=C:\Configs\,target=/home/processorconfig/ -it 858a565b2069