1

Situation: I have been connecting to a MongoDB through an SSH Tunnel from python scripts that I run locally. I am using SSHTunnelForwarder and PyMongo for this. The scale of the scripts have somehow outgrown what my mac is able to do in a reasonable amount of time, which is why I want to start deploying them to remote servers running in a docker container. I want to test that everything works locally first.

Problem: I am not very experienced with Docker and have not yet found a practicable/understandable solution to tunnel to the MongoDB from inside my Docker container.

I found this extremely helpful post on how connections from inside a docker container should work for a MYSQL db From inside of a Docker container, how do I connect to the localhost of the machine? .

However, unfortunately I don't understand how I would have to set everything up to get it working to access a MongoDB from inside the container through a tunnel.

I presume, that I would have to do the following:

  1. Create a SSH tunnel from my terminal (how?) I am currently using SSHTunnelForwarder from within my python scripts.

  2. Get the IP and Port of the local endpoint of my tunnel (how?)

  3. Run my docker with --net=host host.docker.internal (is that enough? from what I understand host.docker.internal only resolves to my local IP, but how would it know the IP of the endpoint of my tunnel?

  4. How would I then set up my MongoDB client within the Docker container? I am using PyMongo and for setting up the client I need an IP (which would be the remote bind address?) and a port (which would be what?)

Cheers

Fabian Bosler
  • 2,310
  • 2
  • 29
  • 49
  • Just imagine the Docker container is a new machine with nothing installed. Install everything you need (for the OS you select for the container), including your SSH utils. Then get a shell into the container, and you'll be able to use the ssh utils you installed inside the container. – duhaime Jul 20 '18 at 17:29
  • Appreciate the answer. Doesn't really help unfortunately. As I said, I am using SSHTunnelForwarder from within my python scripts. This takes care of the of setting up the Tunnel and closing it after the script has finished. – Fabian Bosler Jul 20 '18 at 17:33
  • What value does the ssh tunnel add to your PyMongo connection? Why not just connect to the db on localhost (ie your container's host) directly? – duhaime Jul 21 '18 at 11:50
  • @Duhaine, that makes perfect sense for the deployed container (to production), from where it does not have to tunnel to production. However for testing (especially remote) the tunnel will still be required, and I'd love to be able to test the entire flow locally at least once. – Fabian Bosler Jul 22 '18 at 11:48
  • Can't you just write your test suite and run it inside the container, where your ORM will connect to Mongo on localhost? – duhaime Jul 22 '18 at 13:51
  • I guess that would work. Would require me to set up a local copy of the database thought, right? – Fabian Bosler Jul 23 '18 at 06:03
  • There are a few options. Your container could connect to a mongo on an external host through a uri, or your container could connect to mongo on its host's machine with a `volume` connection, or you could seed your db inside the container so it has no contact with the outside world... – duhaime Jul 23 '18 at 11:05

0 Answers0