I am new to docker, trying to connect .NET Core 3.0 Application with SQL database using docker-compose.
I have defined the connectionstring in docker-compose.yml.
I have tried with 127.0.0.1 with and without giving port(1433).
This is the exception i am getting:
webapi_1 | fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
webapi_1 | An error occurred using the connection to database 'InstaDb' on server '127.0.0.1,1433'.
webapi_1 | fail: Microsoft.EntityFrameworkCore.Query[10100]
webapi_1 | An exception occurred while iterating over the results of a query for context type 'Insta.Infra.Data.Context.InstaDbContext'.
webapi_1 | Microsoft.Data.SqlClient.SqlException (0x80131904): 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)
docker-compose.yml
version: "3.9"
services:
webapi:
build: .
ports:
- "5000:80"
depends_on:
- db
environment:
ConnectionStrings:InstaDBConnection: "Server=127.0.0.1,1433;Database=InstaDb;User=sa;Password=#Admin123;MultipleActiveResultSets=True;"
db:
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "#Admin123"
ACCEPT_EULA: "Y"
Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["Insta.Mvc/Insta.Mvc.csproj", "Insta.Mvc/"]
COPY ["Insta.Infra.Data/Insta.Infra.Data.csproj", "Insta.Infra.Data/"]
COPY ["Insta.Application/Insta.Application.csproj", "Insta.Application/"]
COPY ["Insta.Domain/Insta.Domain.csproj", "Insta.Domain/"]
COPY ["Insta.Common.Utils/Insta.Common.Utils.csproj", "Insta.Common.Utils/"]
COPY ["Insta.Infra.IoC/Insta.Infra.IoC.csproj", "Insta.Infra.IoC/"]
RUN dotnet restore "Insta.Mvc/Insta.Mvc.csproj"
COPY . .
WORKDIR "/src/Insta.Mvc"
RUN dotnet build "Insta.Mvc.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Insta.Mvc.csproj" -c Release -o /app/publish
WORKDIR /src
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh
WORKDIR "/src/Insta.Mvc"
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Insta.Mvc.dll"]
You can create networks with docker composes, where you can access other containers from a container via their domain names as well, like the following:
version: "3.9"
services:
webapi:
build: .
ports:
- "5000:80"
depends_on:
- db
environment:
ConnectionStrings:InstaDBConnection: "Server=db,1433;Database=InstaDb;User=sa;Password=#Admin123;MultipleActiveResultSets=True;"
networks:
- your-network
db:
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "#Admin123"
ACCEPT_EULA: "Y"
networks:
- your-network
networks:
your-network:
name: your-network
Notice that in the connection string you can use the name of the service as a host name.
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.