testcontainers / testcontainers-dotnet

A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.
https://dotnet.testcontainers.org
MIT License
3.65k stars 250 forks source link

[Bug]: Unable to connect to mssql from 2nd test container #1185

Closed gogo199432 closed 1 month ago

gogo199432 commented 1 month ago

Testcontainers version

3.8.0

Using the latest Testcontainers version?

Yes

Host OS

Windows

Host arch

x86

.NET version

8.0.200

Docker version

Client:
 Cloud integration: v1.0.35+desktop.13
 Version:           26.0.0
 API version:       1.45
 Go version:        go1.21.8
 Git commit:        2ae903e
 Built:             Wed Mar 20 15:18:56 2024
 OS/Arch:           windows/amd64
 Context:           default

Server: Docker Desktop 4.29.0 (145265)
 Engine:
  Version:          26.0.0
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.8
  Git commit:       8b79278
  Built:            Wed Mar 20 15:18:01 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker info

Client:
 Version:    26.0.0
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.13.1-desktop.1
    Path:     C:\Program Files\Docker\cli-plugins\docker-buildx.exe
  compose: Docker Compose (Docker Inc.)
    Version:  v2.26.1-desktop.1
    Path:     C:\Program Files\Docker\cli-plugins\docker-compose.exe
  debug: Get a shell into any image or container. (Docker Inc.)
    Version:  0.0.27
    Path:     C:\Program Files\Docker\cli-plugins\docker-debug.exe
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.2
    Path:     C:\Program Files\Docker\cli-plugins\docker-dev.exe
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.23
    Path:     C:\Program Files\Docker\cli-plugins\docker-extension.exe
  feedback: Provide feedback, right in your terminal! (Docker Inc.)
    Version:  v1.0.4
    Path:     C:\Program Files\Docker\cli-plugins\docker-feedback.exe
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.1.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-init.exe
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-sbom.exe
  scout: Docker Scout (Docker Inc.)
    Version:  v1.6.3
    Path:     C:\Program Files\Docker\cli-plugins\docker-scout.exe

Server:
 Containers: 3
  Running: 3
  Paused: 0
  Stopped: 0
 Images: 12
 Server Version: 26.0.0
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
 Kernel Version: 5.15.146.1-microsoft-standard-WSL2
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 2.908GiB
 Name: docker-desktop
 ID: 4ce81f6a-31a4-4159-a4fa-1a4773a61bb8
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Labels:
  com.docker.desktop.address=npipe://\\.\pipe\docker_cli
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support
WARNING: No blkio throttle.write_iops_device support
WARNING: daemon is not using the default seccomp profile

What happened?

I tried setting up 2 test containers. One is a database (mssql) and the other a containerized application that would use this DB. End goal is to have an easy E2E testing setup. However with a very bare-bones setup, the app is unable to connect to the DB. I tried all possible combinations of database connection strings (ip, dns name, container name, network alias, hardcoded etc.). Tried with and without custom network. No dice. However I can connect to the DB from the host, and when I ran adminer, it could also connect. Not sure what I'm doing wrong with EF Core.

Here is the Program.cs of the test app:

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
builder.Configuration.AddEnvironmentVariables();

builder.Services.AddSqlServer<DbContext>(builder.Configuration.GetConnectionString("DefaultConnection"));

var app = builder.Build();

app.MapGet("/", (DbContext ctx) =>
{
    if (!ctx.Database.CanConnect())
    {
        return "Connection failed";
    }

    Console.WriteLine("Connection successful");
    return "Connection successful";
});

app.Run();

Container for the app is created via dotnet publish:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>disable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <Configurations>Debug;Release</Configurations>
        <ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:8.0-jammy-chiseled-extra</ContainerBaseImage>
    </PropertyGroup>
</Project>

And the part that creates the test containers:

using DotNet.Testcontainers.Builders;
using Testcontainers.MsSql;

namespace E2ETests;

[TestFixture]
public class InfraHelper
{
    [Test]
    public async Task SetupInfra()
    {
    var network = new NetworkBuilder().Build();
        var mssql = new MsSqlBuilder()
            .WithPassword("Password1!")
            .WithNetwork(network)
            .WithNetworkAliases("mssql")
            .WithCleanUp(false)
            .Build();

        await mssql.StartAsync();

        //TODO: DB Connection does not work for some reason...
        var app = new ContainerBuilder()
                .WithImage("containertest")
                .DependsOn(mssql)
                .WithNetwork(network)
                .WithEnvironment("ConnectionStrings__DefaultConnection", $"Server={mssql.Name[1..]},{mssql.GetMappedPublicPort(1433)};user=sa;Password=Password1!;MultipleActiveResultSets=True;Encrypt=false")
                .WithPortBinding(8080, true)
                .WithCleanUp(false)
            .Build();

        await app.StartAsync();
    }
}

Relevant log output

2024-05-23 13:38:41 dbug: Microsoft.EntityFrameworkCore.Infrastructure[10401]
2024-05-23 13:38:41       An 'IServiceProvider' was created for internal use by Entity Framework.
2024-05-23 13:38:41 dbug: Microsoft.EntityFrameworkCore.Infrastructure[10403]
2024-05-23 13:38:41       Entity Framework Core 8.0.2 initialized 'DbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:8.0.2' with options: None
2024-05-23 13:38:41 dbug: Microsoft.EntityFrameworkCore.Database.Connection[20005]
2024-05-23 13:38:41       Creating DbConnection.
2024-05-23 13:38:41 dbug: Microsoft.EntityFrameworkCore.Database.Connection[20006]
2024-05-23 13:38:41       Created DbConnection. (20ms).
2024-05-23 13:38:41 dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
2024-05-23 13:38:41       Opening connection to database '' on server 'brave_wescoff,60706'.
2024-05-23 13:38:56 dbug: Microsoft.EntityFrameworkCore.Database.Connection[20004]
2024-05-23 13:38:56       An error occurred using the connection to database '' on server 'brave_wescoff,60706'.
2024-05-23 13:38:56 dbug: Microsoft.EntityFrameworkCore.Infrastructure[10407]
2024-05-23 13:38:56       'DbContext' disposed.
2024-05-23 13:38:56 dbug: Microsoft.EntityFrameworkCore.Database.Connection[20007]
2024-05-23 13:38:56       Disposing connection to database '' on server 'brave_wescoff,60706'.
2024-05-23 13:38:56 dbug: Microsoft.EntityFrameworkCore.Database.Connection[20008]
2024-05-23 13:38:56       Disposed connection to database '' on server '' (1ms).

Additional information

No response

HofmeisterAn commented 1 month ago

To establish a container-to-container communication, you don't need to map the assigned host port. Simply use the network alias and the actual container port. In your case:

Server=mssql,1433
gogo199432 commented 1 month ago

you are of course completely correct. I got so fixated on the network that I forgot about that fact... thanks for the support :)