Modules/businessdev.ALbuild.Containers/Private/Initialize-BcTransparentNetwork.ps1

function Initialize-BcTransparentNetwork {
    <#
    .SYNOPSIS
        Ensures a transparent Docker network exists on the host and returns its name.
 
    .DESCRIPTION
        Internal helper for -Transparent container creation. A transparent network attaches the
        container directly to the physical LAN (its own IP via the LAN's DHCP), so the container is
        reachable from other hosts without port publishing or a firewall rule. If a transparent
        network already exists it is reused; otherwise one is created with 'docker network create -d
        transparent'.
 
        Host prerequisite (logged, not enforced - it cannot be detected reliably from inside the
        container host): if this Docker host is a Hyper-V VM, MACAddressSpoofing must be enabled on the
        VM's network adapter, otherwise Hyper-V drops the container's DHCP/traffic (multiple MACs on one
        vNIC). On a physical host no extra step is needed.
 
    .PARAMETER Name
        Name to use when creating the network (default 'transparent'). Ignored if a transparent
        network already exists (the existing one is reused).
 
    .PARAMETER DockerExecutable
        The Docker executable to use (default 'docker').
 
    .OUTPUTS
        [string] The name of the transparent network to pass to 'docker run --network'.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([string])]
    param(
        [string] $Name = 'transparent',
        [string] $DockerExecutable = 'docker'
    )

    # Reuse an existing transparent network if the host already has one.
    $ls = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru `
        -Arguments @('network', 'ls', '--filter', 'driver=transparent', '--format', '{{.Name}}')
    if ($ls.Success) {
        $existing = @("$($ls.StdOut)" -split "`r?`n" | Where-Object { $_.Trim() } | Select-Object -First 1)
        if ($existing.Count -gt 0) {
            $found = "$($existing[0])".Trim()
            Write-ALbuildLog "Reusing existing transparent Docker network '$found'."
            return $found
        }
    }

    Write-ALbuildLog ("No transparent Docker network found; creating '$Name' (driver=transparent). " +
        "If this host is a Hyper-V VM, enable MACAddressSpoofing on its network adapter or the " +
        "container's DHCP/traffic will be dropped.")
    if (-not $PSCmdlet.ShouldProcess($Name, 'Create transparent Docker network')) { return $Name }

    $create = Invoke-BcDocker -DockerExecutable $DockerExecutable -PassThru `
        -Arguments @('network', 'create', '-d', 'transparent', $Name)
    if (-not $create.Success) {
        throw "Failed to create transparent Docker network '$Name': $($create.StdErr.Trim())"
    }
    return $Name
}