dotnet / sdk-container-builds

Libraries and build tooling to create container images from .NET projects using MSBuild
https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
MIT License
176 stars 30 forks source link

Publish EFBundle Initcontainer #511

Open Bjego opened 8 months ago

Bjego commented 8 months ago

Hi, we are using initcontainers in kubernetes on-prem and in aks. The teams have to build quite long dockerfiles to build their sourcecode and to build an ef core initcontainer (based on dotnet ef bundle).

Issue / Problem

e.g.

ARG VERSION=0.1.0
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 8080
ENV ASPNETCORE_URLS=http://+:8080
COPY SSL/some_company_root.crt /usr/local/share/ca-certificates
RUN update-ca-certificates
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
ARG VERSION
ARG configuration=Release
WORKDIR /src
COPY ["src/AksPoc/AksPoc.csproj", "src/AksPoc/"]
RUN dotnet restore "src/AksPoc/AksPoc.csproj"
COPY . .
WORKDIR "/src/src/AksPoc"
RUN dotnet build "AksPoc.csproj" -c $configuration -o /app/build -p:Version=${VERSION}

# Build the migrationbundle here
FROM build as migrationbuilder
ENV PATH $PATH:/root/.dotnet/tools
RUN dotnet tool install --global dotnet-ef
RUN mkdir /migrations
RUN dotnet ef migrations bundle --self-contained -r linux-x64 -o /migrations/migration
RUN cp ./initentrypoint.sh /migrations/initentrypoint.sh

# Stage to execute the migrationbundle
FROM mcr.microsoft.com/dotnet/aspnet:7.0 as initcontainer
ENV CONNECTIONSTRING="User Id=AZUREPOC;Password=start123;Data Source=db:1521/FREEPDB1;"
COPY --from=migrationbuilder /migrations /migrations
COPY --from=migrationbuilder /src/src/AksPoc/appsettings.json /migrations/appsettings.json
RUN chmod 755 /migrations/migration
RUN chmod 755 /migrations/initentrypoint.sh
WORKDIR /migrations
ENTRYPOINT [ "./initentrypoint.sh" ]

FROM build AS publish
ARG configuration=Release
RUN dotnet publish "AksPoc.csproj" -c $configuration -o /app/publish /p:UseAppHost=false

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

Initscript:

#!/bin/bash
./migration --connection "$CONNECTIONSTRING"

Suggestion

It would be nice and handy, if I could just run something like:

dotnet efbundle --container --tag initcontainer:latest

To build a complete initcontainer for ef core just with one line of bash scripting. Then the developers wouldn't need to understand and write complex dockerfiles.

The efbundle container should, build a complete initcontainer (including appsettings.json) and should be configurable via an environment variable.

Thanks in advance, Bjego