Docker on WSL erroring: “docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.”

dockerlinuxUbuntuunixwindows-subsystem-for-linux

I'm working on a project for a paid internship at my school that requires that I install docker on a WSL ubuntu instance. However, after I follow the instructions from this page to install docker and then running

sudo docker run hello-world

I get the error:

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

I found some other questions from people who had the same problem, and they recommended either running systemctl start docker, which doesn't work due to the fact that systemd isn't the init system in WSL, or running dockerd (the docker daemon). I tried this, errored:

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)

I tried upgrading iptables and my kernel (by running sudo apt-get dist-upgrade) and there was no change. How can I fix this problem?

Best Answer

You have followed instructions to install Docker on Ubuntu, which is a real Linux distribution. This does not suite WSL, because Docker has to be installed in Windows.

The Microsoft description is found in the article Get started with Docker remote containers on WSL 2.

The article lists the prerequisites:

  • Windows 10 at least version 2004
  • Enabled WSL, install a Linux distribution, and update to WSL 2
  • The Linux kernel update package.

Once this is done:

  • Install Docker Desktop in Windows
  • Enable WSL integration with the Docker engine that is installed in Windows.

The details are found in the article, the above is only a short summary of the steps.

The Docker documentation for installation on Windows is found in Docker Desktop WSL 2 backend and might be useful to complete the Microsoft article.

Related Question