SQL Server connection with Docker and Entity Framework
For sharing network between two containers, we could use docker-compose which will create the shared network.
Follow steps below:
docker-compose
version: '3.4'services:coredocker: image: coredocker build: context: . dockerfile: CoreDocker/Dockerfilesql.data: image: microsoft/mssql-server-linux:2017-latest environment: - SA_PASSWORD=Pass@word - ACCEPT_EULA=Y ports: - "1433:1433"
appsettings.json
{ "ConnectionStrings": { "DefaultConnection": "Server=sql.data,1433;Database=CoreDocker;MultipleActiveResultSets=true;User ID=sa;Password=Pass@word" }, "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*"}
Here are two points:
- Change
Server=tcp:127.0.0.1,1433;
toServer=sql.data,1433
which is the service name and port - Remove
Trusted_Connection=True;
As this is not clear, I am assuming that both your service and the db are dockerized.
Then first, in the appsettings.json
you have to specify the IP-address of the docker host instead of localhost, since localhost only refers to the localhost of the container itself. You might want to read more about docker networking here.
Second, I see no reason to overcomplicate the docker-compose.yml by also specifying an override.yml, but it has to be docker-compose.override.yml
instead of docker-compose-override.yml
. For debugging it might be helpful to see how a container is really started (especially if using docker-compose) with the help of docker inspect.