0

I run docker-compose on my WSL with a jupyter notebook, it gives me following information:

[I 00:28:20.921 NotebookApp] Jupyter Notebook 6.1.3 is running at:
[I 00:28:20.921 NotebookApp] http://docker-desktop:3000/?token=...
[I 00:28:20.921 NotebookApp] or http://127.0.0.1:3000/?token=...
[I 00:28:20.921 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

as docker is running on WSL I can't access it via localhost on my windows machine. I looked up the ip of the network adapter which is 172.23.16.1 and tried to access the notebook via 172.23.16.1:3000, but I get an error connection refused.

I also opened incoming and outgoing port 3000 on my windows machine

What have I missed?

Thomas
  • 1

2 Answers2

1
  • Have you map your container port so the host machine can reach?

  • Another common problem is: by default jupiter notebook will only allow traffic coming from localhost (notice that this localhost is the container itself), therefore you can't access from anywhere outside of the container. So to resolve this, make sure you start jupiter notebook and allow traffic coming from all IPs:

jupyter notebook --ip 0.0.0.0
Duc Trung Mai
  • 2,141
  • 1
  • 24
  • 23
  • You'll probably need to do *both* this and one of the workarounds in my answer. I wasn't aware that Jupyter only bound to localhost by default, but even if Jupyter is listening to all addresses, it still won't get traffic between Docker and WSL2 without additional effort. – NotTheDr01ds Jan 19 '21 at 02:39
  • so I mapped within the docker compose file the container port to host port by putting in 3000:3000. I also checked the the docker file in jupyter notebook but the ip was already set to 0.0.0.0 and incoming connection to *. I also performed the action proposed in the WSL2 bridge workaround - still I can't connect on my windows machine to localhost:3000 or the ip within WSL nor the ip of the WSL adapter in my windows machine – Thomas Jan 20 '21 at 04:18
1

Long story short, you are almost certainly running into the same problem documented in this, this, and this question, among others. The last one is most similar, since it is about accessing a WSL2 instance from a Docker container, but they are all the same root cause. To quote my answer (slightly modified) from one of those:

The core issue here is that WSL2 operates in a Hyper-V VM with its own virtual NIC, running NAT'd behind the Windows host. WSL1, on the other hand, ran bridged with the Windows NIC.

On localhost, Windows does seem to do an automatic mapping, but for the host IP address (and thus, on the local network - Including Docker containers, since they are on their own network), it does not. Even with the Docker network in bridged mode, it still does not see the WSL2 IP without additional effort.

You'll find a lot of information on this particular topic on this Github thread, along with several workarounds that I documented in answers to the other questions.

In your case, I would propose running the Jupyter notebook in a WSL1 instance, rather than WSL2. To my knowledge, there's nothing special in Jupyter which would require WSL2 capabilities, right?

Again, with a copy/paste here -- You can convert the WSL2 instance to WSL1 by either doing (from PowerShell) a wsl --set-version <distroname> 1 or by cloning the existing with a wsl --export <distroname> <archivename>.tar and then wsl --import <distroname> <installlocation) <archivename>.tar. I prefer cloning since it gives you a backup.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70