[英]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.我正在开发一个
asp .net core 5.0 web application
,我正在使用 docker 使用 ZAEF93945E84057AE7 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:当我尝试运行 EF 命令
add-migration
时,我的asp.net core
应用程序和MySQL
有 2 项服务,我收到此错误:
Unable to connect to any of the specified MySQL hosts.
This is the code for my Dockerfile
这是我的
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这是我的
docker-compose.yml
文件的代码
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
这是我的
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顺便说一句,当我访问我的
MySQL
客户端时,我可以看到我的数据库正在创建
Turns out that The host network cannot access the docker network.原来主机网络无法访问 docker 网络。 The docker network is for container-to-container communication only.
docker 网络仅用于容器到容器的通信。 Therefore i changed my ConnectionString server to
server=localhost
因此我将我的 ConnectionString 服务器更改为
server=localhost
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.