Starts in:
2 DAYS
0 HR
2 MIN
3 SEC
Starts in:
2 D
0 H
2 M
3 S
Open Closed

Local Docker deployment for single tiered application with sql server #5260


User avatar
0
Sraman created
  • ABP Framework version: v7.2.2
  • UI type: MVC /
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Facing issue while doing a local docker deployment for single tier app. Finding difficult to migrate database. It would be great if you could suggest or provide some documentation something similar to tiered app(https://support.abp.io/QA/Questions/4729/Local-Docker-Deployment)

  • Exception message and stack trace:

ClientConnectionId:01dffe83-aabb-4392-af59-ef5aa1e279a2 mrs-web | Error Number:4060,State:1,Class:11 mrs-web | [17:50:49 ERR] ---------- Exception Data ---------- mrs-web | HelpLink.ProdName = Microsoft SQL Server mrs-web | HelpLink.EvtSrc = MSSQLServer mrs-web | HelpLink.EvtID = 4060 mrs-web | HelpLink.BaseHelpUrl = https://go.microsoft.com/fwlink mrs-web | HelpLink.LinkId = 20476 mrs-web | mrs-web | [17:50:51 ERR] An error occurred using the connection to database 'MRS' on server 'mrs-db'. mrs-web | [17:50:51 ERR] Cannot open database "MRS" requested by the login. The login failed. mrs-web | Login failed for user 'sa'. mrs-web | Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "MRS" requested by the login. The login failed. mrs-web | Login failed for user 'sa'. mrs-web | at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) mrs-web | at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) mrs-web | at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) mrs-web | at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) mrs-web | at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) mrs-web | at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry, SqlConnectionOverrides overrides) mrs-web | at Microsoft.Data.SqlClient.SqlConnection.InternalOpenAsync(CancellationToken cancellationToken) mrs-web | --- End of stack trace from previous location --- mrs-web | at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken) mrs-web | at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken) mrs-web | at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected) mrs-web | at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken) mrs-web | at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync(CancellationToken cancellationToken) mrs-web | at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider1.CreateDbContextWithTransactionAsync(IUnitOfWork unitOfWork) mrs-web | at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider1.CreateDbContextAsync(IUnitOfWork unitOfWork) mrs-web | at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider1.CreateDbContextAsync(IUnitOfWork unitOfWork, String connectionStringName, String connectionString) mrs-web | at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider1.GetDbContextAsync() mrs-web | at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository2.GetDbSetAsync() mrs-web | at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository2.GetListAsync(Boolean includeDetails, CancellationToken cancellationToken) mrs-web | at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) mrs-web | at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() mrs-web | at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) mrs-web | at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) mrs-web | at Volo.Abp.FeatureManagement.StaticFeatureSaver.UpdateChangedFeatureGroupsAsync(IEnumerable`1 featureGroupRecords) mrs-web | at Volo.Abp.FeatureManagement.StaticFeatureSaver.SaveAsync() mrs-web | at Volo.Abp.FeatureManagement.StaticFeatureSaver.SaveAsync() mrs-web | at Volo.Abp.FeatureManagement.StaticFeatureSaver.SaveAsync() mrs-web | at Volo.Abp.FeatureManagement.StaticFeatureSaver.SaveAsync() mrs-web | at Volo.Abp.FeatureManagement.AbpFeatureManagementDomainModule.<>c__DisplayClass8_0.<

  • Steps to reproduce the issue:"

7 Answer(s)
  • User Avatar
    0
    rafael.gonzales created

    Can you share your dockerfile and docker-compose?

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Please check https://docs.abp.io/en/commercial/latest/startup-templates/application/deployment-docker-compose?UI=MVC&DB=EF&Tiered=No

  • User Avatar
    0
    Sraman created

    Please check https://docs.abp.io/en/commercial/latest/startup-templates/application/deployment-docker-compose?UI=MVC&DB=EF&Tiered=No

    Can you suggest some production deployment ideas using above code?

  • User Avatar
    0
    Sraman created

    Can you share your dockerfile and docker-compose?

    I'm able to do build and run container on my docker desktop with the below docker files using build-images-locally.ps1and run-docker.ps1.

    But how should I use my docker file to deploy on production to docker container registry. What changes should I make or any suggestions?

      FROM mcr.microsoft.com/dotnet/aspnet:7.0
      COPY bin/Release/net7.0/publish/ app/
      WORKDIR /app
      ENTRYPOINT ["dotnet", "Acme.Books.dll"]
    
    version: '3.9'
    
    services:
      mrs-web:
        image: pentapack/mrs:latest
        container_name: mrs-web
        build:
          context: ../../
          dockerfile: Pentapack.MRS/Dockerfile.local
        environment:
          - ASPNETCORE_URLS=https://+:443;http://+:80;
          - Kestrel__Certificates__Default__Path=/root/certificate/localhost.pfx
          - Kestrel__Certificates__Default__Password=91f91912-5ab0-49df-8166-23377efaf3cc
          - ConnectionStrings__Default=Server=mrs-db,1433;Database=MRS;User=sa;Password=Aqwx1379!;TrustServerCertificate=true;
        ports:
          - "443:443"
        depends_on:
          mrs-db:
            condition: service_healthy
        restart: on-failure  
        volumes:
          - ./certs:/root/certificate
        networks:
          - mrs-network
    
      mrs-db-migrator:
        image: pentapack/mrs:latest
        container_name: mrs-db-migrator
        build:
          context: ../../
          dockerfile: Pentapack.MRS/Dockerfile.local
        environment:
          - ConnectionStrings__Default=Server=mrs-db,1433;Database=MRS;User=sa;Password=Aqwx1379!;TrustServerCertificate=true;
        command:
          - --migrate-database
        depends_on:
          mrs-db:
            condition: service_healthy
        networks:
          - mrs-network 
    
      mrs-db:
        container_name: mrs-db
        image: 'mcr.microsoft.com/mssql/server:latest'
        ports:
          - "1433:1433"
        volumes:
          - mrs-data:/data/db
        environment:
          - ACCEPT_EULA=Y
          - SA_PASSWORD=Aqwx1379!
        networks:
          - mrs-network
        healthcheck:
          test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-Usa", "-PAqwx1379!", "-Q", "select 1"]
          interval: 10s
          timeout: 10s
          retries: 10
    volumes:
      mrs-data:
        name: mrs-data
    networks:
      mrs-network:
        name: mrs-network
        driver: bridge
    
  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Please read https://docs.abp.io/en/commercial/latest/startup-templates/application/deployment-docker-compose?UI=MVC&DB=EF&Tiered=No

    There is a detailed explanation about what to override, how to override and why to override.

  • User Avatar
    0
    Sraman created

    Please read https://docs.abp.io/en/commercial/latest/startup-templates/application/deployment-docker-compose?UI=MVC&DB=EF&Tiered=No

    There is a detailed explanation about what to override, how to override and why to override.

    I'm confused with Db Migrator, no tiered application don't have DbMigrator.dll. How can we migrate database without that?

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Okay sorry, I thought you are using non-tiered app template but you are basically using no-layered app template.

    DbMigration in no-layered application template is done by passing the argument --migrate-database.

    The initial docker-compose file generated with the template (mrs-db-migrator service in your application) basically runs as db-migrator and exits if you examine the Program.cs:

    if (IsMigrateDatabase(args))
    {
        await app.Services.GetRequiredService<MyAppDbMigrationService>().MigrateAsync();
        return 0;
    }
    

    So the docker-compose file runs the same docker image as 2 different services:

    • One of them is the application itself: mrs-web
    • Other one is the db-migrator: mrs-db-migrator

    So, when you run the docker-compose file, it will look like:

    Which indicates the Exited (0) one is being the db-migrator. You can perfectly use it as it is just fine.

    Another way

    You can also drop using the db-migrator and simply migrate your database whenever you run your application.

    Simply override the OnPostApplicationInitializationAsync and add:

    public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        await context.ServiceProvider
            .GetRequiredService<MyAppDbMigrationService>()
            .MigrateAsync();
    }
    

    This will run the db-migration after your application is initialized instead of manually triggering it with an argument. You can also drop the mrs-db-migrator service in the docker-compose file if you choose to do so.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06