无法将具有EFCore Mysql提供程序的Docker中的Asp.net Core WebApi连接到主机服务器上的非Docker Mysql
我有一个Asp.Net Core 3.1.3 WebApi,提供程序为EfCore 3.1.3和Pomelo.EntityFrameworkCore.MySql 此WebApi是在Docker中构建和部署的 主机服务器是Debian 10,它配置了UFW作为防火墙 我在主机服务器上安装了一个Mysql服务器 我将Mysql的绑定地址设置为0.0.0.0 /etc/mysql/mysql.conf.d/mysqld.cnf无法将具有EFCore Mysql提供程序的Docker中的Asp.net Core WebApi连接到主机服务器上的非Docker Mysql,docker,asp.net-core-3.1,ef-core-3.1,debian-buster,pomelo-entityframeworkcore-mysql,Docker,Asp.net Core 3.1,Ef Core 3.1,Debian Buster,Pomelo Entityframeworkcore Mysql,我有一个Asp.Net Core 3.1.3 WebApi,提供程序为EfCore 3.1.3和Pomelo.EntityFrameworkCore.MySql 此WebApi是在Docker中构建和部署的 主机服务器是Debian 10,它配置了UFW作为防火墙 我在主机服务器上安装了一个Mysql服务器 我将Mysql的绑定地址设置为0.0.0.0 /etc/mysql/mysql.conf.d/mysqld.cnf [mysqld] pid-file = /var/run/m
[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
我在UFW中创建了一个规则,只允许本地主机和docker0
网桥ip地址
在docker compose配置中,我添加了主机服务器的ip地址,但与MySql的连接失败
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 '<db_name>' on server '<host_ip_address>'.
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.
---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
我生成迁移脚本并将其部署在服务器上,因为Pomelo在调用dbContext.Database.Migrate()时将数据库名称设置为空字符串
在dockerfile中,我公开了端口80和443
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
...
我在docker compose文件中将数据库参数作为环境变量发送
我做了一些数据播种,从Identity添加用户和角色
var roleManager = provider.GetRequiredService<RoleManager<RoleModel>>();
roleManager.CreateAsync(new RoleModel(roleName)).GetAwaiter().GetResult();
...
var rolemager=provider.GetRequiredService();
CreateAsync(新RoleModel(roleName)).GetAwaiter().GetResult();
...
我错过了什么?除了在docker compose中使用主机网络模式部署docker容器之外,我没有找到任何其他解决方案 docker-compose.yml
services:
<service_name>:
image: <image_name>
network_mode: "host" #important here
ports:
- "5000:5000"
- "5001:5001"
environment:
ASPNETCORE_Kestrel__Certificates__Default__Password: "${ASPNETCORE_KESTREL_CERTIFICATE_PASSWORD}"
ASPNETCORE_Kestrel__Certificates__Default__Path: "${ASPNETCORE_KESTREL_CERTIFICATE_PATH}"
ASPNETCORE_ENVIRONMENT: Production
"Database:Server": "${Database_Server}"
"Database:Version": "${Database_Version}"
"Database:Name": "${Database_Name}"
"Database:User": "${Database_User}"
"Database:Password": "${Database_Password}"
build:
context: ./Src
dockerfile: Dockerfile-build
volumes:
- ${CERTIFICATE_PATH}:/https
- ${LOGS_PATH}:/app/logs
服务:
:
图片:
网络模式:“主机”#在这里很重要
端口:
- "5000:5000"
- "5001:5001"
环境:
ASPNETCORE_Kestrel_证书_默认密码:${ASPNETCORE_Kestrel_证书_密码}”
ASPNETCORE\u Kestrel\u证书\u默认路径:${ASPNETCORE\u Kestrel\u证书\u路径}
ASPNETCORE_环境:生产
数据库:服务器“${Database\u Server}”
“数据库:版本”:“${Database_Version}”
数据库:名称“${Database\u Name}”
数据库:用户“${Database\u User}”
数据库:密码“${Database\u Password}”
建造:
上下文:./Src
dockerfile:dockerfile生成
卷数:
-${CERTIFICATE\u PATH}:/https
-${LOGS\u PATH}:/app/LOGS
在这种情况下,我只需要使用主机服务器的ip地址来连接到MySql(以及随附的所有Ufw、MySql配置)
var roleManager = provider.GetRequiredService<RoleManager<RoleModel>>();
roleManager.CreateAsync(new RoleModel(roleName)).GetAwaiter().GetResult();
...
services:
<service_name>:
image: <image_name>
network_mode: "host" #important here
ports:
- "5000:5000"
- "5001:5001"
environment:
ASPNETCORE_Kestrel__Certificates__Default__Password: "${ASPNETCORE_KESTREL_CERTIFICATE_PASSWORD}"
ASPNETCORE_Kestrel__Certificates__Default__Path: "${ASPNETCORE_KESTREL_CERTIFICATE_PATH}"
ASPNETCORE_ENVIRONMENT: Production
"Database:Server": "${Database_Server}"
"Database:Version": "${Database_Version}"
"Database:Name": "${Database_Name}"
"Database:User": "${Database_User}"
"Database:Password": "${Database_Password}"
build:
context: ./Src
dockerfile: Dockerfile-build
volumes:
- ${CERTIFICATE_PATH}:/https
- ${LOGS_PATH}:/app/logs