Skip to content

SQL Migration does not run in docker #2952

Closed

Description

@dabagab

Describe the bug

I have followed some articles to be able to use the aspnet core application with sql server.
Unfortunately the migration command does not run.

Steps to reproduce the issue

Provide steps for us to reproduce the issue

  1. Creating a new Asp.net Core 2.2 Web (MVC) project from the official template in the Visual Studio 2019.
  2. Adding Docker-Compose support to the project.
version: '3.4'

services:
  api:
    image: ${DOCKER_REGISTRY-}api
    build:
      context: .
      dockerfile: Api/Dockerfile
    depends_on:
      - db

  db:
    image: "mcr.microsoft.com/mssql/server"
    environment:
      SA_PASSWORD: "Your_password123"
      ACCEPT_EULA: "Y"

The Docker file was generated by Visual Studio. I inserted some line to run the entrypoint.sh

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Api/Api.csproj", "Api/"]
RUN dotnet restore "Api/Api.csproj"
COPY . .
WORKDIR "/src/Api"
RUN dotnet build "Api.csproj" -c Release -o /app

FROM build AS publish
# Inserted lines
COPY ./entrypoint.sh ./app/
RUN chmod +x ./app/entrypoint.sh
CMD /bin/bash ./app/entrypoint.sh
RUN dotnet publish "Api.csproj" -c Release -o /app

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

Entrypoint.sh is from the tutorial of docker page.

#!/bin/bash

set -e
run_cmd="dotnet run --server.urls http://*:80"

until dotnet ef database update; do
>&2 echo "SQL Server is starting up"
sleep 1
done

>&2 echo "SQL Server is up - executing command"
exec $run_cmd

Expected behavior

When I run the Docker-Compose project in VS 2019 then I expect the migration has run. But this is not happen.

Additional information

I did not find any official step-by-step tutorial.

Activity

  1. natemcmaster commented on May 29, 2019

    @natemcmaster
    Contributor

    Migrations do not automatically run when your application begins. Running dotnet App.dll will only start the web server. A separate action is required to run migrations.

    @ajcvickers - do you have documentation or recommendations on how to achieve migrations inside a docker workflow?

  2. changed the title [-]SQL Migration does not run.[/-] [+]SQL Migration does not run in docker[/+] on May 30, 2019
  3. alexk8 commented on May 31, 2019

    @alexk8

    short answer might be

            public static async Task Main(string[] args)
            {
                var host = CreateWebHostBuilder(args).Build();
                using (var scope = host.Services.CreateScope())
                {
                    scope.ServiceProvider.GetRequiredService<AppDbContext>().Database.Migrate();
                }
                host.Run();
    	}

    But I wouldn't say that it's recommended

  4. ajcvickers commented on Jun 3, 2019

    @ajcvickers
    Contributor

    Notes from triage:

    • We don't currently have any guidance on how to do this. Putting this on the backlog to investigate what people should do here.
    • We should also consider if the web publishing support could handle this better--we need to follow up with the web tools team on this.
    • For now, a few ways to handle this are:
      • Run dotnet-ef commands in the container. This will work only if the SDK is installed into the container. @dabagab This might be what you are running into.
      • Generate a SQL script from Migrations and then use it to create/migrate the database from within the container. This will require a tool in the container that can run the SQL.
      • Use either dotnet-ef or a SQL script from outside the container using a connection to the database from outside the container.
  5. ajcvickers commented on Sep 17, 2020

    @ajcvickers
    Contributor

    Assigning to @bricelam to review; may be not action here.

  6. syedhassaanahmed commented on Nov 17, 2020

    @syedhassaanahmed

    @ajcvickers Any updated guidance on how to run migrations in Docker?

  7. ajcvickers commented on Nov 19, 2020

    @ajcvickers
    Contributor

    @syedhassaanahmed No; this issue is tracking adding this guidance to the docs.

  8. transferred this issue fromdotnet/efcoreon Dec 16, 2020
  9. added this to the 6.0.0 milestone on Dec 16, 2020
  10. davelowndes commented on Feb 15, 2021

    @davelowndes

    I have resolved via option 3:

    • expose the database port to your host in docker-compose.yml
    • implement IDesignTimeDbContextFactory
    public class ChassisDbContextFactory : IDesignTimeDbContextFactory<ChassisDbContext>
    {
        public ChassisDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ChassisDbContext>();
            optionsBuilder.UseNpgsql();
    
            return new ChassisDbContext(optionsBuilder.Options);
        }
    }
    
    • cd to the project with the DbContext and provide the connection string via new --connection parameter (EF Core 5.0)
    dotnet ef database update --connection "Host=localhost;Database=chassis;Username=postgres;Password=password"
    

    My run-time connection is set up using normal appsettings.json:

    services.AddDbContext<ChassisDbContext>(options =>
        options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
    );
    
  11. modified the milestones: 6.0.0, 7.0.0 on Nov 3, 2021
  12. CyberoxiDevops commented on Mar 10, 2022

    @CyberoxiDevops

    Hi, any updates ?

  13. ddatdt12 commented on Sep 3, 2022

    @ddatdt12

    I have the same problem. Any solution for that ?

  14. removed their assignment
    on Jul 8, 2023
  15. lolalilox commented on Apr 29, 2024

    @lolalilox

    I have the same problem. Any solution for that ?

  16. lolalilox commented on Apr 29, 2024

    @lolalilox

    So I just resorted back into doing Update-Database manually after doing a docker compose, I guess this could somewhat be automated with a script of some kind. Thanks either way though.

  17. added a commit that references this issue on Aug 11, 2026
    bb42937
  18. modified the milestones: 7.0.0, 11.0.0 on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions