Search code examples
dockerdocker-compose

.Net 8 Container Unable to connect to MySql Container


I have a simple POC I'm working on with .net 8 and mysql. I've docker-ized them using a Dockerfile and docker compose. If I start the mysql container and debug the .net8 api locally in vscode it works without issue. When I attempt to start the .net8 api container however I get this following error outputted to the api container console.

System.AggregateException: An error occurred while writing to logger(s). (Unable to connect to any of the specified MySQL hosts.)
2024-03-10 11:43:03        ---> MySqlConnector.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts

I've read tons of articles about how I need to use the mysql container name in my connection string and create the bridge network in my docker compose. I've done that but I must be missing something super simple.

This is my connection string in .net 8 api appsettings.json

"WeatherDb": "Server=mysqldb;port=3306;Database=WeatherDb;User Id=webuser;Password=password;"

This is my docker compose file

# https://docs.docker.com/compose/
volumes:
  datafiles:

services:
  #DataBase Service
  mysqldb:
    #Pull the latest mysql image
    image: mysql:8.3.0
    #Map port 3306 on the mysql container to port 3306 in the host
    ports:
      - "3307:3306"
    #Specify where the persisted Data should be stored
    volumes:
      - datafiles:/Users/user/SourceControl/mysql_datafiles
    restart: always
    #Specify Environment Variables for mysql
    environment: 
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: webuser
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: WeatherDb
    networks:
      - my-network

  # service images to be be created
  api: 
    build: src/
    ports:
      # list of port mappings
      - 80:80
    environment:
      # list of environment variables within the container
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:80;
    depends_on: 
      - mysqldb
    networks:
      - my-network

networks:
  my-network:
     driver: bridge

Solution

  • Turns out nothing was wrong with the way I set up the local docker compose network and connection string. It was a timing issue. When doing a fresh docker compose up the mysql container wasn't running in time. I fixed this by adding a health check condition to it the mysql container.

    # https://docs.docker.com/compose/
    volumes:
      datafiles:
        driver: local
    
    services:
      #DataBase Service
      mysqldb:
        #Pull the latest mysql image
        image: mysql:8.3.0
        #Map port 3306 on the mysql container to port 3306 in the host
        ports:
          - "3307:3306"
        #Specify where the persisted Data should be stored
        volumes:
          - /Users/myuser/SourceControl/mysql_datafiles:/var/lib/mysql
        restart: always
        #Specify Environment Variables for mysql
        environment: 
          MYSQL_ROOT_PASSWORD: password
          MYSQL_USER: webuser
          MYSQL_PASSWORD: password
          MYSQL_DATABASE: WeatherDb
        healthcheck:
          test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost", "-uroot", "-ppass"]
          interval: 5s
          timeout: 5s
          retries: 20
        networks:
          - my-network
    
      # service images to be be created
      api: 
        build: src/
        ports:
          # list of port mappings
          - 80:80
          #- 8081:8081
        environment:
          # list of environment variables within the container
          - ASPNETCORE_ENVIRONMENT=Docker
          - ASPNETCORE_URLS=http://+:80;
        depends_on: 
          mysqldb:
            condition: service_healthy
        networks:
          - my-network
    
    networks:
      my-network:
         driver: bridge