I followed the steps in this tutorial: https://learn.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?