首页 > 解决方案 > 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?

标签: .netdocker.net-coredockerfile

解决方案


推荐阅读