title: Postgres connection issue EF Core / docker-compose up
body: <p>I have a docker compose file, I have a postgres instance being brough up, and a web api project also, however I am getting the following error:</p>
  
  <pre><code>api_1       |       Application startup exception
  api_1       | Npgsql.NpgsqlException (0x80004005): Exception while connecting
  api_1       |  ---&gt; System.TimeoutException: Timeout during connection attempt
  api_1       |    at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
  api_1       |    at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
  api_1       |    at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
  api_1       |    at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
  api_1       |    at Npgsql.NpgsqlConnection.&lt;&gt;c__DisplayClass32_0.&lt;&lt;Open&gt;g__OpenLong|0&gt;d.MoveNext()
  api_1       | --- End of stack trace from previous location where exception was thrown ---
  api_1       |    at Npgsql.NpgsqlConnection.Open()
  api_1       |    at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
  api_1       |    at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
  api_1       |    at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
  api_1       |    at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
  api_1       |    at my.Claims.Engine.Api.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, myDbContext ctx) in /src/my.Claims.Engine.Api/Startup.cs:line 56
  api_1       |    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
  api_1       |    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  api_1       |    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
  api_1       |    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.&lt;&gt;c__DisplayClass4_0.&lt;Build&gt;b__0(IApplicationBuilder builder)
  api_1       |    at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.&lt;&gt;c__DisplayClass13_0.&lt;UseStartup&gt;b__2(IApplicationBuilder app)
  api_1       |    at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.&lt;&gt;c__DisplayClass0_0.&lt;Configure&gt;g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
  api_1       |    at Microsoft.AspNetCore.HostFilteringStartupFilter.&lt;&gt;c__DisplayClass0_0.&lt;Configure&gt;b__0(IApplicationBuilder app)
  api_1       |    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
  api_1       | Unhandled exception. Npgsql.NpgsqlException (0x80004005): Exception while connecting
  api_1       |  ---&gt; System.TimeoutException: Timeout during connection attempt
  api_1       |    at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
  api_1       |    at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
  api_1       |    at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
  api_1       |    at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
  api_1       |    at Npgsql.NpgsqlConnection.&lt;&gt;c__DisplayClass32_0.&lt;&lt;Open&gt;g__OpenLong|0&gt;d.MoveNext()
  api_1       | --- End of stack trace from previous location where exception was thrown ---
  api_1       |    at Npgsql.NpgsqlConnection.Open()
  api_1       |    at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
  api_1       |    at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
  api_1       |    at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
  api_1       |    at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
  api_1       |    at my.Claims.Engine.Api.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, myDbContext ctx) in /src/my.Claims.Engine.Api/Startup.cs:line 56
  api_1       |    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
  api_1       |    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  api_1       |    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
  api_1       |    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.&lt;&gt;c__DisplayClass4_0.&lt;Build&gt;b__0(IApplicationBuilder builder)
  api_1       |    at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.&lt;&gt;c__DisplayClass13_0.&lt;UseStartup&gt;b__2(IApplicationBuilder app)
  api_1       |    at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.&lt;&gt;c__DisplayClass0_0.&lt;Configure&gt;g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
  api_1       |    at Microsoft.AspNetCore.HostFilteringStartupFilter.&lt;&gt;c__DisplayClass0_0.&lt;Configure&gt;b__0(IApplicationBuilder app)
  api_1       |    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
  api_1       |    at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
  api_1       |    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
  api_1       |    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
  api_1       |    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
  api_1       |    at my.Claims.Engine.Api.Program.Main(String[] args) in /src/my.Claims.Engine.Api/Program.cs:line 17
  my-claims_api_1 exited with code 139
  </code></pre>
  
  <p>Here is my docker-compose:   </p>
  
  <pre><code>
  services: 
      db:
          image: postgres
          restart: always
          ports:
              - '5432:5432'
          environment:
              POSTGRES_USER: root
              POSTGRES_PASSWORD: password
      eventbus:
          image: 'rabbitmq'
          ports:
              - '5672:5672'
              - '15672:15672'
      portal:
          image: ${DOCKER_REGISTRY-}portal
          build:
              context: ./portal
              dockerfile: Docker/Dockerfile
          ports: 
              - "3000:80"
      api:
          image: ${DOCKER_REGISTRY-}my_claims_engine_api
          build:
              context: ./my.Claims.Engine
              dockerfile: my.Claims.Engine.Api/Dockerfile
          ports: 
              - "4000:80"    
  
  </code></pre>
  
  <p>Here is my connection string:   </p>
  
  <pre><code>"myDbContext": "Server=localhost;Port=5432;Database=mydatabase;User Id=root;Password=password;"
  </code></pre>
  
  <p>If I run the postgres as it's own image, and connect via it's local address I am able to connect, my migrations run, no problems whatsoever.</p>
  
  <p>The only time I get the error is via the docker-compose.</p>
  
  <p>Additionally, I am unable to connect to the rabbitmq instance via localhost:15672, any suggestions on that would be great too...</p>
  
  <p>Any ideas where the problem is?</p>