我正在尝试使用连接字符串连接到.NET中的SQL Server映像。因此,一开始,我使用如下命令运行sql server:
docker network create sql-netwrok
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Arman123!" -p 5435:1433 --net sql-network -d mcr.microsoft.com/mssql/server
在运行了我的sql server之后,我编写了我的应用程序Dockerfile,如下所示:
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-migration
WORKDIR /app
COPY *.csproj .
RUN dotnet restore
COPY . .
WORKDIR /app
ENV CONNECTION_STRING="server=<container-name Or IP>,5435;database=Test;User Id=sa;Password=Arman123!;"
RUN dotnet publish -c Release -o out
EXPOSE 88
CMD dotnet run
然后,我尝试了这个命令来构建和运行我的Dockerfile:
docker build -t migration-sample .
docker run -it --net sql-network migration-sample
但在运行应用程序后,我不断收到这个错误:
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 t
he 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, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, Ses
sionData reconnectSessionData, Boolean applyTransientFaultHandling)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions user
Options)
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, DbConnectionInte
rnal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 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, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Migrations.Runner.CreateDatabase(String connectionString) in /app/Runner.cs:line 27
at Migrations.Runner.Main(String[] args) in /app/Runner.cs:line 14
ClientConnectionId:00000000-0000-0000-0000-000000000000
这就是我从控制台应用程序中的环境变量读取连接字符串的方式:
var connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING",
EnvironmentVariableTarget.Process);
我尝试过的方法:
我使用shell在迁移示例容器中移动,收到了sql server ping。这意味着这两个容器可以互相看到。我还使用容器IP&名称
谢谢你的帮助。
我使用以下命令解决了问题:
docker inspect <your sql-server>
在这之后,你会看到类似的东西:
"Networks": {
"sql-network": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"6f927bd17835"
],
"NetworkID": "028dabf00c4bda62e47f086026a503915f1eb11cfae7137fe6aba3d68fc9ee35",
"EndpointID": "47ca6cf26ea317297297091b3b57187f00a18e0748319b73f34d97d95e881ef1",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:02",
"DriverOpts": null
}
}
请使用网关,而不是在连接字符串中使用IPAddress。所以连接字符串应该是:
ENV CONNECTION_STRIGN: "Server=<container_gateway_address>,5435;
Database=Test;User Id=sa; Password=Arman123!;"