我有两个图像-数据库和api。
当我运行一个DB镜像和一个api项目(不是镜像)。一切正常,数据来了。
但是当我尝试运行两个图像时,我有这个-
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)
db容器汇编代码-docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=super_duper_password' -e 'MSSQL_PID=Express' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019
Dockerfile webapi -
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["DockerSample.csproj", "./"]
RUN dotnet restore "DockerSample.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DockerSample.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerSample.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerSample.dll"]
ConfigureServices中到DB的连接字符串-
services.AddDbContext<AppDbContext>(opt =>
opt.UseSqlServer("Data Source=localhost,1433;Initial Catalog=TestDockerDb;User ID=sa;Password=Pa55sw0rd2022"));
你试过127.0.0.1吗?(代替localhost)