efcore
efcore copied to clipboard
Migration bundles directly to a docker container
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.
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
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:
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
We discussed this, but it is out-of-scope for the EF tool to handle all of this.
Reopening to possibly re-discuss - the dotnet SDK now allows publishing directly to docker (blog post), so this may be quite easy.
We've also had recent discussions about migration application best practices, and one thing that came up was deploying migrations to your production environment as containerized applications (/cc @davidfowl).
Thank you guys! I've made the experience that most of the developers, I know are overstrained with building migration containers. Because of that I do have to consult many of my colleagues.Best regards,Bjego Sent from Android PhoneAm 31.10.2023 09:58 schrieb Shay Rojansky @.***>: Reopening to possibly re-discuss - the dotnet SDK now allows publishing directly to docker (blog post), so this may be quite easy. We've also had recent discussions about migration application best practices, and one thing that came up was deploying migrations to your production environment as containerized applications (/cc @davidfowl).
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>
@roji do you need some more input, ideas or information which I may can contribute?
@Bjego I think we're pretty clear on this; prioritizing and implementing it is another matter, of course (depending on competing priorities).
I current build the container bundle migration with other Dockerfile in my repo. One for application and other for bundle migration.
In kubernetes, we running bundle migration with job before container application with argocd presync hooks.
I would like to set Env variable "ConnectionStrings__Conexao" in my terminal and running validation migration with command
dotnet ef migrations list
I Current we use Task bash in the pipeline with "sed" and "replace_tokens" for replace connection string in the file "appsettings.json.