我已经使用以下命令拉取并运行了SQL Server 2017容器镜像:
docker pull microsoft/mssql-server-linux
docker run --name mssql -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=!Abcd123' -p 1433:1433 -d microsoft/mssql-server-linux
我还使用以下命令将Docker Core Web API应用程序部署到ASP.NET容器:
dotnet publish -c Release -o Output
docker build -t apitest .
docker run -p 3000:80 --name apitest_1 apitest
Dockerfile
的内容
FROM microsoft/dotnet
COPY Output /app
WORKDIR /app
EXPOSE 80/tcp
ENTRYPOINT ["dotnet", "DockerSQLTest.dll"]
在我的Web API应用程序中,我创建了一个实体框架核心迁移,它将创建数据库并播种一些数据。在Startup
类的Configure
方法中,我添加了以下代码以将挂起的迁移应用于数据库:
public async void Configure(IApplicationBuilder app,
IHostingEnvironment env,
StudentDbContext dbContext)
{
await dbContext.Database.MigrateAsync();
...
}
数据库连接字符串是从包含以下部分的appsettings.json
中检索的:
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Database=student;User Id=sa;Password=!Abcd123;"
}
但应用程序无法正常运行,出现异常消息:
fail: WebApplication6.Startup[0]
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.ExistsAsync(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
有什么问题吗?
发布于 2018-11-16 15:33:50
Docker内置DNS服务器,容器通过容器名称相互连接。在本例中,您将SQL Server容器命名为mssql
,因此这是需要放入.NET应用程序的连接字符串中的服务器名称:Server=mssql;Database=student;User Id=sa;Password=!Abcd123;
。
看看.NET Core album viewer sample on GitHub,它使用Docker Compose来定义多容器应用程序。
发布于 2019-10-23 15:12:52
解决方法是先检查集装箱网络,然后找到网络IP并放入appsetting.json
"ConnectionStrings": {
"DefaultConnection": "Server=172.17.0.2;Database=VehicleKey;User Id=sa;Password=p@ssW0rd;"
}
查找SQLSERVER的名称
发布于 2018-10-22 17:45:01
首先,当您使用两个或更多容器时,不能有任何本地主机连接。每个docker容器都有自己的内部网络IP。一旦您使用公开的端口启动了一个容器,以便能够连接到该容器,您需要指定主机IP (容器实际运行的位置)。
因此,作为示例,您应该有用于连接的下一个字符串:
Server=192.168.1.99,1433;Database=student;User Id=sa;Password=!Abcd123;
其中: 192.168.1.99 -运行docker容器的主机的实际IP。
https://stackoverflow.com/questions/52926064
复制相似问题