DEV Community

Kim CH
Kim CH

Posted on Edited on

How to Share Docker Across Multiple WSL 2 Distros and Windows (Without Docker Desktop)

Docker Desktop makes local container development easy, but its strict licensing terms lead many developers to seek alternative approaches. Running Docker Engine directly inside WSL 2 is one such solution, giving you native Linux performance, total control over your setup, and zero licensing restrictions.

By exposing the Docker daemon over TCP, you can transform a single WSL 2 distribution into a shared container host—giving your Windows terminal and secondary WSL environments instant, seamless access to the exact same Docker engine.

+-----------------------------------------------------------------------+
|                           WINDOWS HOST OS                             |
|  +---------------------------+     +-------------------------------+  |
|  |   Windows Terminal        |     |   Secondary WSL Distro        |  |
|  |   (PowerShell / CMD)      |     |   (e.g., Ubuntu-Debian)       |  |
|  |   docker-cli              |     |   docker-ce-cli               |  |
|  +-------------+-------------+     +---------------+---------------+  |
|                |                                   |                  |
|                +-----------------+-----------------+                  |
|                                  |                                    |
|                   DOCKER_HOST = tcp://127.0.0.1:2375                  |
|                                  |                                    |
|                                  v                                    |
|  +-----------------------------------------------------------------+  |
|  |                      PRIMARY WSL DISTRO                         |  |
|  |  +-----------------------------------------------------------+  |  |
|  |  |                 Docker Engine (dockerd)                   |  |  |
|  |  |                 Listening on:                             |  |  |
|  |  |                 - unix:///var/run/docker.sock             |  |  |
|  |  |                 - tcp://127.0.0.1:2375                   |  |  |
|  |  +-----------------------------------------------------------+  |  |
|  +-----------------------------------------------------------------+  |  |
+-----------------------------------------------------------------------+
Enter fullscreen mode Exit fullscreen mode

Here are step-by-step

Step 1: Set Up the Primary WSL Host

Install Docker Engine

sudo apt update && sudo apt upgrade -y && \
sudo apt install -y ca-certificates curl gnupg lsb-release && \
sudo install -m 0755 -d /etc/apt/keyrings && \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null && \
sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin && \
sudo usermod -aG docker $USER && \
sudo systemctl enable --now docker
Enter fullscreen mode Exit fullscreen mode

Override Docker’s systemd unit file

  • Open the docker.service
sudo systemctl edit docker.service
Enter fullscreen mode Exit fullscreen mode
  • Paste the following content
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://127.0.0.1:2375
Enter fullscreen mode Exit fullscreen mode

Ensure it's always ready when all its terminals closed

  • Modify the wsl.conf by
sudo vi /etc/wsl.conf
Enter fullscreen mode Exit fullscreen mode
  • Paste the following content
[boot]
systemd=true
Enter fullscreen mode Exit fullscreen mode

Restart services

sudo systemctl daemon-reload && \
sudo systemctl restart docker && \
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Step 2: Setting WSL Docker Client

Install docker-ce-cli

sudo apt-get update && \
sudo apt-get install -y ca-certificates curl gnupg && \
sudo install -m 0755 -d /etc/apt/keyrings && \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \
sudo chmod a+r /etc/apt/keyrings/docker.gpg && \
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null && \
sudo apt-get update && \
sudo apt-get install -y docker-ce-cli
Enter fullscreen mode Exit fullscreen mode

Define DOCKER_HOST in ~/.zshrc

export DOCKER_HOST=tcp://127.0.0.1:2375
Enter fullscreen mode Exit fullscreen mode

Verify

source ~/.zshrc && docker ps -a
Enter fullscreen mode Exit fullscreen mode

Step 3: Setting for Windows as Docker Client

Install docker-cli

winget install Docker.DockerCLI
Enter fullscreen mode Exit fullscreen mode

Define DOCKER_HOST

[System.Environment]::SetEnvironmentVariable('DOCKER_HOST', "tcp://127.0.0.1:2375", 'User')
Enter fullscreen mode Exit fullscreen mode

Verify

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Notes

After wsl --shutdown or restarted Windows

  • You have to open the terminal of WSL-Docker-Host first

If Testcontainer is being using

It needs to be disable RYUK

  • For WSL, by modifying ~/.zshrc
export TESTCONTAINERS_RYUK_DISABLED = true
Enter fullscreen mode Exit fullscreen mode
  • For Windows, by
[System.Environment]::SetEnvironmentVariable('TESTCONTAINERS_HOST_OVERRIDE', "127.0.0.1", 'User') && `
[System.Environment]::SetEnvironmentVariable('TESTCONTAINERS_RYUK_DISABLED', "true", 'User')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)