Zorgis
Zorgis

Reputation: 41

Docker with .NetCore, should I use the SDK or Runtime image for micro-services?

I'm pretty new to docker and I was wondering.

When you are working with many micro-services (50+). Is it still relevant to use the runtime image versus the SDK image?

In order to use the runtime image, I need to do a self-contained publish, that is around 100MO bigger.

With 50 micro-services, it's 5GO of data, to have self-contained app.

Is it worth it to take the Runtime image in this case?

Upvotes: 3

Views: 3923

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239440

The runtime image contains the runtime, so you don't need to publish self-contained. The SDK is only required if you need to build. The runtime has everything necessary to, you know, run. If you're publishing self-contained, you'd only need the OS, so your base image would just be alpine or something, not ASP.NET Core at all (because ASP.NET Core would be contained in the app).

Then, Docker has staged builds. As such, the typical way to do this is to build and publish all in the image, just in different stages. The final image is based on the last stage, so that is where you'd reference the runtime image. For example:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["MyProject/MyProject.csproj", "MyProject/"]
RUN dotnet restore "MyProject/MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.dll"]

Each FROM line indicates a new stage. The only thing that survives is the final stage, where all that's being done is the published files are copied in and the app is run, giving you an optimally sized image. However, using the staged build, all the logic necessary to build and publish your app is contained in the image.

Upvotes: 10

Related Questions