title: Cannot connect Asp.net Core WebApi in Docker with EFCore Mysql provider to non Docker Mysql on host server
body: <p>I have an Asp.Net Core 3.1.3 WebApi with EfCore 3.1.3 and Pomelo.EntityFrameworkCore.MySql as provider
  This WebApi is built and deployed in Docker.</p>
  
  <p>The host server is Debian 10, it is configured with UFW as firewall.</p>
  
  <p>I have a Mysql Server installed on the host server.</p>
  
  <p>I set the bind address to 0.0.0.0 for Mysql</p>
  
  <p>/etc/mysql/mysql.conf.d/mysqld.cnf</p>
  
  <pre><code>[mysqld]
  pid-file        = /var/run/mysqld/mysqld.pid
  socket          = /var/run/mysqld/mysqld.sock
  datadir         = /var/lib/mysql
  log-error       = /var/log/mysql/error.log
  bind-address    = 0.0.0.0
  </code></pre>
  
  <p>I created a rule in UFW to allow only localhost and <code>docker0</code> bridge ip address
  <a href="https://i.stack.imgur.com/oSrNU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oSrNU.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/BHIt4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BHIt4.png" alt="enter image description here"></a></p>
  
  <p>In my docker-compose config, I added the ip address of my host server, but the connection to MySql fails</p>
  
  <pre><code>info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
  Entity Framework Core 3.1.3 initialized 'ApplicationContext' using provider 'Pomelo.EntityFrameworkCore.MySql' with options: using lazy-loading proxies ServerVersion 8.0.19 MySql 
  fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
  An error occurred using the connection to database '&lt;db_name&gt;' on server '&lt;host_ip_address&gt;'.
  info: Microsoft.EntityFrameworkCore.Infrastructure[10404]
  A transient exception has been encountered during execution and the operation will be retried after 0ms.
  MySql.Data.MySqlClient.MySqlException (0x80004005): Connect Timeout expired.
  ---&gt; System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'System.Net.Sockets.Socket'.
  </code></pre>
  
  <p>In Startup.cs, here is the config for EfCore:</p>
  
  <pre><code>services.AddDbContext&lt;ApplicationContext&gt;(opts =&gt;
        {
          opts
          .UseLazyLoadingProxies()
          .UseMySql(connectionString, mySqlOptions =&gt;
                      mySqlOptions
                        .ServerVersion(new Version(dbOptions.Version), ServerType.MySql)
                        .CharSet(CharSet.Utf8Mb4)
                        .EnableRetryOnFailure(3))
          .EnableDetailedErrors();
        });
  </code></pre>
  
  <p>To build the connection string, I use MySql.Data.MySqlClient.MySqlConnectionStringBuilder:</p>
  
  <pre><code>var builder = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder
        {
          Server = dbOptions.Server,
          Database = dbOptions.Name,
          UserID = dbOptions.User,
          Password = dbOptions.Password,       
          PersistSecurityInfo = true
        };
  
        return builder.ToString();
  </code></pre>
  
  <p>I generate the migration script and deployed it on the server, since Pomelo sets Database Name to Empty String when calling dbContext.Database.Migrate();</p>
  
  <p>In the dockerfile, I expose ports 80 and 443.</p>
  
  <pre><code>FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
  WORKDIR /app
  EXPOSE 80
  EXPOSE 443
  ...
  </code></pre>
  
  <p>I sent Database params as Environment variables in the docker-compose file.</p>
  
  <p>I did some data seeding to add users and roles from Identity.</p>
  
  <pre><code>var roleManager = provider.GetRequiredService&lt;RoleManager&lt;RoleModel&gt;&gt;();
  roleManager.CreateAsync(new RoleModel(roleName)).GetAwaiter().GetResult();
  ...
  </code></pre>
  
  <p>What did I miss?</p>