The game plan

Pre-requisites

Enable-WindowsOptionalFeature -Online -FeatureName 'Containers' -AllEnable-WindowsOptionalFeature -Online -FeatureName 'Microsoft-Hyper-V' -AllEnable-WindowsOptionalFeature -Online -FeatureName 'VirtualMachinePlatform' -AllEnable-WindowsOptionalFeature -Online -FeatureName 'Microsoft-Windows-Subsystem-Linux' -All
winget install --id 'Microsoft.Powershell' --scope machine
PS> winget install Canonical.Ubuntu
PS> ubuntu
Installing, this may take a few minutes
Please create a default UNIX user account. The username does not need to match your Windows username.
Installation successful!
Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.10.60.1-microsoft-standard-WSL2 x86_64)
[...]
exit

Meanwhile, on the Windows side…

wsl --list
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

Install docker in WSL

sudo apt update && sudo apt upgrade
sudo apt install --no-install-recommends apt-transport-https ca-certificates curl gnupg2
source /etc/os-release
curl -fsSL https://download.docker.com/linux/${ID}/gpg | sudo apt-key add -
echo “deb [arch=amd64] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable” | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
sudo usermod -aG docker $USER
sudo mkdir /etc/docker/
sudo vi /etc/docker/daemon.json
{
"hosts": [ "tcp://0.0.0.0:" ],
"tls": false
}
sudo dockerd
sudo dockerd &
sudo pkill dockerd
ss -peanut
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
tcp LISTEN 0 4096 *:2375 *:* ino:41752 sk:2001 v6only:0 <->
docker -H 127.0.0.1 run --rm hello-world
export DOCKER_HOST="tcp://127.0.0.1:2375"
sudo visudo
%docker ALL=(ALL) NOPASSWD: /usr/bin/dockerd

Build docker-cli for Windows

git clone https://github.com/docker/cli.git
cd cli
docker buildx bake
docker buildx bake --set binary.platform=windows/amd64
cp build/docker-windows-amd64.exe /mnt/c/tools

Back in the Windows zone

C:\
cd c:\tools
ren docker-windows-amd64.exe docker.exe
wsl -- ip -o -4 -json addr list eth0 `
| ConvertFrom-Json `
| %{ $_.addr_info.local } `
| ?{ $_ }
docker -H <YOUR_WSL_IP> ps
docker run --rm hello-world
docker context create wsl --docker "host=tcp://<YOUR_WSL_IP>:2375"docker context use wsldocker run --rm hello-world
docker context update wsl --docker "host=tcp://<NEW_WSL_IP>:2375"
$wslip = wsl -- ip -o -4 -json addr list eth0 | ConvertFrom-Json | %{ $_.addr_info.local } ` | ?{ $_ }Write-Host "Setting Docker context 'wsl' to host=tcp://$($wslip):2375"docker context update wsl --docker "host=tcp://$($wslip):2375"docker context use wsl
Logo

Ubuntu 社区为您提供最前沿的新闻资讯和知识内容

更多推荐