1

I have following dockerfile which runs my tests:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
COPY ./ ./
RUN dotnet restore ./mysln.sln -r linux-x64

RUN dotnet build ./tests/mytests/mytests.csproj
ENTRYPOINT ["dotnet", "test", "./tests/mytests/mytests.csproj", "--no-build"]

I'd like to split build and test step, so I don't have entire codebase with obj/bin files in my image (which is executed later, and can be executed multiple times, so there is no reason to build it each time).

For instance:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
COPY ./ ./
RUN dotnet restore ./mysln.sln -r linux-x64

RUN dotnet build ./tests/mytests/mytests.csproj

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS tests

COPY --from=build ./tests/mytests/ ./tests/mytests/

ENTRYPOINT ["dotnet", "test", "./tests/mytests/mytests.csproj", "--no-build"]

However this does not work for some reason, dotnet test does nothing (no error reported, no std out) - just quits, even though it's running in target image.

Shadow
  • 2,089
  • 2
  • 23
  • 45
  • does `"dotnet", "test", "-v=detailed", ...` bring an output? – anion Dec 06 '22 at 20:01
  • Tip: I would not copy to `./` because that contains the **entire filesystem** (bin boot dev etc ...). Rather, do something like `WORKDIR /app` or `WORKDIR /source` first. Also copy just your sln and project files after that, and do your _restore_. Then, finally, `COPY . .` to bring in the code. That way changing the code doesn't cause _restore_ to happen again. e.g.: https://github.com/dotnet/dotnet-docker/blob/main/samples/dotnetapp/Dockerfile – Wyck Dec 06 '22 at 20:29
  • @Wyck that was just oversimplification, I'll update question – Shadow Dec 07 '22 at 08:46
  • @anion it prints info that some tasks are run, but no conclussion. – Shadow Dec 07 '22 at 08:47
  • For what it's worth, I was able to use your Dockerfile verbatim, (but supplying my own source files, of course, since you have not provided any), and it worked fine for me. – Wyck Dec 07 '22 at 13:40

1 Answers1

1

Your test stage is based on the sdk:6.0 tag, not taking advantage of the copying and build you've already done in your build stage. I would recommend the following:

Instead of: FROM mcr.microsoft.com/dotnet/sdk:6.0 AS tests

Use this: FROM build AS tests

Edit: Also should remove this line: COPY --from=build ./tests/mytests/ ./tests/mytests/

This pattern is documented at https://github.com/dotnet/dotnet-docker/tree/36e083bb836a5f9a3444ef7ad4459e5c580a7984/samples/complexapp#running-tests-as-an-opt-in-stage

Matt Thalman
  • 3,540
  • 17
  • 26