vs 2017 COPY 失败:Docker 文件,CreateFile 系统找不到指定的文件。
vs 2017 COPY 失败:Docker 文件,CreateFile 系统找不到指定的文件。
复制失败:CreateFile \\?\\C:\\ProgramData\\Docker\\tmp\\docker-builder056926419\\WebApplication1.sln:系统找不到指定的文件。\n示例文件:\nFROM microsoft/aspnetcore:2.0 AS base\nWORKDIR /app\nEXPOSE 80\nFROM microsoft/aspnetcore-build:2.0 AS builder\nWORKDIR /src\nCOPY WebApplication1.sln ./\nCOPY WebApplication1/WebApplication1.csproj Web/\nRUN dotnet restore\nCOPY . .\nWORKDIR /src/Web\nRUN dotnet build -c Release -o /app\nFROM builder AS publish\nRUN dotnet publish -c Release -o /app\nFROM base AS production\nWORKDIR /app\nCOPY --from=publish /app .\nENTRYPOINT [\"dotnet\", \"WebApplication1.dll\"]
在使用VS 2017进行COPY操作时出现了问题,错误信息为"Docker File, CreateFile The system cannot find the file specified"。经过分析,发现问题出现在目录结构上。解决方法如下:
- 将dockerfile移动到bin/Release/PublishOutput目录下(在运行VS发布后,选择文件系统)
- 将所有文件移动到一个名为site的文件夹中
- 运行docker build命令
具体的讨论可以参考https://forums.docker.com/t/copy-failed-trying-to-install-plain-asp-net-mvc-app-to-a-container/57217/3。目前尚不清楚如何在VS中自动解决这个问题。
在Visual Studio 2019中,我发现了一个有趣的问题。当您尝试创建由VS自动生成的Dockerfile时,生成的Dockerfile会放在主项目中,而不是根(.sln)文件夹中,其中存在.dockerignore文件。
我有以下项目的文件夹结构:
. .. .dockerignore .gitignore MyProject.API MyProject.API.Client MyProject.Model MyProject.Repository MyProject.Repository.Settings MyProject.sln README.md Tests.MyProject.Repository Tests.Model Tests.MongoContext.Infrastructure Tests.MongoQuery Tests.OdataFilters
主项目是MyProject.API.csproj(Web API),位于MyProject.API中。
从VS自动生成的dockerfile具有以下结构:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM microsoft/dotnet:2.2-sdk AS build WORKDIR /src COPY ["MyProject.API/MyProject.API.csproj", "MyProject.API/"] COPY ["MyProject.Model/MyProject.Model.csproj", "MyProject.Model/"] COPY ["MyProject.Repository/MyProject.Repository.csproj", "MyProject.Repository/"] COPY ["MyProject.Repository.Settings/MyProject.Repository.Settings.csproj", "MyProject.Repository.Settings/"] RUN dotnet restore "MyProject.API/MyProject.API.csproj" COPY . . WORKDIR "/src/MyProject.API" RUN dotnet build "MyProject.API.csproj" -c Release -o /app FROM build AS publish RUN dotnet publish "MyProject.API.csproj" -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "MyProject.API.dll"]
当我尝试根据Docker的说明构建我的镜像时,我遇到了问题:
Linux容器类型:
Step 7/20 : COPY ["MyProject.API/MyProject.API.csproj", "MyProject.API/"] COPY failed: stat /var/lib/docker/tmp/docker-builder799586484/MyProject.API/MyProject.API.csproj: no such file or directory
Windows容器类型:
Step 7/20 : COPY ["MyProject.API/MyProject.API.csproj", "MyProject.API/"] COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder753862033\MyProject.API\MyProject.API.csproj: The system cannot find the path specified.
根据此答案,我将我的Dockerfile移动到根文件夹(存在.sln和.dockerignore文件的位置),问题得到解决。