Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration bundles directly to a docker container #31999

Open
Tracked by #19587
Bjego opened this issue Oct 9, 2023 · 7 comments
Open
Tracked by #19587

Migration bundles directly to a docker container #31999

Bjego opened this issue Oct 9, 2023 · 7 comments

Comments

@Bjego
Copy link

Bjego commented Oct 9, 2023

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

@ajcvickers
Copy link
Member

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

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. and removed type-enhancement labels Oct 16, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2023
@roji
Copy link
Member

roji commented Oct 31, 2023

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

@roji roji reopened this Oct 31, 2023
@Bjego
Copy link
Author

Bjego commented Oct 31, 2023 via email

@ajcvickers ajcvickers added this to the Backlog milestone Nov 2, 2023
@ajcvickers ajcvickers added type-enhancement area-migrations area-tools and removed closed-no-further-action The issue is closed and no further action is planned. labels Nov 2, 2023
@Bjego
Copy link
Author

Bjego commented Nov 3, 2023

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

@roji
Copy link
Member

roji commented Nov 3, 2023

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

@Vinaum8
Copy link

Vinaum8 commented Apr 25, 2024

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.

@Vinaum8
Copy link

Vinaum8 commented Apr 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants