← Back to list

Connecting Windows container to Linux container

There are quite a few articles online that Windows containers can run alongside Linux containers on a Windows host. TL;DR; when switching…

Tomas M · 2024-08-01 05:41 · 2 claps · 2.0 min read
#docker #linux-containers #windows-containers
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud 🔓 · Open Source

Connecting Windows container to Linux container

There are quite a few articles online that Windows containers can run alongside Linux containers on a Windows host. TL;DR; when switching between Linux/Windows contexts, containers that were running stay up. However, if, say, a Full Framework ASP.NET app running in Windows IIS container needs to connect to Linux SQL Server container we will start running into issues. Network stacks in Windows and Linux contexts are different. If one would run docker network ls on each no common network would be found, meaning that containers like mentioned above cannot be put on the same network.

An intuitive guess would be to connect them via host, but again there are two issues: windows container does not have host.docker.internal mapped by default and, once mapping is done, the Windows firewall blocks all requests. Starting with firewall issues both blockers can be solved.

A network created in Windows context is not much more than a virtual network adapter, and firewall rules may be adjusted for it. So after creating a network:

  • Open Windows Defender Firewall with Advanced Security;
  • Select Inbound Rules;
  • Select New Rule…;
  • In wizard choose Custom, click next;
  • All Programs, click next;
  • Any protocol type (should be default), next;
  • In the local IP section, choose These IP addresses and add the host’s IP address of the virtual network adapter created (ipconfig, the network adapter will have docker's network id in its name). Click next;
  • Allow the connection, next;
  • Select all profiles for the sake of simplicity, next;
  • Enter the name of your choosing, next;
  • Finish.

Since this is a virtual network card and it cannot be accessed from outside it is safe to disable firewall on it.

Make sure not to delete this network or IP address in this rule will have to updated after recreation.

Network adapter may be named by specifying -o com.docker.network.windosshim.networkname=some_name with docker network create command. Unfortunately after restart this human readable name was lost.

The second part of getting this to work is mapping host.docker.internal in Windows container. Without a common network container cannot be resolved by its name leaving host.docker.internal most viable option. Since this has to be done each time a new container is started it’s better to automate it:

$network = docker network ls |
    Select-Object -Skip 1 |
    ConvertFrom-String -PropertyNames Id,Name |
    Where-Object { $_.Name -eq '<docker network name>' }
$adapter = Get-NetAdapter -Name "*$($network.Id)*"
$hostIp = Get-NetIPAddress -InterfaceIndex $adapter.InterfaceIndex |
    Select-Object -ExpandProperty IPAddress
$command = "Add-Content -Path C:\windows\system32\drivers\etc\hosts -Value '$hostIp`thost.docker.internal'"
docker exec <container name> powershell -Command $command

PowerShell script appends mapping line to hosts file in running container so that calls to host.docker.internal would be routed to Windows host.

Now with the server adjusted in the connection string ASP.NET app (or similar) should be able to connect to SQL server.

The above have been achieved on Windows 10 host machine.


메타데이터
post_id
4cdb2ef96206
slug
connecting-windows-container-to-linux-container-4cdb2ef96206
url
https://medium.com/@tomas.madajevas/connecting-windows-container-to-linux-container-4cdb2ef96206
canonical_url
https://medium.com/@tomas.madajevas/connecting-windows-container-to-linux-container-4cdb2ef96206
author_url
https://medium.com/@tomas.madajevas
status
ok
fetched_at
2026-06-24 11:06:28