【发布时间】:2021-10-24 01:10:43
【问题描述】:
我有一个 ASP.Net Core Web API 应用程序,我在其中访问一个文件,该文件位于与“Controllers”文件夹相同级别的“Data”文件夹中。当我在没有 docker 的情况下运行应用程序时,它工作得非常好。但是当我在 docker 上运行应用程序时,它只会抛出一个错误。
在docker logs <containerid> 的帮助下检查日志后,我发现了以下内容:
Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMB6BABGSIF7", Request id "0HMB6BABGSIF7:00000001": An unhandled exception was thrown by the application.
System.IO.FileNotFoundException: Could not find file '/app/.\Data\petronas.xlsx'.
File name: '/app/.\Data\petronas.xlsx'
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
以下是我的 Docker 文件:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY ./ ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
WORKDIR /app
COPY --from=build-env /app/out .
COPY Data ./Data
ENV ASPNETCORE_URLS=http://+:5000
ENTRYPOINT ["dotnet", "Validator.dll"]
访问文件的代码:
var lineitems = ConvertExcelToDataTable(@".\Data\petronas.xlsx");
【问题讨论】:
-
你是如何访问你的文件代码的?
-
@aamd 我已经添加了代码。
-
似乎您在 Windows 上的开发和
\Data\petronas.xlsx需要在容器上更改为/Data/petronas.xlsx...我现在不记得 dotnet 容器是基于什么的,但是,如果我没记错的话,它是Linux。此外,当您离开中间容器时,您可以使用mcr.microsoft.com/dotnet/aspnet:3.1映像而不是 sdk...它针对运行 aspnet 核心应用程序进行了优化,并且应该更小。
标签: c# docker asp.net-core dockerfile