I need to know how to refactor the Dockerfile with Dotnet application

Viewed 230

I need to refactor the Dockerfile of a Dotnet application which looks similar to the below.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80   
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["DockerExplained/DockerExplained.csproj", "DockerExplained/"]
RUN dotnet restore "DockerExplained/DockerExplained.csproj"
COPY . .
WORKDIR "/src/DockerExplained"
RUN dotnet build "DockerExplained.csproj" -c Release -o /app/build  
FROM build AS publish
RUN dotnet publish "DockerExplained.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerExplained.dll"]

where as i need to use ubuntu20.04 as base image with image layers and i was told not to mention the busterslim. Can anyone guide how to proceed on this.

========================================

Here is the fresh requirement for the file... requirement is to install everything inside the raw image of Ubuntu 20.04 and i am not able to package the image layers start FROM ubuntu 20.04 I get seven different images but i need everything to run inside the ubuntu 20.04..

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["DockerExplained/DockerExplained.csproj", "DockerExplained/"]
RUN dotnet restore "DockerExplained/DockerExplained.csproj"
COPY . .
WORKDIR "/src/DockerExplained"
RUN dotnet publish "DockerExplained.csproj" -c Release -o /app/publish

FROM ubuntu:20.04

#Runtime-deps

FROM debian:buster-slim

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates \
        \
# .NET Core dependencies
        libc6 \
        libgcc1 \
        libgssapi-krb5-2 \
        libicu63 \
        libssl1.1 \
        libstdc++6 \
        zlib1g \
    && rm -rf /var/lib/apt/lists/*

# Configure web servers to bind to port 80 when present
ENV ASPNETCORE_URLS=http://+:80 \
    # Enable detection of running in a container
    DOTNET_RUNNING_IN_CONTAINER=true
        
#Runtime

from mcr.microsoft.com/dotnet/core/runtime-deps:3.1-buster-slim

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl \
    && rm -rf /var/lib/apt/lists/*

# Install .NET Core
RUN dotnet_version=3.1.3 \
    && curl -SL --output dotnet.tar.gz https://dotnetcli.azureedge.net/dotnet/Runtime/$dotnet_version/dotnet-runtime-$dotnet_version-linux-x64.tar.gz \
    && dotnet_sha512='e3f6f9b81bc3828b60f7da5a5c341373dc17f971f1de3f2714adcca180a630a60d4b681166fe78434d8b2ce023d2d08eff4f1935ec664130b7f856fa8e1cac2b' \
    && echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
    && mkdir -p /usr/share/dotnet \
    && tar -ozxf dotnet.tar.gz -C /usr/share/dotnet \
    && rm dotnet.tar.gz \
    && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

#ASPdotnet.3.1-busterslim

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim

# Install ASP.NET Core
RUN aspnetcore_version=3.1.3 \
    && curl -SL --output aspnetcore.tar.gz https://dotnetcli.azureedge.net/dotnet/aspnetcore/Runtime/$aspnetcore_version/aspnetcore-runtime-$aspnetcore_version-linux-x64.tar.gz \
    && aspnetcore_sha512='ced0b7c9faaf250fbee2c850e8941d47a17da06fa8e576d57063fa4bbc6c8435de6ab488b683d4a692cd622e24d9592f11608a1b358074167cf0b76024815611' \
    && echo "$aspnetcore_sha512  aspnetcore.tar.gz" | sha512sum -c - \
    && tar -ozxf aspnetcore.tar.gz -C /usr/share/dotnet ./shared/Microsoft.AspNetCore.App \
    && rm aspnetcore.tar.gz


WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "DockerExplained.dll"]``
1 Answers

The tag list for the aspnet images can be found here along with the OS versions in each image.

If you want to use Ubuntu 20.04, you need to change your base image to

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-focal AS base

If you want the default Debian 10, you can just remove the 'buster-slim' part of the image tag so you get

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base

As you can see in the list, 3.1 and 3.1-buster-slim are the same image so if you use 3.1 you'll get the same image as you use now.

Edit: Dockerfile explanation, build / runtime

Here you create a temporary image called base which is based on the aspnet runtime image

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80   

Then you create a temporary image called build based on the SDK image where you build your solution. Note that this has nothing to do with the image above.

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

Then you create a new temporary image called publish based on the temporary build image

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

And finally you create the final image where you build upon your 'base' image from the top. Here you copy the published app from the 'publish' image.

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

As you can see, the final image is made from the first 3 lines + the last 4 lines in your Dockerfile. The middle part is where you build your solution and those bits of the Dockerfile aren't included in the final image.

Here's how you could structure it in a more simple way with only 2 steps: build and runtime/final.

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["DockerExplained/DockerExplained.csproj", "DockerExplained/"]
RUN dotnet restore "DockerExplained/DockerExplained.csproj"
COPY . .
WORKDIR "/src/DockerExplained"
RUN dotnet publish "DockerExplained.csproj" -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS final
WORKDIR /app
EXPOSE 80   
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "DockerExplained.dll"]
Related