执行dotnet-docker aspnetapp的docker build命令时遭遇文件占用错误
系统信息
.NET SDK: Version: 8.0.205 Commit: 3e1383b780 Workload version: 8.0.200-manifests.818b3449 Runtime Environment: OS Name: Windows OS Version: 10.0.19045 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.205\ .NET workloads installed: There are no installed workloads to display. Host: Version: 8.0.5 Architecture: x64 Commit: 087e15321b .NET SDKs installed: 6.0.423 [C:\Program Files\dotnet\sdk] 8.0.202 [C:\Program Files\dotnet\sdk] 8.0.205 [C:\Program Files\dotnet\sdk]
- Windows 10笔记本电脑
- Docker Desktop 4.3
- Visual Studio IDE(CLI执行
docker build同样失败) - 暂未发现防火墙问题(无法100%确认),2017年微软曾关闭过同类问题
问题描述
运行微软官方的基础aspnetapp应用,执行docker build -t aspnetapp时频繁报错,核心错误是无法添加文件...[文件路径]...进程无法访问文件,因为另一个进程正在使用它。用Process Monitor查看发现docker.exe在访问这些文件,但不清楚原因,其他容器运行正常。

错误日志
time="2024-06-04T17:17:09-05:00" level=error msg="Can't add file \\\?\\C:\\code\\simple-dotnet6-app-running-windows-container\\dotnet-docker\\samples\\aspnetapp\\.vs\\aspnetapp\\FileContentIndex\\bec4c4e8-54fd-4b4d-b17f-f94c9982374f.vsidx to tar: open \\\?\\C:\\code\\simple-dotnet6-app-running-windows-container\\dotnet-docker\\samples\\aspnetapp\\.vs\\aspnetapp\\FileContentIndex\\bec4c4e8-54fd-4b4d-b17f-f94c9982374f.vsidx: The process cannot access the file because it is being used by another process." time="2024-06-04T17:17:09-05:00" level=error msg="Can't add file \\\?\\C:\\code\\simple-dotnet6-app-running-windows-container\\dotnet-docker\\samples\\aspnetapp\\.vs\\aspnetapp\\v17 to tar: archive/tar: missed writing 1694429 bytes" time="2024-06-04T17:17:09-05:00" level=error msg="Can't add file \\\?\\C:\\code\\simple-dotnet6-app-running-windows-container\\dotnet-docker\\samples\\aspnetapp\\.vs\\aspnetapp\\v17\\.futdcache.v2 to tar: archive/tar: missed writing 1694429 bytes" time="2024-06-04T17:17:09-05:00" level=error msg="Can't add file \\\?\\C:\\code\\simple-dotnet6-app-running-windows-container\\dotnet-docker\\samples\\aspnetapp\\.vs\\aspnetapp\\v17\\.suo to tar: archive/tar: missed writing 1694429 bytes" time="2024-06-04T17:17:09-05:00" level=error msg="Can't add file \\\?\\C:\\code\\simple-dotnet6-app-running-windows-container\\dotnet-docker\\samples\\aspnetapp\\.vs\\aspnetapp\\v17\\DocumentLayout.json to tar: archive/tar: missed writing 1694429 bytes" time="2024-06-04T17:17:09-05:00" level=error msg="Can't add file \\\?\\C:\\code\\simple-dotnet6-app-running-windows-container\\dotnet-do
解决方法
1. 排除Visual Studio临时文件
从日志能看到Docker在尝试打包.vs目录下的文件,这些是Visual Studio的缓存文件,根本不需要打包进镜像。在项目根目录创建.dockerignore文件,添加以下内容:
.vs/ bin/ obj/
这样Docker构建时会自动跳过这些目录,避免文件被占用的问题。
2. 关闭Visual Studio后重试
就算加了.dockerignore,如果Visual Studio还在运行,后台进程可能仍在锁定文件。完全关闭VS(包括任务管理器里的残留进程),再重新执行docker build。
3. 检查Docker文件共享权限
打开Docker Desktop设置 → 资源 → 文件共享,确保已勾选项目所在的C盘,点击应用并重启Docker,避免权限不足导致的文件访问问题。
4. 清理Docker缓存
缓存的镜像层可能引发异常,执行以下命令清理:
docker system prune -af
清理完成后重新构建镜像。
5. 升级Docker Desktop
你当前用的4.3版本较老,存在不少Windows容器的兼容性bug。升级到最新稳定版,能修复大部分文件锁定相关的问题。
内容的提问来源于stack exchange,提问作者SovietFrontier
相关产品推荐
相关产品推荐

