title: C# and Docker - Can't connect to containerized MySQL server from containerized .NET Core 3.1 Web API
body: <p>For reference, I tried ideas from the following links to no avail:</p>
  <p><a href="https://stackoverflow.com/questions/57851823/docker-compose-unable-to-connect-to-any-of-the-specified-mysql-hosts">Docker-Compose Unable to connect to any of the specified MySQL hosts</a>
  <a href="https://stackoverflow.com/questions/52676741/connect-to-mysql-container-from-web-api-net-core-container-how-to-get-ip-addre">Connect to MySQL container from Web Api .Net Core Container? How to get Ip Address?</a></p>
  <p>I have three containerized apps: mysql@8.0 &quot;exposed&quot; -- for lack of a better term -- behind port 9999; a .NET Core 3.1 WebAPI; and a containerized Angular app. The Angular app can successfully make calls to the WebAPI behind port 5001 just fine. The issue is the web API establishing a connection with the MySQL container, it seems.</p>
  <p><strong>All apps are deployed as containers on my local, development workstation.</strong> The web API and MySQL db are being deployed with a single <code>docker-compose.yml</code> which I've shared below. I built a simple image for the front-end application and deployed it from the Docker command line.</p>
  <p>Here is my <code>docker-compose.yml</code> for the API and DB:</p>
  <pre><code>version: &quot;3.3&quot;
  
  services: # list of services composing your application
    db: # the service hosting your MySQL instance
      image: mysql:8.0 # the image and tag docker will pull from docker hub
      volumes: # this section allows you to configure persistence within multiple restarts
        - db_data:/var/lib/mysql
      restart: always # if the db crash somehow, restart it
      ports:
        - &quot;9999:3306&quot;
      environment: # env variables, you usually set this to override existing ones
        MYSQL_ROOT_PASSWORD: *****
      networks:
        - soar-network
    soar-api: # you application service
      build: ./ # this tells docker-compose to not pull from docker hub, but to build from the Dockerfile it will find in ./
      restart: always
      ports:
        - &quot;5001:80&quot;
      networks:
        - soar-network
      # set a dependency between your service and the database:
      # this means that your application will not run if the db service is not running,
      # but it doesn't assure you that the dabase will be ready to accept incoming connection
      # (so your application could crash untill the db initializes itself)
      depends_on:
        - db
  
  volumes:
    db_data: # this tells docker-compose to save your data in a generic docker volume. You can see existing volumes typing 'docker volume ls'
  
  networks:
    soar-network:
      driver: bridge
  </code></pre>
  <p>The Web API code is using the following connection string for the <code>DbContext</code> I'm using in my code:</p>
  <pre><code>  &quot;ConnectionStrings&quot;: {
      &quot;DefaultConnection&quot;: &quot;server=localhost;port=9999;uid=root;pwd=*****;database=SoarDb&quot;
    }
  </code></pre>
  <p>Note the <code>port</code> in the connection string matches what I'm mapping in the <code>docker-compose</code>. I <em>have</em> tried using <code>3306</code> and <code>33060</code>, as well, to no avail.</p>
  <p>I've also tried using <code>127.0.0.1</code> as the <code>server</code> value with no luck.</p>
  <p>The error being logged in the web API container is as follows:</p>
  <pre><code>soar-api_1  | fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
  soar-api_1  |       An error occurred using the connection to database 'SoarDb' on server 'localhost'.
  soar-api_1  | fail: Microsoft.EntityFrameworkCore.Query[10100]
  soar-api_1  |       An exception occurred while iterating over the results of a query for context type 'DataAccess.SoarDataContext'.
  soar-api_1  |       MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
  soar-api_1  |          at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 442
  </code></pre>
  <p>The other strange thing is: I can add migrations and update the database with <code>dotnet-ef add migration</code> and <code>dotnet-ef database update</code> to this particular containerized db.</p>
  <p>Any insight is greatly appreciated. I have tried many different permutations of settings and tweaks with no luck and don't know what I'm misunderstanding.</p>