简体   繁体   中英

Docker mysql container Unable to connect to any of the specified MySQL hosts

I'm working on a asp .net core 5.0 web application which i'am running on docker using docker-compose. I have 2 services my asp.net core application and MySQL when i try to run the EF command add-migration i get this error:

 Unable to connect to any of the specified MySQL hosts.

This is the code for my Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
EXPOSE 8008
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["chatroom.csproj", "."]
RUN dotnet restore "./chatroom.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "chatroom.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "chatroom.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
EXPOSE 5000
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "chatroom.dll"]

And this is the code for my docker-compose.yml file

version: '3.0'
services:
  db:
    image: mysql:latest
    container_name: mysqldb
    environment:
      MYSQL_ROOT_PASSWORD: root      
      MYSQL_DATABASE: livredb
      MYSQL_USER: wassef
      MYSQL_PASSWORD: hw
    restart: always
    ports:
      - "3306:3306"

  web:
    image: chatroom
    build: 
      context: .
      dockerfile: Dockerfile
    ports:
      - "8008:5000"
    depends_on:
      - db

And this is my ConnectionString

"ConnectionStrings": {
    "mydb": "server=db;port=3306;userid=wassef;password=hw;database=livredb;"
  },

by the way i can see my database being created when i access my MySQL client

Turns out that The host network cannot access the docker network. The docker network is for container-to-container communication only. Therefore i changed my ConnectionString server to server=localhost

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM