title: ASP.NET Core 6 cannot resolve database name when running in docker container (in WSL2)
body: <p>I am dockerizing an ASP.NET Core 6 application step by step as follows:</p>
  <ul>
  <li>I have set up WSL2 in Windows 10 and installed Racher Desktop. This allows running docker commands as if run in the WSL2 guest</li>
  <li>I have run a Postgresql container and it is accessible to both host (Windows) pgAdmin and for the ASP.NET Core when run from Visual Studio:</li>
  </ul>
  <pre><code>docker run -d -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres -p 5433:5432 postgres
  </code></pre>
  <p>(port 5432 was hijacked by some service before installing the Postgres and I had to use another port).</p>
  <p>When running from Visual Studio, the application uses the following connection string:</p>
  <pre><code>Host=localhost:5433; Database=foo-next; Username=postgres; Password=postgres; Timeout=300; CommandTimeout=300
  </code></pre>
  <p>I have created a Dockerfile and a docker image for the application. When running I receive the following error:</p>
  <pre><code>Unhandled exception. System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name does not resolve
     at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses, AddressFamily addressFamily, ValueStopwatch stopwatch)
     at System.Net.Dns.&lt;&gt;c.&lt;GetHostEntryOrAddressesCoreAsync&gt;b__33_0(Object s, ValueStopwatch stopwatch)
     at System.Net.Dns.&lt;&gt;c__DisplayClass39_0`1.&lt;RunAsync&gt;b__0(Task &lt;p0&gt;, Object &lt;p1&gt;)
     at System.Threading.Tasks.ContinuationResultTaskFromTask`1.InnerInvoke()
     at System.Threading.Tasks.Task.&lt;&gt;c.&lt;.cctor&gt;b__272_0(Object obj)
     at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
  --- End of stack trace from previous location ---
     at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&amp; currentTaskSlot, Thread threadPoolThread)
  --- End of stack trace from previous location ---
     at Npgsql.TaskExtensions.ExecuteWithTimeout[TResult](Func`2 func, NpgsqlTimeout timeout, CancellationToken cancellationToken)
     at Npgsql.TaskExtensions.WithCancellation[T](Task`1 task, CancellationToken cancellationToken)
     at Npgsql.TaskExtensions.WithTimeout[T](Task`1 task, NpgsqlTimeout timeout)
     at Npgsql.Internal.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
     at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
     at Npgsql.Internal.NpgsqlConnector.&lt;Open&gt;g__OpenCore|191_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
     at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
     at Npgsql.UnpooledConnectorSource.Get(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
     at Npgsql.NpgsqlConnection.&lt;Open&gt;g__OpenAsync|45_0(Boolean async, CancellationToken cancellationToken)
     at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
     at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
     at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
     at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists(Boolean async, CancellationToken cancellationToken)
     at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists(Boolean async, CancellationToken cancellationToken)
     at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.ExistsAsync(CancellationToken cancellationToken)
     at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
  </code></pre>
  <p>I interpret this as the application not being able to resolve <code>postgres</code> hostname. I have checked if Postgres containers and the .NET Core one run in the same network and they seem they do, but some attributes are missing for my app network metadata:</p>
  <p>Wehn running in the container, the application uses the following connection string:</p>
  <pre><code>Host=postgres:5433; Database=foo-next-docker; Username=postgres; Password=postgres; Timeout=300; CommandTimeout=300
  </code></pre>
  <pre><code>docker inspect postgres -f &quot;{{json .NetworkSettings.Networks }}&quot;
  {&quot;bridge&quot;:{&quot;IPAMConfig&quot;:null,&quot;Links&quot;:null,&quot;Aliases&quot;:null,&quot;NetworkID&quot;:&quot;343ece236719b94d53df0afe1ab0d405a19102002f67ea60a1914016d4e4f96b&quot;,&quot;EndpointID&quot;:&quot;9c3740b300caaf50f86731f00530dce62b386b56ccc2fe3da96c4cb677dcb42d&quot;,&quot;Gateway&quot;:&quot;172.17.0.1&quot;,&quot;IPAddress&quot;:&quot;172.17.0.3&quot;,&quot;IPPrefixLen&quot;:16,&quot;IPv6Gateway&quot;:&quot;&quot;,&quot;GlobalIPv6Address&quot;:&quot;&quot;,&quot;GlobalIPv6PrefixLen&quot;:0,&quot;MacAddress&quot;:&quot;02:42:ac:11:00:03&quot;,&quot;DriverOpts&quot;:null}}
  
  docker inspect foonextapp -f &quot;{{json .NetworkSettings.Networks }}&quot;
  {&quot;bridge&quot;:{&quot;IPAMConfig&quot;:null,&quot;Links&quot;:null,&quot;Aliases&quot;:null,&quot;NetworkID&quot;:&quot;343ece236719b94d53df0afe1ab0d405a19102002f67ea60a1914016d4e4f96b&quot;,&quot;EndpointID&quot;:&quot;&quot;,&quot;Gateway&quot;:&quot;&quot;,&quot;IPAddress&quot;:&quot;&quot;,&quot;IPPrefixLen&quot;:0,&quot;IPv6Gateway&quot;:&quot;&quot;,&quot;GlobalIPv6Address&quot;:&quot;&quot;,&quot;GlobalIPv6PrefixLen&quot;:0,&quot;MacAddress&quot;:&quot;&quot;,&quot;DriverOpts&quot;:null}}
  </code></pre>
  <p>The same result I get when explicitly running the container attached to the <code>bridge</code> network:</p>
  <pre><code>docker run -p 8080:80 --name fooapp fooapp --network=bridge
  </code></pre>
  <p>Not sure if it provides more information, but the whole docker network list is the following:</p>
  <pre><code>docker network ls
  NETWORK ID     NAME       DRIVER    SCOPE
  343ece236719   bridge     bridge    local
  df0c354db7cf   host       host      local
  6efb481fc02b   minikube   bridge    local
  23bdb658109a   none       null      local
  </code></pre>
  <p>Any suggestions about what to try next in order to make the app running in local container resolve the database container name?</p>
  <p>Note: all commands were run in Windows.</p>