dotnet / dotnet/efcore

Migration bundles directly to a docker container

Open
#31,999 7 comments 0 reactions 0 assignees View on GitHub
area-migrations area-tools customer-reported needs-design
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Hi guys,
we are using initcontainers in kubernetes (on prem and on aks). Every team has to create pretty long dockerfiles to generate a migration bundle and let it end in a specific target.
This is not too easy for every developer

# Problem

- Executing bundles as initcontainer requires quite a lot of steps, which are not obvious for many developers.
- The dockerfile needs one or two extra stages
- The docker build needs a target parameter to get just the initcontainer
- Write a little initscript to use env variables in the entrypoint to set the connectionstring

e.g.
```Dockerfile
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:
```Bash
#!/bin/bash
./migration --connection "$CONNECTIONSTRING"
```

# Suggestion
As you see this is quite complex.
Because of that I'd love to have a command, which build a complete dockercontainer with just one command like:
```bash
dotnet efbundle --container --tag initcontainer:latest
```
This command should do all the efbundling and copying of appsettings.json into a docker container. That container should be configurable via environment variables like CONNECTIONSTRING.

Thanks in advance,
Bjego

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.