Communication error from .net core docker web.api to phisical CB Server

Hi All,
I’ve encountered a problem of connection from my docker .net core 3.1 web.api against CB Server running on a virtual machine in the lan. The problem is related just to the scenario in which the web.api is running in a docker container. In the following scenarios the system works properly:

  • web.api in IIS Express on my laptop
  • IIS on a server
  • CB server in the same docker client of the web.api

I’m using .net SDK 2.7. The bootstrap is the following:

// define settings for this Couchbase Cluster connection
        var config = new ClientConfiguration()
            LoggerFactory = loggerFactory,
            PoolConfiguration = new PoolConfiguration
                MaxSize = 20,
                MinSize = 1,
                SendTimeout = 12000

            // Impostazione del keep-alive per verifica se migliora la connettività al cluster
            EnableTcpKeepAlives = true, //default it true
            TcpKeepAliveTime = 1000 * 60, //set to 60secs
            TcpKeepAliveInterval = 5000,

            // assign one or more Couchbase Server URIs available for bootstrap
            Servers = new List<Uri> {
                new Uri(server)
            UseSsl = false,
            BucketConfigs = new Dictionary<string, BucketConfiguration>
                //{"SmartPipeTracking", new BucketConfiguration
                {bucketName, new BucketConfiguration
                        UseEnhancedDurability = true,
                        BucketName = bucketName,
                        UseSsl = false

        // Eseguo lo shoutdown qualora il metodo di inizializzazione venisse richiamato due volte
        if(ClusterHelper.Initialized) Cleanup();

        // initialize Cluster, to be built as a singleton
        ClusterHelper.Initialize(config, new PasswordAuthenticator(username, password));

I’ve also tried to set the following tcp parameter inside the container:

- net.ipv4.tcp_keepalive_time=295
- net.ipv4.tcp_keepalive_intvl=45
- net.ipv4.tcp_keepalive_probes=15
privileged: true

and I confirm that accessing the container shell the values are correct.

The problem is logged as this:

2020-12-04 12:56:20.0757 | DEBUG | init main
2020-12-04 13:04:36.2436 | ERROR | System.IO.IOException: The response ended prematurely.
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ChunkedEncodingReadStream.ReadAsyncCore(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)
at System.IO.StreamReader.ReadLineAsyncInternal()
2020-12-04 13:04:48.4162 | ERROR | Failed to perform SelectBucket operation for ‘SmartPipeTracking’. Reason TransportFailure.

It happens after more or less 10 min the web.api startup. After that the communication with the database is not working anymore.

The containers are running in a dedicated bridge network.

Hello @Dario_Mazza since you mentioned docker container I am assuming its Linux based container?

TCPKeepAlive with SDK 2.x would not work due to a known issue
This was fixed with SDK 3.x
There are two options here
a) Migrate to SDK 3.x (recommended) as 2.x will go End of Life in March, In the latest versions this is taken care of.
b) Use the Ping API in 2.x to ping the database in a separate thread to keep the connection alive.

Hope this helps !

Thanks for your help, I’ve implemented an internal Task that query the bucket every 5 minutes. I’ve tried to use the ping API
Bucket.Ping(new ServiceType { ServiceType.KeyValue, ServiceType.Query, ServiceType.Search });
but the communication error still happen. So, I’ve put in the task a
var result = Bucket.Get(“XX”);
and now the communication is stable during the time.

Best regards