Skip to content

Migration bundles directly to a docker container #31999

Description

@Bjego

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

Activity

  1. ajcvickers commented on Oct 12, 2023

    @ajcvickers
    Contributor

    We discussed this, but it is out-of-scope for the EF tool to handle all of this.

  2. roji commented on Oct 31, 2023

    @roji
    Member

    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).

  3. reopened this on Oct 31, 2023
  4. Bjego commented on Oct 31, 2023

    @Bjego
    Author
  5. added this to the Backlog milestone on Nov 2, 2023
  6. Bjego commented on Nov 3, 2023

    @Bjego
    Author

    @roji do you need some more input, ideas or information which I may can contribute?

  7. roji commented on Nov 3, 2023

    @roji
    Member

    @Bjego I think we're pretty clear on this; prioritizing and implementing it is another matter, of course (depending on competing priorities).

  8. Vinaum8 commented on Apr 25, 2024

    @Vinaum8

    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.

  9. Vinaum8 commented on Apr 25, 2024

    @Vinaum8

    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.

  10. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions