Reputation: 17068
Docker compose
version: '3.4'
services:
legalregtech.web.host:
image: ${DOCKER_REGISTRY-}legalregtechwebhost
build:
context: .
dockerfile: src/LegalRegTech.Web.Host/Dockerfile
networks:
- dev-net
ports:
- "22742:80"
depends_on:
- sqlServer
sqlServer:
image: "mcr.microsoft.com/mssql/server:2019-latest"
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: 'XXXXX'
ports:
- "1433:1433"
networks:
- dev-net
volumes:
- /C/Databases/SqlServer/Data:/var/opt/mssql/data
networks:
dev-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.172.0.0/16
gateway: 172.172.0.1
Docker file:
FROM mcr.microsoft.com/dotnet/aspnet:2.1 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:2.1 AS build
WORKDIR /src
COPY ["src/LegalRegTech.Web.Host/LegalRegTech.Web.Host.csproj", "src/LegalRegTech.Web.Host/"]
COPY ["src/LegalRegTech.Web.Core/LegalRegTech.Web.Core.csproj", "src/LegalRegTech.Web.Core/"]
COPY ["src/LegalRegTech.Application/LegalRegTech.Application.csproj", "src/LegalRegTech.Application/"]
COPY ["src/LegalRegTech.AzureCosmosDB/LegalRegTech.AzureCosmosDB/LegalRegTech.GraphDBConnector.csproj", "src/LegalRegTech.AzureCosmosDB/LegalRegTech.AzureCosmosDB/"]
COPY ["src/LegalRegTech.Core.Shared/LegalRegTech.Core.Shared.csproj", "src/LegalRegTech.Core.Shared/"]
COPY ["src/LegalRegTech.Core/LegalRegTech.Core.csproj", "src/LegalRegTech.Core/"]
COPY ["src/LegalRegTech.Application.Shared/LegalRegTech.Application.Shared.csproj", "src/LegalRegTech.Application.Shared/"]
COPY ["src/LegalRegTech.EntityFrameworkCore/LegalRegTech.EntityFrameworkCore.csproj", "src/LegalRegTech.EntityFrameworkCore/"]
RUN dotnet restore "src/LegalRegTech.Web.Host/LegalRegTech.Web.Host.csproj"
COPY . .
RUN dotnet publish "LegalRegTech.Web.Host.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "LegalRegTech.Web.Host.dll"]
I get this exception:
Unhandled Exception: System.Data.SqlClient.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)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection()
at Hangfire.SqlServer.SqlServerStorage.UseConnection[T](DbConnection dedicatedConnection, Func2 func) at Hangfire.SqlServer.SqlServerStorage.UseConnection(DbConnection dedicatedConnection, Action
1 action)
at Hangfire.SqlServer.SqlServerStorage.Initialize()
at LegalRegTech.Web.Startup.Startup.ConfigureServices(IServiceCollection services) in /src/src/LegalRegTech.Web.Host/Startup/Startup.cs:line 149
SQL Server is running fine, I am able to connect through SSMS in local computer. I think in the container the app is not able to connect to the SQL Server.
When running docker-compose up
a network get created as below
It gets created with the suffix 6clicks_ which is the root folder of my application and when I do the inspect on that network the container is {}
[
{
"Name": "6clicks_dev-net",
"Id": "b6f693ce2402e7c52d0c8af3048eaebd86ed19b41a43dca381f30712c00f4193",
"Created": "2021-12-10T02:34:23.8790393Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.172.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {
"com.docker.compose.network": "dev-net",
"com.docker.compose.project": "6clicks",
"com.docker.compose.version": "2.0.0"
}
}
]
Connection string:
"ConnectionStrings": {
"Default": "Data Source=127.0.0.1; Initial Catalog=xxxxx;User ID=sa; Password=xxxxx; Persist Security Info=True; MultipleActiveResultSets=True"
}
The reason I can see on the docker logs is Login failed for user 'sa'. Reason: Password did not match that for the login provided. [CLIENT: 172.172.0.2]
. However I am able to login in with SSMS with same password
Upvotes: 0
Views: 1604
Reputation: 14034
Since legalregtech.web.host
is not configured to connect to sqlServer
explicitly, it tries to connect 127.0.0.1
as configured in your appsettings.json
file.
For use within docker-compose, it's easiest to simply override this connection string to reach the sqlServer
service by adding an environment variable:
legalregtech.web.host:
# ...
environment:
- ConnectionStrings__Default=Data Source=sqlServer; Initial Catalog=xxxxx;User ID=sa; Password=xxxxx; Persist Security Info=True; MultipleActiveResultSets=True
(A more compact connection string would be: Server=sqlServer;Database=xxxxx;User ID=sa;Password=xxxxx;MultipleActiveResultSets=True;
)
Here's a fully working docker-compose file:
version: '3.4'
services:
mssql:
image: mcr.microsoft.com/mssql/server
environment:
- MSSQL_SA_PASSWORD=zHDgRw7qw4QJyrrhgXK2mG45
- ACCEPT_EULA=Y
- MSSQL_PID=Developer
dotnetapp:
image: dotnetapp
depends_on:
- mssql
build:
context: .
dockerfile: Dockerfile
restart: always
environment:
- ConnectionStrings__Default=Server=mssql;Database=amplily;User ID=sa;Password=zHDgRw7qw4QJyrrhgXK2mG45;MultipleActiveResultSets=True;
Upvotes: 0