0
votes

I set up SQL Server on Docker. I can't connect to it from my .NET Core app.

For example, when running

dotnet ef database update

I get this error

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: 35 - An internal exception was caught)

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=myDb;User=sa;Password=reallyStrongPwd123"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 1443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY testProj/TestProj.csproj TestProj/
RUN dotnet restore "testproj/TestProj.csproj"
COPY . .
WORKDIR "/src/testProj"
RUN dotnet build "TestProj.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestProj.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestProj.dll"]
2
did you setup ASPNETCORE_ENVIRONMENT? do you have only appsettings.json or you also have appsettings.your environment name.json like appsettings.Development.json - NAS
@NAS I don't. I'll create it. Thanks - Guga Todua
I updated the answer - NAS

2 Answers

2
votes

When you run a app in container, your app is isolated from your host machine. So, if you run your sql server on your host machine, you can run your app container with docker run --network="host" command. Or you can user docker-compose instead of this like below;

mydbserver:
    container_name: mydbserver
    image: "mcr.microsoft.com/mssql/server"
    restart: always
    environment:
        SA_PASSWORD: "Your_password123"
        ACCEPT_EULA: "Y"
    volumes:
        - your_path:/...wherever you want to mount...

myapp:
    container_name: myapp
    image: myapp
    restart: always
    depends_on: 
        - mydbserver
    links:
        - mydbserver
    volumes:
        - your_path:/...wherever you want to mount...

If you run your app and db like above, you can use "mydbserver" in your connection string's "Server" section for connect to sql server.

You can find all this information in docker documentation.

0
votes

In dockerfile set the ENVIRONMENT

ENV ASPNETCORE_ENVIRONMENT Development

and you must have appsettings.Development.json and in StartUp.cs, you load files like this

public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            
        }