Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to SQL Server docker container from another container?

I have pulled and run SQL Server 2017 container image using the following command:

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

And I also deployed an ASP.NET Core Web API application to a Docker container, using the following commands:

dotnet publish -c Release -o Output
docker build -t apitest .
docker run -p 3000:80 --name apitest_1 apitest

The content of Dockerfile:

FROM microsoft/dotnet
COPY Output /app
WORKDIR /app
EXPOSE 80/tcp
ENTRYPOINT ["dotnet", "DockerSQLTest.dll"]

In my Web API application, I have created an Entity Framework Core migration which will create the database and seed some data. In Configure method of Startup class, I add the following code to apply the pending migrations to the database:

public async void Configure(IApplicationBuilder app,
                            IHostingEnvironment env, 
                            StudentDbContext dbContext)
{
    await dbContext.Database.MigrateAsync();
    ...
}

And the database connection string is retrieved from appsettings.json which contains the following section:

"ConnectionStrings": {
    "DefaultConnection": "Server=localhost,1433;Database=student;User Id=sa;Password=!Abcd123;"
}

But the app cannot run correctly, the exception message:

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)

Is there anything wrong?

like image 515
WPInfo Avatar asked Oct 22 '18 09:10

WPInfo


1 Answers

Docker has a DNS server built in, and containers connect to each other by the container name. In your case you named the SQL Server container mssql so that's the server name you need to put into your connection string for the .NET app: Server=mssql;Database=student;User Id=sa;Password=!Abcd123;.

Check out the .NET Core album viewer sample on GitHub, which uses Docker Compose to define a multi-container app.

like image 178
Elton Stoneman Avatar answered Oct 02 '22 13:10

Elton Stoneman