Skip to content

Latest commit

 

History

History
66 lines (52 loc) · 3.22 KB

Networking.md

File metadata and controls

66 lines (52 loc) · 3.22 KB

Networking

Containers default to ContainerNetworkingMode.Bridged. The default WSLC bridge supports container-to-container IP traffic but not container-name DNS. None disables networking.

Managed networks and alias DNS

Both backends can create an owned bridge network:

var backend = await ContainerBackends.ResolveAsync(ct);
await using var network = await backend.CreateNetworkAsync(cancellationToken: ct);
await using var database = new ContainerBuilder(backend)
    .WithImage("redis:7")
    .WithNetwork(network.Name)
    .WithNetworkAliases("cache")
    .Build();
await database.StartAsync(ct);

Attach another container using the same backend and WithNetwork(network.Name) to reach cache:6379. Dispose containers before disposing the network. Names/aliases must not contain secrets. Network aliases require an explicit network; a named network cannot be combined with None.

Docker uses Testcontainers networks. WSLC uses the managed Microsoft.WSL.Containers 3.0.5 network API (Session.CreateNetwork/DeleteNetwork and ContainerSettings.Networks), so creating a managed network and attaching containers with aliases no longer needs the wslc CLI. The ordinary default-bridge path remains SDK-only as well. See Capabilities and Cancellation for timeouts and platform limits.

The WSLC default bridge has no container-name DNS; alias resolution is a property of the managed network, so WithNetwork(...).WithNetworkAliases(...) is the supported shape for container-to-container names.

Host loopback (WSLC)

Microsoft.WSL.Containers 3.0.5 can expose the Windows host loopback interface to the session under a hostname you choose. It is session-wide and off by default:

ContainerBackends.Use(new WslContainerBackend(WslContainerRuntimeOptions.Default with
{
    HostLoopback = "host.internal",
}));

A container can then reach a service listening on the Windows host by that name. Use it only when a container must call a host process; the alternative — and the normal shape for a test host calling into a container — is a published port on 127.0.0.1.

Host connections and IPs

  • WSLC host bindings are IPv4 loopback only. Clients should use 127.0.0.1, not localhost: IPv6-first clients, notably SQL Server clients, may not fall back.
  • Docker bindings default to 127.0.0.1; explicit PortBinding.HostAddress and TCP/UDP protocol selections are forwarded to Docker.
  • Random host ports are allocated by the runtime, never by probing for an unused port.
  • GetMappedPublicPort returns the assigned host port. TCP/HTTP readiness checks use the mapped host port on IPv4 loopback.
  • NetworkIp is read after startup for the selected network, and is null before start or without a network. Prefer explicit aliases for container-to-container traffic on managed networks.
  • Built-in module ConnectionMode.Container providers remain unimplemented; use explicit aliases and service ports to construct such connection strings.

UDP mapping remains unsupported by the WSLC public contract. Dynamic network attachment and arbitrary network drivers are not exposed by the shared API; a managed network's subnet/gateway options available on Microsoft.WSL.Containers are not surfaced yet.