WSL 上的 Docker 出现错误:“docker:无法连接到 unix:///var/run/docker.sock 上的 Docker 守护进程。docker 守护进程是否正在运行?”

WSL 上的 Docker 出现错误:“docker:无法连接到 unix:///var/run/docker.sock 上的 Docker 守护进程。docker 守护进程是否正在运行?”

我正在为学校的带薪实习项目工作,该项目要求我在 WSL ubuntu 实例上安装 docker。但是,在我按照以下说明操作后这一页安装 docker 然后运行

sudo docker run hello-world

我收到错误:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

我从遇到同样问题的人那里找到了一些其他问题,他们建议要么运行systemctl start docker,但由于systemdWSL 中不是 init 系统,因此无法运行,要么运行dockerd(docker 守护进程)。我试过这个,出错了:

time="2021-08-13T14:42:48.183317500-04:00" level=info msg="Starting up"
time="2021-08-13T14:42:48.219667900-04:00" level=info msg="libcontainerd: started new containerd process" pid=10598
time="2021-08-13T14:42:48.219758500-04:00" level=info msg="parsed scheme: \"unix\"" module=grpc
time="2021-08-13T14:42:48.219782400-04:00" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
time="2021-08-13T14:42:48.219819200-04:00" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///var/run/docker/containerd/containerd.sock  <nil> 0 <nil>}] <nil> <nil>}" module=grpc
time="2021-08-13T14:42:48.219854500-04:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
time="2021-08-13T14:42:48.254159100-04:00" level=info msg="starting containerd" revision=e25210fe30a0a703442421b0f60afac609f950a3 version=1.4.9
time="2021-08-13T14:42:48.271842300-04:00" level=info msg="loading plugin \"io.containerd.content.v1.content\"..." type=io.containerd.content.v1
time="2021-08-13T14:42:48.271938000-04:00" level=info msg="loading plugin \"io.containerd.snapshotter.v1.aufs\"..." type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.282592100-04:00" level=info msg="skip loading plugin \"io.containerd.snapshotter.v1.aufs\"..." error="aufs is not supported (modprobe aufs failed: exit status 1 \"modprobe: FATAL: Module aufs not found in directory /lib/modules/4.4.0-19041-Microsoft\\n\"): skip plugin" type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.282655000-04:00" level=info msg="loading plugin \"io.containerd.snapshotter.v1.btrfs\"..." type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.282826400-04:00" level=info msg="skip loading plugin \"io.containerd.snapshotter.v1.btrfs\"..." error="path /var/lib/docker/containerd/daemon/io.containerd.snapshotter.v1.btrfs (wslfs) must be a btrfs filesystem to be used with the btrfs snapshotter: skip plugin" type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.282846500-04:00" level=info msg="loading plugin \"io.containerd.snapshotter.v1.devmapper\"..." type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.282873900-04:00" level=warning msg="failed to load plugin io.containerd.snapshotter.v1.devmapper" error="devmapper not configured"
time="2021-08-13T14:42:48.282891500-04:00" level=info msg="loading plugin \"io.containerd.snapshotter.v1.native\"..." type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.283006900-04:00" level=info msg="loading plugin \"io.containerd.snapshotter.v1.overlayfs\"..." type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.283468100-04:00" level=info msg="loading plugin \"io.containerd.snapshotter.v1.zfs\"..." type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.283622300-04:00" level=info msg="skip loading plugin \"io.containerd.snapshotter.v1.zfs\"..." error="path /var/lib/docker/containerd/daemon/io.containerd.snapshotter.v1.zfs must be a zfs filesystem to be used with the zfs snapshotter: skip plugin" type=io.containerd.snapshotter.v1
time="2021-08-13T14:42:48.283650700-04:00" level=info msg="loading plugin \"io.containerd.metadata.v1.bolt\"..." type=io.containerd.metadata.v1
time="2021-08-13T14:42:48.283687900-04:00" level=warning msg="could not use snapshotter devmapper in metadata plugin" error="devmapper not configured"
time="2021-08-13T14:42:48.283719100-04:00" level=info msg="metadata content store policy set" policy=shared
time="2021-08-13T14:42:48.284129000-04:00" level=info msg="loading plugin \"io.containerd.differ.v1.walking\"..." type=io.containerd.differ.v1
time="2021-08-13T14:42:48.284158200-04:00" level=info msg="loading plugin \"io.containerd.gc.v1.scheduler\"..." type=io.containerd.gc.v1
time="2021-08-13T14:42:48.284196800-04:00" level=info msg="loading plugin \"io.containerd.service.v1.introspection-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284238500-04:00" level=info msg="loading plugin \"io.containerd.service.v1.containers-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284256900-04:00" level=info msg="loading plugin \"io.containerd.service.v1.content-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284311100-04:00" level=info msg="loading plugin \"io.containerd.service.v1.diff-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284336000-04:00" level=info msg="loading plugin \"io.containerd.service.v1.images-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284383600-04:00" level=info msg="loading plugin \"io.containerd.service.v1.leases-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284411900-04:00" level=info msg="loading plugin \"io.containerd.service.v1.namespaces-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284430700-04:00" level=info msg="loading plugin \"io.containerd.service.v1.snapshots-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.284448100-04:00" level=info msg="loading plugin \"io.containerd.runtime.v1.linux\"..." type=io.containerd.runtime.v1
time="2021-08-13T14:42:48.284738900-04:00" level=info msg="loading plugin \"io.containerd.runtime.v2.task\"..." type=io.containerd.runtime.v2
time="2021-08-13T14:42:48.284970100-04:00" level=info msg="loading plugin \"io.containerd.monitor.v1.cgroups\"..." type=io.containerd.monitor.v1
time="2021-08-13T14:42:48.285348400-04:00" level=info msg="loading plugin \"io.containerd.service.v1.tasks-service\"..." type=io.containerd.service.v1
time="2021-08-13T14:42:48.285376700-04:00" level=info msg="loading plugin \"io.containerd.internal.v1.restart\"..." type=io.containerd.internal.v1
time="2021-08-13T14:42:48.285462400-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.containers\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285492300-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.content\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285526300-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.diff\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285547200-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.events\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285567300-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.healthcheck\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285587700-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.images\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285608600-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.leases\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285627700-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.namespaces\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285647900-04:00" level=info msg="loading plugin \"io.containerd.internal.v1.opt\"..." type=io.containerd.internal.v1
time="2021-08-13T14:42:48.285727800-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.snapshots\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285757700-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.tasks\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285778300-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.version\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.285796900-04:00" level=info msg="loading plugin \"io.containerd.grpc.v1.introspection\"..." type=io.containerd.grpc.v1
time="2021-08-13T14:42:48.288091300-04:00" level=info msg=serving... address=/var/run/docker/containerd/containerd-debug.sock
time="2021-08-13T14:42:48.290093400-04:00" level=info msg=serving... address=/var/run/docker/containerd/containerd.sock.ttrpc
time="2021-08-13T14:42:48.290649900-04:00" level=info msg=serving... address=/var/run/docker/containerd/containerd.sock
time="2021-08-13T14:42:48.290688200-04:00" level=info msg="containerd successfully booted in 0.048662s"
time="2021-08-13T14:42:48.304911700-04:00" level=info msg="parsed scheme: \"unix\"" module=grpc
time="2021-08-13T14:42:48.304940700-04:00" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
time="2021-08-13T14:42:48.304998900-04:00" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///var/run/docker/containerd/containerd.sock  <nil> 0 <nil>}] <nil> <nil>}" module=grpc
time="2021-08-13T14:42:48.305013000-04:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
time="2021-08-13T14:42:48.305870600-04:00" level=info msg="parsed scheme: \"unix\"" module=grpc
time="2021-08-13T14:42:48.305910200-04:00" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
time="2021-08-13T14:42:48.305928800-04:00" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///var/run/docker/containerd/containerd.sock  <nil> 0 <nil>}] <nil> <nil>}" module=grpc
time="2021-08-13T14:42:48.305957000-04:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
time="2021-08-13T14:42:48.313617500-04:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
time="2021-08-13T14:42:48.316205300-04:00" level=warning msg="Your kernel does not support cgroup memory limit"
time="2021-08-13T14:42:48.316237300-04:00" level=warning msg="Unable to find cpu cgroup in mounts"
time="2021-08-13T14:42:48.316245800-04:00" level=warning msg="Unable to find blkio cgroup in mounts"
time="2021-08-13T14:42:48.316253900-04:00" level=warning msg="Unable to find cpuset cgroup in mounts"
time="2021-08-13T14:42:48.316261600-04:00" level=warning msg="Unable to find pids cgroup in mounts"
time="2021-08-13T14:42:48.316390300-04:00" level=info msg="Loading containers: start."
time="2021-08-13T14:42:48.325245700-04:00" level=warning msg="Running iptables --wait -t nat -L -n failed with message: `iptables v1.8.4 (legacy): can't initialize iptables table `nat': Table does not exist (do you need to insmod?)\nPerhaps iptables or your kernel needs to be upgraded.`, error: exit status 3"
time="2021-08-13T14:42:48.501619100-04:00" level=info msg="stopping event stream following graceful shutdown" error="<nil>" module=libcontainerd namespace=moby
time="2021-08-13T14:42:48.502669400-04:00" level=info msg="stopping healthcheck following graceful shutdown" module=libcontainerd
time="2021-08-13T14:42:48.502675100-04:00" level=info msg="stopping event stream following graceful shutdown" error="context canceled" module=libcontainerd namespace=plugins.moby
time="2021-08-13T14:42:49.504913000-04:00" level=warning msg="grpc: addrConn.createTransport failed to connect to {unix:///var/run/docker/containerd/containerd.sock  <nil> 0 <nil>}. Err :connection error: desc = \"transport: Error while dialing dial unix:///var/run/docker/containerd/containerd.sock: timeout\". Reconnecting..." module=grpc
failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.8.4 (legacy): can't initialize iptables table `nat': Table does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.
 (exit status 3)

我尝试升级 iptables 和内核(通过运行sudo apt-get dist-upgrade),但没有任何变化。我该如何解决这个问题?

答案1

您已按照说明在 Ubuntu(真正的 Linux 发行版)上安装 Docker。这不适用于 WSL,因为必须安装 Docker在 Windows 中

微软的描述可以在文章中找到 在 WSL 2 上开始使用 Docker 远程容器

文章列出了先决条件:

  • Windows 10 至少为 2004 版本
  • 启用 WSL、安装 Linux 发行版并更新到 WSL 2
  • Linux 内核更新包。

完成后:

  • 安装 Docker Desktop在 Windows 中
  • 启用 WSL 与安装在 Windows 中的 Docker 引擎的集成。

详细信息请参阅文章,以上只是步骤的简短概述。

有关在 Windows 上安装的 Docker 文档位于 Docker Desktop WSL 2 后端 并且可能对完成 Microsoft 文章有帮助。

答案2

大部分内容都是从我最近在 Stack Overflow 上回答的类似问题中复制粘贴的:

您正在 Ubuntu WSL 上直接安装 Docker Engine,这很好,但我想确保您了解您还有另一种选择。“推荐”方法(正如 @harrymc 指出的那样)是安装适用于 Windows 的 Docker 桌面

Docker Desktop 确实在基础 Docker Engine 上提供了一些附加功能:

  • 它可以在多个 WSL2 实例之间共享
  • 它可以从 PowerShell 和 CMD 运行
  • 它提供了容器和卷的 GUI 仪表板
  • 它可以处理自动升级(尽管有些人可能认为这不一定是一个优势)
  • 这是一种便捷的方法,可以自动为您处理下面所有其他的内容。

两个版本都运行相同的引擎,但 Desktop 也提供了这些额外的便利功能。如果你不需要或不想要这些,而且你真的只想安装 Docker Engine,那么当前问题的解决方案应该是运行:

sudo service docker start

然后再次尝试该示例。这是 WSL 中必需的额外步骤,Docker 文档中未涵盖该步骤。

(额外提示)我建议hello-world通过以下方式运行:

sudo docker run --rm hello-world

在我看来,没有理由让那个特定的容器在退出后继续挂起。我希望 Docker 能更新这些文档。

此外,假设一切正常,我建议通过以下方式删除该图像:

sudo docker rmi hello-world

解释:

在“常规”Ubuntu 上,软件包执行的安装过程的一部分docker-ce是启动 Docker 守护程序。但是,由于 WSL 的工作方式不同(缺少运行级别、systemd、启动脚本支持等),该步骤失败。

因此,当您进入(当前)步骤 3 来验证守护进程是否正在通过运行时docker run ...,结果并非如此。

当您进入“Linux 安装后步骤:配置 Docker 以在启动时启动”部分时,您也会遇到类似的问题*

这在 WSL 上不起作用,因为没有“启动”的概念(同样,没有 Systemd)。因此,您需要一种替代方法来确保引擎在 WSL 中运行。当然,sudo service docker start当您需要它时,它可以只是一本手册。或者,如果您愿意,您可以将以下内容添加到您的~/.bashrc

wsl.exe -u root -e sh -c "service docker status || service docker start"

还有其他方法,但这只是一个例子。

相关内容