vscode-dev-containers icon indicating copy to clipboard operation
vscode-dev-containers copied to clipboard

Unable to connect to container Mongo DB within Visual Studio Code?

Open russjr08 opened this issue 4 years ago • 4 comments

Hello,

Apologies if this has already been covered, I could not see this in any prior issues that I came across. I've just setup a new Node.js & Mongo DB development container as it seems like a fantastic feature (learned about it in a recent Microsoft Developer session)! I hope we'll see more projects featuring this in the future.

To the actual problem: Using the included MongoDB extension for VSC while using this development container, it cannot seem to reach the MongoDB server running in the container.

Specifically, it hangs at the connection setup wizard like so: image

Now I have forwarded the mongodb port via devcontainer.json (though I am not sure if this is needed, since shouldn't this be taking place inside the container's network?) and can see that it's running through netstat:

node ➜ /workspace (main ✗) $ netstat -tulnp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.11:34723        0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::33885                :::*                    LISTEN      144/node            
udp        0      0 127.0.0.11:33186        0.0.0.0:*                           -                   
node ➜ /workspace (main ✗) $ 

Meanwhile, it connects just fine in Mongo Compass running on my local computer:

image

This is with the exact same connection string as well (picked up from mongosh in the container).

Now I'm not exactly sure if this is an issue with the extension, or with the container itself (as this is my first time using a dev container), but I figured since it's bundled with this container it was worth bringing up.

  • VSCode Version: 1.61.2
  • Local OS Version: EndeavourOS (Arch Linux Derivative)
  • Reproduces in: <Remote - Containers>
  • Name of Dev Container Definition with Issue: Node.js & Mongo DB

Steps to Reproduce:

  1. Create a new Node.js & Mongo DB container within Visual Studio Code
  2. When either prompted by the Mongo extension to add a connection, or by activating the menu via the Mongo extension on the sidebar, attempt to connect either with a connection string after running mongosh in the container terminal, or click "Advanced Connection Settings" -> "Open form" and use the defaults.

If any further information is required, I'd be happy to provide it!

And a personal thanks from me, for helping make VSCode one of the most intuitive development environments.

russjr08 avatar Nov 04 '21 04:11 russjr08

Hmmm. Not quite sure what might be happening if you're using the definition without modifications. I do not have that exact Linux distro, but my Ubuntu and Debian machines are working as is macOS. I assume you're also using visual-studio-code-bin out of AUR (or using just the official .tgz binaries) for VS Code?

There was another report about Arch (https://github.com/microsoft/vscode-dev-containers/issues/1054) that also has not appeared elsewhere that I suspected was some sort of configuration nuance with Docker itself or a missing package given there's no official Docker Arch packages. Unfortunately, I do not have a great suggestions for triaging.

Chuxel avatar Nov 04 '21 21:11 Chuxel

Oh one possibility that could be missing is that extensions that store passwords often use libsecret on Linux. Typically that means you have to have gnome-keyring installed as well. If you're running another desktop environment, that could be causing the extension to error.

Chuxel avatar Nov 04 '21 21:11 Chuxel

Thanks for getting back to me! So I actually do have Gnome 40 installed, along with libsecret 0.20.4-1 installed, and visual-studio-code-bin is what I am using as well (by the way, since its in the AUR I'm not sure if Microsoft is pushing it to the AUR, or just a kind contributor, but either way I'm incredibly glad its there!)

All of the the docker components actually are working quite perfectly, it just seems to be the Mongo extension that does not work with it, which since I can use Mongo Compass to manage the database I do have options, though I do believe having VSCode integration would make the project more portable to those who want to contribute or try it out locally. Though, I did notice that because the extension is stuck attempting to connect, it tends to break most of the functionality of vscode, I can't reload the window, close out of the window, etc it needs to be force quit with killall code.

I did think that there was a possibility the keyring wasn't unlocked, since I sign into my desktop using a Yubikey which means the keyring isn't unlocked until I open an application that requires it (Such as Element, Calendar, 1Password, etc) - I just checked to make sure the keyring is unlocked, and it is.

I also tried to grab the flatpak version of VSCode however when trying to open the project folder in a container I get "Docker version 17.12.0 or later is required." even though I have Docker version 20.10.10, build b485636f4b installed - I am going to assume this is a sandboxing limitation/nature of Flatpaks that are causing this though.

russjr08 avatar Nov 05 '21 00:11 russjr08

Resurrecting an older topic, but as I ran into the same problem today and this was about the only source I found, my own findings might be helpful to others.

The key seems to be that the MongoDB Extension running from VS Code inside a devcontainer does not know how to find and connect to localhost:27017. This is probably due to the fact that the database is running in a different container and localhost resolves to the current one you are developing in.

What I had to do was to set the port inside devcontainer.json in the format ":" like this: "forwardPorts": ["db:27017"]

This allows me to then connect inside VS Code to "db:27017" instead of "localhost:27017". This way, you will see the port inside the port list in VS Code and then MongoDB for VS Code works like it should.

At this point, I can connect via MongoDB Compass as "localhost:27017" and via VS Code as "db:27017". Kind of a brain twister it seems, but as soon as VS Code knows where to resolve the port to, everything seems fine.

spectrum-coding avatar May 16 '22 12:05 spectrum-coding