用 WSL 运行 Docker 镜像

2019-04-28 17:19:51 +08:00
 ly50247

Win 10 的 WSL 功能基本完善了,但一直运行不了 Docker (只能运行 Docker Client,Server 需要用虚拟机)。不过因为 WSL 中可以安装多个实例,并且可以同时运行,互不干扰,所以可以将 Docker 镜像下载下来,创建新的 WSL 实例,这样虽然不能实现 Docker 的很多功能,但作为本地开发和日常使用基本也够用了,感兴趣的朋友可以试下。

运行效果

先看下效果(都是终端界面,不截图了):

% wsldl busybox (从 Docker Hub 下载镜像,安装为 WSL 实例,生成启动脚本等)
Downloading 'library/busybox:latest@latest' (1 layers)...
#################################################################### 100.0$
#################################################################### 100.0$

Download of images into '/home/goreliu/tmp/docker/busybox' complete.
Use something like the following to load the result into a Docker daemon:
  tar -cC '/home/goreliu/tmp/docker/busybox' . | docker load
bin/
bin/[
bin/[[
bin/acpid
bin/add-shell
...
var/spool/
var/spool/mail/
var/www/

% wsll (列出所有 WSL 实例)
适用于 Linux 的 Windows 子系统:
Mine (默认)
thrift
empty
debian
rootfs
alpine
hello-world
memcached
busybox

% wsldr busybox (通过 docker 镜像中的 Entrypoint 和 Cmd 启动) 
/mnt/c/tmp/docker #

% wsld busybox (直接进入 shell )
/mnt/c/tmp/docker #

% wsldr hello-world (运行个 hello-world,这个我之前已安装)

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

实现方式

  1. 首先,从 Docker Hub 下载镜像,有现成脚本,download-frozen-image-v2.sh
  2. 然后,准备用于安装 WSL 实例的最小镜像(可以从 https://github.com/0xbadfca11/miniwsl/releases 下载)。
  3. 最后,自己编写各种自动化的脚本,我是用 zsh 写的,先贴在下边,如果感兴趣的人较多我再放到 GitHub 上,不然就不折腾了。
#!/bin/zsh
# 这个文件需要在 .zshrc 里 source

# 文件位置:
# c:/tmp 对应 ~/tmp
# WSL 实例安装到 c:/tmp/wsl/
# Docker 文件放置在 c:/tmp/docker/ 中,WSL 实例安装后可以删除

export WS='/mnt/c/Windows/System32'
alias wsl="/init $WS/wsl.exe"
# 运行指定 WSL 实例
alias wsld="/init $WS/wsl.exe -d"
# 列出 WSL 实例
alias wsll="/init $WS/wsl.exe -l"
# 列出在运行的 WSL 实例
alias wsllr="/init $WS/wsl.exe -l --running"
# 杀掉在运行的 WSL 实例
alias wslt="/init $WS/wsl.exe -t"
# 用于创建新的 WSL 实例,需要自己指定镜像文件
# 用法: wsln 实例名称
alias wsln='wsli c:/mine/app/miniwsl/rootfs.tgz'

# 安装 WSL 实例
wsli() {
	(($# > 0)) || {
		echo "Usage: $0 tarfile [name]"
		return 1
	}

	[[ -n $2 && -e ~/tmp/wsl/$2 ]] && {
		echo $2 exist
		return 1
	}

	local name=${${1%%.*}:t}
	[[ -n $2 ]] && name=$2

	local filename=$1
	[[ ${1:e} == xz ]] && xz -d $1 && filename=${1%.*}

	/init $WS/wsl.exe --import $name c:/tmp/wsl/$name $1
}

# 删除 WSL 实例,包括其中的所有文件
wslu() {
	[[ ! -n $1 || $1 == Mine ]] && return 1

	/init $WS/wsl.exe --unregister $1 && rm -rvf ~/tmp/wsl/$1
}

# 从 Docker Hub 下载镜像文件,并安装为 WSL 实例
wsldl() {
	[[ -n $1 ]] || return 1

	local name=$1
	local all=${name}:latest

	[[ $1 == *:* ]] && {
		name=${1/:/_}
		all=$1
	}

	download-frozen-image-v2.sh ~/tmp/docker/$name $all

	cd ~/tmp/docker/$name && wsldocker $name
}

# 将 Docker Hub 的配置文件转换成初始化脚本,由 wsldl 调用,也可手动运行
wsldocker() {
	local name=$1

	[[ -f repositories ]] || return 1

	[[ -n $1 ]] || name=$(jq -r 'keys[0]' repositories)

	wsli c:/mine/app/miniwsl/rootfs.tgz $name || return 1
	wsldinit

	for i ($(jq -r '.[0].Layers[]' manifest.json)) {
		/init $WS/wsl.exe -d $name tar -xvf $i -C/
	}
}

# 运行 Docker 镜像的初始化脚本
wsldr() {
	[[ -n $1 ]] || return 1

	[[ -f ~/tmp/wsl/$1/user ]] || {
		echo ~/tmp/wsl/$1/user not found
		return 1
	}

	/init $WS/wsl.exe -d $1 -u "$(cat ~/tmp/wsl/$1/user)" /myinit
}

还有一个用来处理 Docker 镜像配置文件的脚本 wsldinit:

#!/bin/zsh
[[ -f repositories ]] || {
    echo repositories not found
    return 1
}

name=$(jq -r 'keys[0]' repositories)

[[ -d ~/tmp/wsl/$name ]] || {
    echo ~/tmp/wsl/$name not found
    return 1
}

userfile=~/tmp/wsl/$name/user
runfile=~/tmp/wsl/$name/myinit

[[ -f manifest.json ]] || {
    echo manifest.json not found
    return 1
}

json=$(jq -r '.[0].Config' manifest.json)

User=$(jq -r '.config.User' $json)
if [[ -n $User ]] {
    echo $User > $userfile
} else {
    echo root > $userfile
}

echo '#!/bin/sh\n' > $runfile

cat $json | jq -r '.config.Env[]' | sed 's/^/export /g' >> $runfile

echo >> $runfile

WorkingDir=$(jq -r '.config.WorkingDir' $json)
[[ -n "$WorkingDir" ]] && {
    echo cd \"$WorkingDir\" >> $runfile
}

echo -n "\nexec " >> $runfile

Entrypoint=$(jq '.config.Entrypoint[]' $json 2>/dev/null | tr '\n' ' ')
[[ -n "$Entrypoint" ]] && {
    echo -n $Entrypoint >> $runfile
}

Cmd=$(jq '.config.Cmd[]' $json | tr '\n' ' ')
[[ -n "$Cmd" ]] && {
    echo $Cmd >> $runfile
}

/init /mnt/c/Windows/System32/wsl.exe -t $name
cp $runfile ${runfile:h}/rootfs/
/init /mnt/c/Windows/System32/wsl.exe -d $name chmod 755 /myinit

因为是从我自己环境拷贝出来的,运行可能会有问题,喜欢折腾的朋友可以玩玩。

8562 次点击
所在节点    程序员
25 条回复
mimimiZ
2019-04-28 17:22:56 +08:00
ummm 楼主为什么要在 WSL 里面用 docker 直接在 win 上面用不是也可以... 只是好奇,我是在 mac 上直接使用 docker for mac 的 所以觉得 docker for win 应该也差不多。
ly50247
2019-04-28 17:26:27 +08:00
@mimimiZ 直接在 Windows 下运行需要用虚拟机,而且体积较大,不想安装。
mimimiZ
2019-04-28 17:28:16 +08:00
@ly50247 原来如此 明白了!
v2dead
2019-04-28 17:31:19 +08:00
楼主思路清奇,这个想法相当给劲啊。不过有 volume 卷和端口映射应该会有点问题。
xzc19970719
2019-04-28 17:33:37 +08:00
。。。强
gosansam
2019-04-28 17:34:39 +08:00
难得一见教程贴 顶了
KuroNekoFan
2019-04-28 17:36:08 +08:00
直接用 docker-desktop 可能简单点……
Rwing
2019-04-28 17:44:39 +08:00
ly50247
2019-04-28 17:45:39 +08:00
@v2dead 端口映射也可以通过脚本来处理,但比较麻烦,需要适配不同程序。更高级的功能就很难实现了。不过如果有人专门开发的话还是能实现不少功能,虽然意义不是很大,也难说一定不会有好事者。我想更可能的是利用 Docker Hub 的镜像自己实现一套适用于 WSL 的系统,专门用来开发,毕竟很多人都只是用 Docker 的最基本功能。
ly50247
2019-04-28 17:53:24 +08:00
@Rwing 我记得之前试过不行,一会再试试吧。
ly50247
2019-04-28 19:07:53 +08:00
@Rwing 搜了一下好像确实能用,但据说高于 17.09 的就不行了,而且功能不全,不想折腾它了。另外其实我没有什么用 Docker 的需求,主要是去下它的镜像玩玩,不想一直起个服务。
hantsy
2019-04-28 19:42:12 +08:00
Docker for win 一直不想用,第一它还是基于 Hyper-V 虚拟机的,第二与 VirtualBox 等互不相容,可是我要用 VirtualBox,所以直到现在我还是用 DockerToolbox (也就是使用 VirtualBox 的版本)。

用 Docker 还是要用 Linux。
ly50247
2019-04-28 19:51:18 +08:00
@hantsy 我也因为装了 VirtualBox 而一直没尝试 Hyper-V,虽然用了 WSL 后就很少用 VrtualBox 了,但没有还是不方便。也许哪天有兴趣了再折腾下,把 VirtualBox 换成 Hyper-V。

另外我也写了几个 VirtualBox 的 alias:

alias vm='/init </dev/null /mnt/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe'
alias vmlist='vm list vms; echo --RUNNING--; vm list runningvms'
alias vmup='vm startvm archlinux --type headless'
alias vmdown='vm controlvm archlinux savestate'
alias vmpause='vm controlvm archlinux pause'
alias vmresume='vm controlvm archlinux resume'
alias vmhalt='vm controlvm archlinux poweroff'

vw() {
[[ $1 == "-r" ]] && {
ssh -tq $USER@$HOST $*[2,-1]
return
}

local args
(($# >= 1)) && args="zsh -ic '$*'"

ssh -tq $USER@$HOST $args
}
beginor
2019-04-28 21:07:41 +08:00
Docker for Win 最大的痛点是卷映射时, 不支持文件锁定, 导致很多服务(尤其是数据库)无法运行。
tt0411
2019-04-28 21:09:05 +08:00
回归 Windows 指日可待
Imr
2019-04-28 21:51:43 +08:00
和虚拟机运行一个 coreos 做 server 端比,优势在哪,感觉像是从一个坑跳进另一个坑
bellchu
2019-04-28 22:15:52 +08:00
Docker 对内核环境有要求,WSL 的内核非全部 feature 都 enable 状态,再加上 WSL 的 low io,我觉得没啥意义。
ly50247
2019-04-28 22:17:59 +08:00
@Imr 不需要运行 Server,直接用就可以,节省资源。
xdlucky
2019-04-28 22:31:55 +08:00
还是装虚拟机吧,虚拟机多好玩啊
0Y89tX3MgR4I
2019-04-28 22:43:02 +08:00
nice !最近正好有这个需求,不过看下来感觉还是太折腾了,我个人还是宁愿找个 Linux 环境。不过还是支持楼主整理一下放到 GitHub 上

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/559536

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX