Diogo Ribeiro
Diogo Ribeiro

Reputation: 43

Running Windows and Linux containers on docker and WSL

For a project we're working on, we have a very specific requirement: we need to run both Windows Containers and Linux Containers over Docker. On a Windows machine running Windows 11 and the latest version of WSL (2.2.4.0), we followed this guide to set up Docker on Windows for Windows Containers, and installed docker-ce on WSL to run the Linux Containers.

We also configured WSL networking mode as mirrored and turned on the hostAddressLoopback experimental feature, as per Microsoft’s instructions here.

Using the newly created Docker context, we can launch Linux Containers on WSL and access them from the host OS. Similarly, we can launch Windows Containers and communicate with them on the host OS.

We have a particular requirement where the containers on WSL need to communicate with the containers on the host OS, and this is where we run into a problem.

We can't connect to the Windows container from within WSL, no matter which address we use. We tried localhost, the IP of the Windows container, all addresses returned by the ip a command, and more, but all attempts resulted in timeouts. However, if we run a Python echo server on Windows (non-containerised) and bind a port (e.g., port 8080), we can use telnet from WSL to connect to it without any issues.

Given that services running directly on Windows (non-containesized) can be accessed from WSL, we are wondering if there is some port forwarding we're missing that would allow the Windows Containers and Linux Containers to communicate. Or, alternatively, we might need to create a specific network with a predefined subnet to put both containers in.

We tried all of the above experiments with Windows Firewall both enabled and disabled, but the issue persists.

Any ideas?

Upvotes: 1

Views: 332

Answers (1)

Ovidiu Buligan
Ovidiu Buligan

Reputation: 2812

I had a similar situation same setup with network mirroring and docker inside WSL2 . I had a nginx proxy running in docker in WSL2 that needed to access a service that was running on windows (without docker). I only needed to set the networking mode of the nginx container to host and it worked.

services:
  my-nginx:
    build:
      target: final
      context:   "."
    ports:
      - 80:80
    network_mode: "host"   # <<<<<<<<<

nginx config snippet :

    location ~ /api {    
       proxy_pass http://127.0.0.1:5000; # requires  network_mode: "host" for frontend container       
    }

If your windows container has the docker container port exposed , the windows container should be available from windows host at curl http://127.0.0.1:80/api and also from linux container at curl http://127.0.0.1:80/api

Upvotes: 0

Related Questions