![](/img/trans.png)
[英]Cannot run container in Docker for Windows: “System cannot find the file specified”
[英]Link Docker container to host file system (windows)
我有一个 web api(dotNetCore 3.1),我正在尝试对它进行 Dockerize。
当我想将文件写入我的基本主机时,事情开始变得复杂。 从我读到的内容来看,这可能与定义“卷”有关。
假设我有这部分代码:
using (StreamWriter sr = new StreamWriter(@"C:\program\tmp.txt"))
{
while (max == -1 || counter < max)
{
try
{
sr.WriteLine($"Counter: {++counter}");
}
catch (Exception)
{
throw;
}
}
}
我设法构建了一个容器并正确运行它,但可能抛出异常,因为他不知道指定的路径。
这是我的 Dockerfile:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1903 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["dockerTest/dockerTest.csproj", "dockerTest/"]
RUN dotnet restore "dockerTest/dockerTest.csproj"
COPY . .
WORKDIR "/src/dockerTest"
RUN dotnet build "dockerTest.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "dockerTest.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dockerTest.dll"]
如果您能帮我将主机文件系统链接到容器,我会很高兴。
当您使用 CLI 启动 docker 容器时,您必须使用该选项来安装驱动器。 (您可能需要稍微使用主机路径)。
$ docker run -d --name devtest -v C:\data\input.data:/data nginx:latest
容器启动后,您不必使用主机路径
using (StreamWriter sr = new StreamWriter(@"/data/input.data"))
这应该在大多数情况下有效。 小曲折。
您还应该考虑 docker compose 文件,它可以为您节省大量输入和传递太多命令行参数的工作。
更多信息在这里:
Docker CLI: https : //docs.docker.com/storage/volumes/
Docker Compose: https : //docs.docker.com/compose/compose-file/#volume-configuration-reference
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.