may be somebody know what the problem appears. I try to start api with db conteiners but always my Fluent Migrator failed with db connection
I created the docker file:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /src
COPY *.sln "./"
COPY ["Desk.API/Desk.API.csproj", "Desk.API/"]
COPY ["Desk.Migrations/Desk.Migrations.csproj", "Desk.Migrations/"]
RUN dotnet restore "Desk.API/Desk.API.csproj"
COPY . .
WORKDIR "/src/Desk.API"
RUN dotnet publish "Desk.API.csproj" -c Release -o /app --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app .
EXPOSE 80
ENTRYPOINT ["dotnet", "Desk.API.dll"]
and the docker-compose file:
version: '3.9'
services:
sqldata:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
- SA_PASSWORD=vV5r9tn0M4@
- ACCEPT_EULA=Y
ports:
- "1450:1433"
api:
build:
context: .
dockerfile: Desk.API/docker/Dockerfile
ports:
- "8080:80"
depends_on:
- sqldata
connection string
"ConnectionStrings": {
"Default": "Data Source=sqldata;Initial Catalog=Desk;Persist Security Info=True;User ID=sa;Password=vV5r9tn0M4@"
},
I always get the next exception
fail: FluentMigrator.Runner.Processors.SqlServer.SqlServer2016Processor[0] There was an exception checking if table VersionInfo in (null) exists
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: Could not open a connection to SQL Server)
What am I doing wrong? I don’t understand how fix this issue to provide correct connection
2
Answers
You are using the default bridge network and you can resolve hostname in default bridge network.
Containers on the default bridge network can only access each other by IP addresses, unless you use the –link option, which is considered legacy
You can define your own network to bypass the issue
Also try removing
Persist Security Info=True
from connection stringas the mssql default port is 1433 and the both services are on the same network (@Hans Kilian => so the first connection string should works). If the problem persist i suggest you to :