Hi, working with docker and .NET core is really cool in matters of scalability and separation of micro services. However, for the beginners, it could be tedious to have the containers access the SQL Server databases.
Enable SQL-Based notification
As you all know, SQL Server supports windows-based and SQL-based authentication. Knowing, that containers are based on linux (in most of the cases), the first authentication option is logically eliminated. Hence, to make the containers access the SQL, the server should be configured to support sql authentication. Click here to check the docs.
Open the port !
Yes, obviously, the container will access to SQL Server using a specific port (1433 as the default value). The host computer firewall has to authorize incoming access through the port 1433 in order to make the containers access the databases.
Configure the right server IP address
When accessing local databases, we are generally using localhost or dots (.) in our connection strings. These connection strings won’t work on the containers for obvious reasons. The right IP address is the ip attributed by docker to docker nat (check it using ipconfig).
Configure your docker profile for your app
To make the deployment easier, create a specific profile for the docker deployment in your launchSettings.json. The snippet below is an example of this profile.
As you can see, the environment variable ASPNETCORE_ENVIRONMENT takes the value “docker”. To have settings specific to the docker environment, create a file named appsettings.docker.json in the root directory and set a specific connection string (with the adequate IP address).
However, when you run your container using docker run, the default environment name will be “Production” and your setting file will not be taken in account.
Don’t worry. The solution is as simple as adding an ENV instruction to your docker file just before copying the final files as specified by the example below
FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "yourapp.dll"]