.net - How to build and run a .NET Core Console app in Docker which contains multiple libs?
问题描述
I'm struggling to find a good example of doing this, so hoping for some help.
My project/file structure is like this:
\src
\src\BulkImporter
\src\BulkImporter\BulkImporter.csproj
\src\lib1
\src\lib1\lib1.csproj
\src\lib2
\src\lib2\lib2.csproj
So, i'm trying to dockerize BulkImporter
. To build the app, it needs lib1
and lib2
.
I'm trying to do a multi-stage docker build, so a build + runtime image.
Here's my current Dockerfile:
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app
# copy everything and build the project
COPY . ./
RUN dotnet restore src/BulkImporter/*.csproj
RUN dotnet publish src/BulkImporter/*.csproj -c Release -o out
# build runtime image
FROM microsoft/dotnet:2.1-runtime
WORKDIR /app
COPY --from=build-env /app/src/BulkImporter/out ./
ENTRYPOINT ["dotnet", "BulkImporter.dll"]
But, it doesn't seem right. I don't know if i need to 'build everything'. Also, i'm got a csv
file in my app, that is erroring when i run the app, as it's looking in an app/Data/mycsv.csv
, when normally it exists in /Data/mycsv.csv
.
So i think there's some problems with my Dockerfile.
Can someone point me to a good example of how to do this, or tell me what's wrong with my Dockerfile?
Thanks
EDIT
I've updated my Dockerfile to be closely based on the dotnet docker example.
It's building fine, but still facing the CSV issue.
Here's my updated dockerfile:
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY src/BulkImporter/BulkImporter.csproj ./BulkImporter/
COPY src/Domain/Domain.csproj ./Domain/
COPY src/ElasticSearch.SearchService/ElasticSearch.SearchService.csproj ./ElasticSearch.SearchService/
WORKDIR /app/BulkImporter
RUN dotnet restore
# copy and publish app and libraries
WORKDIR /app/
COPY src/BulkImporter/. ./BulkImporter/
COPY src/Domain/. ./Domain/
COPY src/ElasticSearch.SearchService/. ./ElasticSearch.SearchService/
WORKDIR /app/BulkImporter
RUN dotnet publish -c Release -o out
# build final release runtime image
FROM microsoft/dotnet:2.1-runtime AS runtime
WORKDIR /app
COPY --from=build /app/BulkImporter/out ./
ENTRYPOINT ["dotnet", "BulkImporter.dll"]
I'm building the image from the folder above the src
folder, with the following command:
docker build -t bulkimporter . -f .\src\BulkImporter\Dockerfile
The error i get when running is:
Could not find file '/app/FakeData\locations.csv'.
Yet when i do a dotnet publish
from the BulkImporter
folder on my PC, the bin\Release\netcoreapp2.1\publish\FakeData
folder has the locations.csv
file there.
The code that loads the CSV is this:
var suburbLines = (await File.ReadAllLinesAsync("FakeData\\locations.csv")).Skip(1);
(runs fine outside Docker).
Can anyone spot the problem?
解决方案
推荐阅读
- server - Wildfly 最新稳定版
- ruby-on-rails - AMP 访问控制允许凭据错误
- selenium-webdriver - Selenium 获取返回 NULL 的文本
- javascript - 根据字符串的数量范围对对象数组进行排序
- java - 具有不同键的 Spring Boot 应用程序属性
- pact - PACT 与 Spring Cloud 合约测试
- mysql - 如果所有子列都使用触发器mysql具有相同的数据,如何更新父列
- javascript - 画布背景隐藏了我添加的矩形
- php - 如何从 Laravel 中的输入日期获取文本中的星期几
- android - 无法使用类“java.util.HashSet”强制转换对象“[]”