'Cannot connect dockerized ASP.NET Core app to dockerized SQL Server

I have just configured the project to connect to a dockerized SQL Server. add-migration and update-database commands via VS worked so I think the connection string is fine. I'm guessing it has something to do with the built app being in a container. I don't use docker-compose. I plan on running them separately.

The app successfully ran but it shows this error when performing actions with the database and shows this error:

SqlException: 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)

The documentations couldn't help me out. Other questions similar to this but none of the solutions worked such as the AllowRemoteConnections set enabled (already enabled by default).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source