User Tools

Site Tools


docker:replace-docker-desktop-on-windows-with-docker-engine-in-wsl2-ubuntu

Replace Docker Desktop on windows with Docker Engine in WSL2 (Ubuntu)

1. Install WSL2 on Windows

2. Install Ubuntu in WSL2

This can be install from Microsoft Store.

2. Install Git bash

This can be installed alongside Tortoise GIT.

3. Install Docker Engine in WSL 2 in Ubuntu.

You can follow these steps: https://docs.docker.com/engine/install/ubuntu/

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] 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

https://docs.docker.com/engine/install/linux-postinstall/

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
docker run hello-world
sudo systemctl enable docker.service
sudo systemctl enable containerd.service

4. Remote access to docker in WSL2 from Windows

You can follow these steps: https://docs.docker.com/config/daemon/remote-access/

sudo mkdir /etc/systemd/system/docker.service.d

sudo vi /etc/systemd/system/docker.service.d/override.conf 

Add the below to the override.conf file:

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://127.0.0.1:2375

sudo systemctl daemon-reload
sudo systemctl restart docker.service
sudo systemctl status docker.service

5. Configure Testcontainers to connect to the docker server from Ubuntu

In git bash:

vi ~/.testcontainers.properties

Add the following lines:
docker.client.strategy=org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy
docker.host=tcp\://127.0.0.1\:2375

6. Install windows docker client

There are 2 options here.

6.1 Option 1 - Use directly binaries provided by docker.com

The client downloaded form docker.com will connect to the server from WSL2 (using DOCKER_HOST variable).

https://download.docker.com/win/static/stable/x86_64/

First, extract the latest version and put the installation path in the windows PATH env variable.

Second, add env variable

DOCKER_HOST = tcp://localhost:2375

Now, test containers should work and also the docker client.

6.2 Option 2 - Use choco to install binaries

The client downloaded form choco will connect to the local server (from docker engine component).

choco install docker-cli
choco install docker-compose
choco install docker-engine
choco install kubernetes-cli

So, when DOCKER_HOST is not present the docker client will connect to the local docker engine:

Otherwise it will connect to WSL2 (by setting

DOCKER_HOST = tcp://localhost:2375

)

Leave a WSL2 console opened with ubuntu (which runs docker)!
wsl -d ubuntu

This is because Windows automatically closes the WSL idle instances. (so the docker client cannot connect to the docker inside Ubuntu)

To use docker-compose.exe, add this path in the env %PATH% variable:
C:\ProgramData\docker\cli-plugins
docker/replace-docker-desktop-on-windows-with-docker-engine-in-wsl2-ubuntu.txt · Last modified: 2024/09/27 18:58 by odefta