azuredockerdocker-composeazure-appservice

Azure AppService and SQL Server in Docker compose can't connect


I have a multi-container app, with front app, API and a database. Everything is containerized using docker-compose and everything is working fine with Docker Desktop.

I'm trying to exec docker-compose.yml in an Azure AppService with multi-container option, but for some reason I get this error:

2024-08-30T07:58:34.316196930Z warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
2024-08-30T07:58:34.316274431Z       Failed to determine the https port for redirect.
2024-08-30T07:58:52.250150859Z fail: Microsoft.AspNetCore.Server.Kestrel[13]
2024-08-30T07:58:52.250200459Z       Connection id "0HN68P65RFJRL", Request id "0HN68P65RFJRL:00000001": An unhandled exception was thrown by the application.
2024-08-30T07:58:52.250208059Z       Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)
2024-08-30T07:58:52.250214759Z        ---> System.Net.Sockets.SocketException (00000005, 0xFFFDFFFF): Name does not resolve
2024-08-30T07:58:52.250220259Z          at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses, AddressFamily addressFamily, Nullable`1 startingTimestamp)
2024-08-30T07:58:52.250225659Z          at System.Net.Dns.GetHostAddresses(String hostNameOrAddress, AddressFamily family)
2024-08-30T07:58:52.250230660Z          at Microsoft.Data.SqlClient.SNI.SNICommon.GetDnsIpAddresses(String serverName)
2024-08-30T07:58:52.250235460Z          at Microsoft.Data.SqlClient.SNI.SNITCPHandle.Connect(String serverName, Int32 port, TimeSpan timeout, Boolean isInfiniteTimeout, SqlConnectionIPAddressPreference ipPreference, String cachedFQDN, SQLDNSInfo& pendingDNSInfo)
2024-08-30T07:58:52.250240560Z          at Microsoft.Data.SqlClient.SNI.SNITCPHandle..ctor(String serverName, Int32 port, Int64 timerExpire, Boolean parallel, SqlConnectionIPAddressPreference ipPreference, String cachedFQDN, SQLDNSInfo& pendingDNSInfo, Boolean tlsFirst, String hostNameInCertificate, String serverCertificateFilename)
2024-08-30T07:58:52.250245760Z          at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
2024-08-30T07:58:52.250250960Z          at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
2024-08-30T07:58:52.250255760Z          at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnectionString connectionOptions, Boolean withFailover)
2024-08-30T07:58:52.250261860Z          at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
2024-08-30T07:58:52.250280060Z          at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
2024-08-30T07:58:52.250285460Z          at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
2024-08-30T07:58:52.250290460Z          at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
2024-08-30T07:58:52.250295860Z          at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
2024-08-30T07:58:52.250300860Z          at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
2024-08-30T07:58:52.250305760Z          at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
2024-08-30T07:58:52.250321760Z          at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
2024-08-30T07:58:52.250326961Z          at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
2024-08-30T07:58:52.250332361Z          at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
2024-08-30T07:58:52.250337361Z          at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
2024-08-30T07:58:52.250342261Z          at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
2024-08-30T07:58:52.250347061Z          at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
2024-08-30T07:58:52.250351861Z          at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
2024-08-30T07:58:52.250362961Z          at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
2024-08-30T07:58:52.250367761Z          at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)
2024-08-30T07:58:52.250372361Z          at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
2024-08-30T07:58:52.250376961Z          at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
2024-08-30T07:58:52.250381461Z          at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
2024-08-30T07:58:52.250386561Z          at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)
2024-08-30T07:58:52.250391861Z          at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
2024-08-30T07:58:52.250396961Z          at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
2024-08-30T07:58:52.250401561Z          at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
2024-08-30T07:58:52.250406161Z          at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
2024-08-30T07:58:52.250410661Z          at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
2024-08-30T07:58:52.250415162Z          at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
2024-08-30T07:58:52.250419562Z          at Api.Data.MyContext..ctor(DbContextOptions`1 options) in /home/my-app-user/app/src/Data/MyContext.cs:line 10
2024-08-30T07:58:52.250424062Z          at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2024-08-30T07:58:52.250428662Z          at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
2024-08-30T07:58:52.250433162Z          at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
2024-08-30T07:58:52.250437762Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-08-30T07:58:52.250442462Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
2024-08-30T07:58:52.250447262Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-08-30T07:58:52.250451962Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-08-30T07:58:52.250460662Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
2024-08-30T07:58:52.250465862Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-08-30T07:58:52.250470562Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
2024-08-30T07:58:52.250475362Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-08-30T07:58:52.250479962Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-08-30T07:58:52.250484562Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
2024-08-30T07:58:52.250489162Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
2024-08-30T07:58:52.250494262Z          at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
2024-08-30T07:58:52.250498862Z          at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
2024-08-30T07:58:52.250503462Z          at lambda_method9(Closure, IServiceProvider, Object[])
2024-08-30T07:58:52.250507963Z          at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
2024-08-30T07:58:52.250512863Z          at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
2024-08-30T07:58:52.250517763Z          at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
2024-08-30T07:58:52.250522263Z       --- End of stack trace from previous location ---
2024-08-30T07:58:52.250526963Z          at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
2024-08-30T07:58:52.250531863Z          at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
2024-08-30T07:58:52.250536663Z          at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
2024-08-30T07:58:52.250541363Z          at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
2024-08-30T07:58:52.250550363Z          at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
2024-08-30T07:58:52.250555163Z          at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
2024-08-30T07:58:52.250559763Z          at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
2024-08-30T07:58:52.250564263Z          at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
2024-08-30T07:58:52.250568963Z       ClientConnectionId:00000000-0000-0000-0000-000000000000

As you can see, my docker-compose.yml is correct:

services:
    app:
        image: xxx/app:1.0.0
        ports:
            - 8080:8080
        environment:
            - Api=http://api:8080/
            - ASPNETCORE_ENVIRONMENT=Production
        networks:
            - network-1
        depends_on:
            api:
                condition: service_healthy
    api:
        image: xxx/api:1.0.0
        # ports:
        #     - 50000:8080
        environment:
            - ConnectionStrings__DefaultConnection=Server=db,1433;Database=Employees;User Id=sa;Password=myStrongP@ssword;TrustServerCertificate=True;
            - ASPNETCORE_ENVIRONMENT=Production
        networks:
            - network-1
            - network-2
        depends_on:
            db:
                condition: service_healthy        
        healthcheck:
            test: ["CMD", "curl", "-f", "http://api:8080/healthcheck"]
            interval: 20s
            retries: 15
            start_period: 20s
            timeout: 20s    
    db:
        environment:
          ACCEPT_EULA: Y
          SA_PASSWORD: myStrongP@ssword
        image: mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04
        #ports:
        #     - 1433:1433
        volumes:
            - db-volume:/var/opt/mssql/data/
        user: root
        networks:
            - network-2
        healthcheck:
            test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P myStrongP@ssword -C -b -Q \"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'\""]
            interval: 20s
            retries: 15
            start_period: 20s
            timeout: 20s    
volumes:
    db-volume:
networks:
  network-1:
    driver: bridge
  network-2:
    driver: bridge

Any ideas? I have searched a lot of Stack Overflow questions but none of them solve my issue.


Solution

  • solved, you have to upgrade the appservice plan because the basic one was not enough for sql server, it needs more ram and/or cpu, once this is done it starts without problems