title: How to expose Docker TCP socket on WSL2? (WSL-installed Docker, not Docker Desktop)
body: <p>I want to connect to Docker TCP socket running in WSL2 via PyCharm. I can't seem to expose the socket, I think possibly because there's limited control of docker-daemon within WSL2 (systemctl)? I can't use Docker Desktop because I need GPU support (Windows Dev Channel + nvidia-docker). I've tried the following:</p>
  <h3>export DOCKER_HOST=tcp://0.0.0.0:2375</h3>
  <pre><code>$ export DOCKER_HOST=tcp://0.0.0.0:2375
  $ sudo service docker restart
  $ docker context ls
  NAME                DESCRIPTION                               DOCKER ENDPOINT      KUBERNETES ENDPOINT   ORCHESTRATOR
  default *           Current DOCKER_HOST based configuration   tcp://0.0.0.0:2375                         swarm
  Warning: DOCKER_HOST environment variable overrides the active context. To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable.
  $ curl --unix-socket /var/run/docker.sock http:/localhost/version
  {&quot;Platform&quot;:{&quot;Name&quot;:&quot;Docker Engine - Community&quot;},&quot;Components&quot;:[{&quot;Name&quot;:&quot;Engine&quot;,&quot;Version&quot;:&quot;19.03.11&quot;,&quot;Details&quot;:{&quot;ApiVersion&quot;:&quot;1.40&quot;,&quot;Arch&quot;:&quot;amd64&quot;,&quot;BuildTime&quot;:&quot;2020-06-01T09:10:54.000000000+00:00&quot;,&quot;Experimental&quot;:&quot;false&quot;,&quot;GitCommit&quot;:&quot;42e35e61f3&quot;,&quot;GoVersion&quot;:&quot;go1.13.10&quot;,&quot;KernelVersion&quot;:&quot;4.19.121-microsoft-standard&quot;,&quot;MinAPIVersion&quot;:&quot;1.12&quot;,&quot;Os&quot;:&quot;linux&quot;}},{&quot;Name&quot;:&quot;containerd&quot;,&quot;Version&quot;:&quot;1.2.13&quot;,&quot;Details&quot;:{&quot;GitCommit&quot;:&quot;7ad184331fa3e55e52b890ea95e65ba581ae3429&quot;}},{&quot;Name&quot;:&quot;runc&quot;,&quot;Version&quot;:&quot;1.0.0-rc10&quot;,&quot;Details&quot;:{&quot;GitCommit&quot;:&quot;dc9208a3303feef5b3839f4323d9beb36df0a9dd&quot;}},{&quot;Name&quot;:&quot;docker-init&quot;,&quot;Version&quot;:&quot;0.18.0&quot;,&quot;Details&quot;:{&quot;GitCommit&quot;:&quot;fec3683&quot;}}],&quot;Version&quot;:&quot;19.03.11&quot;,&quot;ApiVersion&quot;:&quot;1.40&quot;,&quot;MinAPIVersion&quot;:&quot;1.12&quot;,&quot;GitCommit&quot;:&quot;42e35e61f3&quot;,&quot;GoVersion&quot;:&quot;go1.13.10&quot;,&quot;Os&quot;:&quot;linux&quot;,&quot;Arch&quot;:&quot;amd64&quot;,&quot;KernelVersion&quot;:&quot;4.19.121-microsoft-standard&quot;,&quot;BuildTime&quot;:&quot;2020-06-01T09:10:54.000000000+00:00&quot;}
  $ curl http://localhost:2375/version
  curl: (7) Failed to connect to localhost port 2375: Connection refused
  </code></pre>
  <p>The final command I expect to give a result like <code>{&quot;Version&quot;:&quot;17.05.0-ce&quot;,&quot;ApiVersion&quot;:&quot;...}</code> but connection refused. Indeed, if I try to connect via Windows host Pycharm it refuses connection. I also see many tutorials / SO posts saying not to use this DOCKER_HOST approach, but I'm not sure why.</p>
  <h3>/lib/systemd/system/docker.service &gt; ExecStart</h3>
  <p>Per <a href="https://stackoverflow.com/a/51017236/362790">stackoverflow</a>, <a href="https://serverfault.com/a/843332">serverfault</a>, <a href="https://www.ivankrizsan.se/2016/05/18/enabling-docker-remote-api-on-ubuntu-16-04/" rel="noreferrer">ivankrizsan</a>, I edited <code>/lib/systemd/system/docker.service</code> with <code>ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375</code>. But when I try <code>systemctl daemon-reload</code> it errors; WSL2 doesn't support <code>systemctl</code> commands (<a href="https://github.com/MicrosoftDocs/WSL/issues/457" rel="noreferrer">WSL/457</a>).</p>
  <pre><code>$ sudo systemctl daemon-reload
  System has not been booted with systemd as init system (PID 1). Can't operate.
  </code></pre>
  <p>I also tried restarting WSL2 (Powershell <code>wsl --shutdown</code>, re-open WSL2) in case docker-daemon will pick those change up, but no cigar.</p>
  <pre><code>$ curl http://localhost:2375/version
  curl: (7) Failed to connect to localhost port 2375: Connection refused
  </code></pre>
  <h3>/etc/default/docker &gt; DOCKER_OPTS</h3>
  <p>Per <a href="https://stackoverflow.com/a/37179733/362790">stackoverflow</a>, I edited <code>/etc/default/docker</code> with <code>DOCKER_OPTS=&quot;-H unix:// -H tcp://0.0.0.0:2375&quot;</code> then <code>sudo service docker restart</code>. Same connection refused errors.</p>