Docker是在WSL中运行还是连接回Windows?

9
在WSL中成功安装Docker后,如果我运行标准的Docker命令,会出现连接错误。在所有示例中,使用sudo或不使用sudo都会产生相同的结果。
root@SUR002731165154:~# sudo docker info
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

root@SUR002731165154:~# sudo docker version
Client:
Version:      1.13.1
API version:  1.26
Go version:   go1.7.5
Git commit:   092cba3
Built:        Wed Feb  8 06:42:29 2017
OS/Arch:      linux/amd64
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

然而,如果我在命令中添加-H localhost:2375,我可以毫无问题地运行Docker。 Docker是连接到我的本地主机Windows 10还是在WSL上本地运行,或者两者都有?

root@SUR002731165154:~# docker -H localhost:2375 version
Client:
Version:      1.13.1
API version:  1.26
Go version:   go1.7.5
Git commit:   092cba3
Built:        Wed Feb  8 06:42:29 2017
OS/Arch:      linux/amd64

Server:
Version:      1.13.1
API version:  1.26 (minimum version 1.12)
Go version:   go1.7.5
Git commit:   092cba3
Built:        Wed Feb  8 08:47:51 2017
OS/Arch:      linux/amd64
Experimental: true

root@SUR002731165154:~# docker -H localhost:2375 info
Containers: 11
Running: 0
Paused: 0
Stopped: 11
Images: 8
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1
runc version: 9df8b306d01f59d3a8029be411de015b7304dd8f
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.8-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934 GiB
Name: moby
ID: 4LYM:R5T5:6CPZ:Z2KC:YQ4R:NGN4:V6SR:DF7E:YPYO:7FHY:EQW5:2T7W
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 13
Goroutines: 21
System Time: 2017-02-28T18:54:13.7726687Z
EventsListeners: 0
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
3个回答

8

Docker守护程序无法在WSL下运行,因为它没有实现必要的内核ABI。如果您正在运行Windows版Docker,则可能连接到它管理的Hyper-V虚拟机。

由于它在不同的机器上,您将无法将WSL目录挂载为Docker卷,但是您可以从WSL目录构建镜像。这是因为在构建时,客户端创建tarball并将其发送到守护程序。


自从WSL2发布以来,情况发生了变化。现在运行了完整的Linux内核实例,而不是Hyper-V虚拟化。 - edddd

4
这是一个解决方法,即在WSL上使用Docker而不是在Windows上进行。只需将以下内容添加到您的WSL .bashrc文件中即可。
export PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export PATH="$PATH:/mnt/c/Program\ Files/Docker/Docker/resources/bin"
alias docker=docker.exe
alias docker-compose=docker-compose.exe

参考资料:https://blog.jayway.com/2017/04/19/running-docker-on-bash-on-windows/

该文介绍了在Windows上通过Bash运行Docker的方法。首先需要安装Ubuntu Bash,然后安装Docker并启动服务即可在Bash中使用Docker命令。值得注意的是,在Windows文件系统和容器之间共享文件需要使用特殊路径。

如何在shell脚本中使用别名:https://unix.stackexchange.com/a/125405/15183 - Vadzim

0

正如其他答案所述,Docker 无法在 WSL 中安装和托管,但可以像任何 Windows 可执行文件一样直接从其中使用。

可能需要额外的努力才能使其在没有那些烦人的 .exe 扩展名的情况下可用: Windows 上的 Bash - exe 文件的别名。 链接的解决方法提供了一个准备好的自定义脚本,用于轻松创建符号链接。与 bash 别名不同,符号链接可以在非交互式 shell 脚本中自由使用。


还有一种方法是通过将WSL连接到Docker for Windows作为远程主机来描述,具体请参见https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly

如果你想知道“为什么不直接在WSL中从Docker for Windows运行docker.exe和docker-compose.exe?”,那是因为在该环境中以交互模式运行Docker或Docker Compose存在一个错误。简而言之,您无法以交互模式在前台运行任何内容,这使其无法用于真正的Web开发。

但遗憾的是,那里没有关于确切错误症状的参考。而我到目前为止也没有遇到过这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接