0

I followed the steps in this tutorial: https://docs.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-ver15 to create a container with my personal database backup. This part is working correctly and I am able to connect to the database through SSMS using these credentials:

  • Server Name: localhost,1433 OR 172.0.0.1,1433 (these both work)
  • Username: sa
  • Password: my password

Then I created a web API application with this Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build-env
WORKDIR /src

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /src
COPY --from=build-env /src/out .
ENTRYPOINT ["dotnet",'MyApp.Api.dll"]

This is my connection string in appsettings.json

"Server=localhost,1433;Database=MyDB;User Id=sa;Password=PASSWORD"

However, I keep getting this error when running the web api container:

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]

An error occurred using the connection to database 'MyDB' on server 'localhost,1433'.

fail: Microsoft.EntityFrameworkCore.Query[10100]

An exception occurred while iterating over the results of a query for context type 'NAMESPACE.MyContext'.

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)

I already tried changing the connection string to connect by name instead of localhost or IP (I used sql1 which is the name of the container, but still doesn't work)

Since I'm fairly new to Docker and containers, would this type of setup require a Docker Compose file? or it should just work the way it currently is?

Found this answer: accessing a docker container from another container Maybe someone can confirm if this is the way to go?

SOfanatic
  • 5,400
  • 5
  • 31
  • 54
  • This smells like some network connection going wrong / firewall issue. Sure you can connect using SSMS from the same location? – TomTom Jul 13 '20 at 14:44
  • @TomTom yes, I'm pretty new to Docker and containers and forgot to mention that this is all running from my PC. Meaning the mssql container is in my docker desktop and SSMS is in the same PC and I am able to connect and query. – SOfanatic Jul 13 '20 at 14:49
  • Just to have a clear picture of your services, `docker ps` ? And your API and SQL are sharing the same network? – abestrad Jul 13 '20 at 14:52
  • @abestrad yes docker ps and API + SQL same network. – SOfanatic Jul 13 '20 at 15:02
  • 1
    docker ps will show the column names, try to use that as the hostname instead of localhost, or the service name – abestrad Jul 13 '20 at 15:04
  • @abestrad already tried that, the name of the SQL container is sql1 so I did `Server=sql1;Database=MyDB...` – SOfanatic Jul 13 '20 at 15:09
  • If both server and application are running on containers, then best approach is to use docker compose which automatically sets up the network for you. – Durga Prasad Jul 14 '20 at 18:27
  • @DurgaPrasad I believe this is the right approach as I got it to work with Compose. Thanks – SOfanatic Jul 16 '20 at 12:51
  • If you need acces host network from docker, see https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container – George Paoli Jul 21 '20 at 00:56

0 Answers0