It seems you have 2 Docker Containers:
- For Web API
- For SQL Server
You need Web API Container to Access SQL Server container for doing database operations. Here you need Docker Compose. You can create it with Visual Studio itself. It's code should be as shown below:
version: '3.4'
services:
dockercrud:
image: ${DOCKER_REGISTRY-}dockercrud
build:
context: .
dockerfile: WebAPI/Dockerfile
sqldata:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
- SA_PASSWORD=yourpassword
- ACCEPT_EULA=Y
ports:
- "1433:1433"
You reference SQL Server with sqldata
service and so in your connection string you can refer it as:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=sqldata;Initial Catalog=MovieDB;Persist Security Info=True;User ID=SA;Password=yourpassword"
}
}
Reference - CRUD Operations in ASP.NET Core and SQL Server with Docker