我有一个ASP.NETCore6WebAPI在Docker中运行,我想连接到本地SQL Server数据库(未停靠!),但我无法这样做。通过IP连接到远程服务器上的数据库工作正常,但使用连接字符串
var dbHost = "COM-3195\\IMRANMSSQL";
var dbName = "CustomersDb";
var dbPassword = "prg@321654";
var connectionString = $"Data Source={dbHost};Initial Catalog={dbName};User Id=sa;Password={dbPassword}";
我的停靠文件:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["CustomerWebService/CustomerWebService.csproj", "CustomerWebService/"]
RUN dotnet restore "CustomerWebService/CustomerWebService.csproj"
COPY . .
WORKDIR "/src/CustomerWebService"
RUN dotnet build "CustomerWebService.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CustomerWebService.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CustomerWebService.dll"]
我的码头工
services:
customerwebservice:
image: ${DOCKER_REGISTRY-}customerwebservice
build:
context: .
dockerfile: CustomerWebService/Dockerfile
extra_hosts:
- "COM-3195\\IMRANMSSQL:<IP>"
我的应用程序没有连接到数据库,日志中显示了这一点:
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://[::]:443
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app/
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 6.0.5 initialized 'CustomerDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.5' with options: None
fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
An error occurred using the connection to database 'master' on server '<IP>,1433'.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)
如果我在没有Docker的情况下运行,如果我在composer.yml
文件中确实喜欢这样,它工作正常
version: '3.4'
networks:
backend:
services:
customerdb:
container_name: customer-db
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=prg@321654
networks:
- backend
ports:
- 8001:1433
customerwebapi:
container_name: cutomer-api
image: ${DOCKER_REGISTRY-}customerwebapi
build:
context: .
dockerfile: CustomerWebAPI/Dockerfile
networks:
- backend
ports:
- 8002:80
environment:
- DB_HOST= customerdb
- DB_NAME= CustomersDb
- DB_SA_PASSWORD=prg@321654
它工作正常,但它运行在本地,8001在SQL Server中,但我希望我的本地SQL Server添加。
请帮帮忙,我刚开始学习码头管理。
请帮助我从我的Docker映像连接到SQL Server数据库(未停靠)
2条答案
按热度按时间nzrxty8p1#
要访问主机上的某些内容,可以在
extra_hosts
部分中为host-gameway
添加一个名称,通常将其命名为host.docker.internal
,因此我们在这里也将这样做。然后,您需要将数据库主机名更改为
host.docker.internal
,这样您应该能够连接。我对如何在程序中指定数据库主机名感到有点困惑。在C#代码片段中,您显示的是一个硬编码的值。但是在容器中运行数据库的docker-compose文件中,您设置了一个环境变量。要做的“好”事情是通过环境变量来处理它,因此我将在这里显示该解决方案
iibxawm42#
您有以下两种解决方案:
docker inspect <container_id>
、docker network ls
和docker network inspect <network_id>
等命令接收“网关”的IP地址docker network inspect ...
在IPAM > Config > Gateway
处找到eth0
。我不是100%肯定,但您也应该能够使用至少从您的主网络接口的IP地址。作为一般信息,即使容器和DB运行在同一主机上,也不能使用
localhost
,因为这将被解析到不同的系统(一次用于容器,一次用于主机本身)