Run a ruby script in a docker container which connects to mongodb localhost port - ruby

I am a newbie with docker and trying my hands onto it. I am facing a minor problem, your help in any way would be appreciated!
I have a ruby script in which I am connecting to my localhost port which is assigned to my mongodb database. I am using MongoClient to connect to the database from the script:
clientDB = Mongo::Client.new(["localhost:37017"], :database => 'Database', :user => 'user', :password => 'password')
or
clientDB = Mongo::Client.new(["127.0.0.1:37017"], :database => 'Database', :user => 'user', :password => 'password')
If I run the script using ruby monthly_count_script.rb command, it works perfectly but as I am exploring Docker, I want to run that script into a container. So to run a container I am using following command
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/app -v "$PWD"/../gems:/usr/local/bundle -w /usr/src/app --net=host ruby ruby monthly_count_script.rb
Unfortunately I am keep getting following error and I don't know why it is not able to connect to the localhost 37017 port even though my database is working properly and I am able to connect to the database using Robomongo or mongodb console.
This is the error log:
D, [2018-06-04T14:15:11.527381 #1] DEBUG -- : MONGODB | Topology type 'single' initializing.
D, [2018-06-04T14:15:11.527639 #1] DEBUG -- : MONGODB | Server 127.0.0.1:37017 initializing.
D, [2018-06-04T14:15:11.529252 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:11.530774 #1] DEBUG -- : MONGODB | Topology type 'single' initializing.
D, [2018-06-04T14:15:11.531058 #1] DEBUG -- : MONGODB | Server 127.0.0.1:37017 initializing.
D, [2018-06-04T14:15:11.532518 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:12.032037 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:12.533348 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
D, [2018-06-04T14:15:13.036087 #1] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:37017
Looking forward to your help/guidance.
FYI I was following this documentation for running a ruby script in docker:
https://docs.docker.com/samples/library/ruby/

Local ports are not available from inside Docker containers, you only can connect to ports opened in the container or from other containers with --link.
To connect to host's ports you should use the IP for the docker network's gateway.
To get the gateway IP of your container run:
docker inspect --format='{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}'
Usually it will return 172.17.0.1 (I repeat, this answer is only valid for default network configurations, assuming that the container is connected to a bridge network)
Then you can update the mongo client config:
clientDB = Mongo::Client.new(["172.17.0.1:37017"], :database => 'Database', :user => 'user', :password => 'password')

You can access mongodb on your computer from docker with host.docker.internal. This (or a similar, can't remember) SO-thread helped me.

Related

Getting Error running psql Command for PostgreSQL

I have installed the Postgres on my windows 10 PC and I set the environment variable also after when I run psql command it shows following error:
could not connect to server:
Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5433?
I have checked the port no service is running on 5433 and also tried changing it but getting the same error.I also tried installing Postgres outside Program File (thought maybe it's permission related issue) but still problem persist.I installed Postgres 13

Can't connect via "vagrant ssh" to vagrant box created with "vagrant package"

A vagrant box which was created using "vagrant package" cannot be connected to using "vagrant ssh".
Note - I am using libvirt and QEMU, not Virtualbox.
I took a working vagrant instance and created a vagrant box using
vagrant package --output abc.box
vagrant box add abc.box --name abc
I then created an empty directory and generated the Vagrantfile using
vagrant init
I edited the Vagrantfile and set config.vm.box = "abc"
I then tried a "vagrant up", and it hangs on the SSH connection
I ran a "vagrant -ssh-config" which returns settings. I was able to use the IP and key listed there to connect to the instance.
Can anybody tell me what the correct process is for getting this kind of instance to start properly? Is it by design that the insecure access isn't preserved in the packaged box? Should I just be installing my own SSH keys and logging in using "ssh" instead of "vagrant ssh"?
I expected the "vagrant up" to complete, but it never returned the prompt. Attempts to connect to it using "vagrant ssh" from another terminal didn't work.
I turned on debugging and saw the log entries listed below.
DEBUG ssh: == Net-SSH connection debug-level log START ==
DEBUG ssh: D, [2019-09-03T18:00:51.803268 #4837] DEBUG -- net.ssh.transport.session[1ae7be8]: establishing connection to 192.168.121.230:22
D, [2019-09-03T18:00:51.804016 #4837] DEBUG -- net.ssh.transport.session[1ae7be8]: connection established
I, [2019-09-03T18:00:51.804202 #4837] INFO -- net.ssh.transport.server_version[1ae6e64]: negotiating protocol version
D, [2019-09-03T18:00:51.804260 #4837] DEBUG -- net.ssh.transport.server_version[1ae6e64]: local is `SSH-2.0-Ruby/Net::SSH_4.2.0 x86_64-linux'
D, [2019-09-03T18:00:51.814838 #4837] DEBUG -- net.ssh.transport.server_version[1ae6e64]: remote is `SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3'
I, [2019-09-03T18:00:51.815157 #4837] INFO -- net.ssh.transport.algorithms[1adcbe4]: sending KEXINIT
D, [2019-09-03T18:00:51.815403 #4837] DEBUG -- socket[1ae792c]: queueing packet nr 0 type 20 len 1156
D, [2019-09-03T18:00:51.815504 #4837] DEBUG -- socket[1ae792c]: sent 1160 bytes
DEBUG ssh: == Net-SSH connection debug-level log END ==
INFO retryable: Retryable exception raised: #<Errno::ECONNRESET: Connection reset by peer - recvfrom(2)>
INFO ssh: Attempting to connect to SSH...
INFO ssh: - Host: 192.168.XXX.XXX
INFO ssh: - Port: 22
INFO ssh: - Username: vagrant
INFO ssh: - Password? false
INFO ssh: - Key Path: ["/home/XXXXX/.vagrant.d/insecure_private_key"]
DEBUG ssh: - connect_opts: {:auth_methods=>["none", "hostbased", "publickey"], :config=>false, :forward_agent=>false, :send_env=>false, :keys_only=>true, :verify_host_key=>false, :password=>nil, :port=>22, :timeout=>15, :user_known_hosts_file=>[], :verbose=>:debug, :encryption=>["aes128-cbc", "3des-cbc", "blowfish-cbc", "cast128-cbc", "aes192-cbc", "aes256-cbc", "rijndael-cbc#lysator.liu.se", "idea-cbc", "arcfour128", "arcfour256", "arcfour", "aes128-ctr", "aes192-ctr", "aes256-ctr", "cast128-ctr", "blowfish-ctr", "3des-ctr", "none"], :logger=>#<Logger:0x00000000034d08c0 #level=0, #progname=nil, #default_formatter=#<Logger::Formatter:0x00000000034d0848 #datetime_format=nil>, #formatter=nil, #logdev=#<Logger::LogDevice:0x00000000034d07f8 #shift_period_suffix=nil, #shift_size=nil, #shift_age=nil, #filename=nil, #dev=#<StringIO:0x00000000034d0938>, #mon_owner=nil, #mon_count=0, #mon_mutex=#<Thread::Mutex:0x00000000034d07a8>>>, :keys=>["/home/XXXXXX/.vagrant.d/insecure_private_key"]}```

OpenShift rhc port-forward add port

I am new to OpenShift, and trying to run elasticsearch in a DIY cartridge. The elasticsearch app seems to be working, but I cannot connect to the 3306 port which it needs for node to node communication.
When I run the
$ rhc port-forward -a myapp
Forwarding ports ...
To connect to a service running on OpenShift, use the local address
Service Local OpenShift
------- -------------- ------------------------
java 127.0.0.1:3306 => 127.10.49.101:3306
java 127.0.0.1:9200 => 127.10.49.101:9200
But when I run my indexing groovy/grails app that uses an elasticsearch plugin to index data, it cannot connect to the OpenShift elasticsearch node. It should be using port 3306 to connect to the OpenShift elasticsearch node. The app config has
client.node = 'node'
client.hosts = [ [host:'127.0.0.1', port:3306 ] ]
Note that my indexing app worked fine when elasticsearch was run locally, but it does not connect to elasticsearch running on OpenShift.

Couldn't connect to server 127.0.0.1:27017 on Windows 7

I have problem with connecting to the server on mongodb:
mongo
MongoDB shell version: 3.0.1
connecting to: test
Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:179
exception: connect failed
Set up the MongoDB environment (MongoDB’s default data directory path is \data\db):
md \data\db
Start MongoDB (Assuming MongoDB is at "C:\mongodb")
C:\mongodb\bin\mongod.exe
Connect to MongoDB
C:\mongodb\bin\mongo.exe
More Details here.
Run the mongod server:
mongod --dbpath /root/hemant/mongodb_data/data/db
Once the server started listening to the port 27017.
Run the mongo.exe
You might get an error like below:
Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:179
exception: connect failed
Restart the machine, this works in my case.

Run MySQL query over multiple SSH tunnels?

I have a situation somewhat similar to "How to create a ssh tunnel in ruby and then connect to mysql server on the remote host."
From my local machine, I want to run a query against a production MySQL database host.
My local machine cannot directly connect to the database host. However, if I SSH to a gateway machine, I can run the MySQL client and connect to the database host.
I'm trying to figure out how to programmatically to run a query from my machine that is forwarded to the gateway machine, which forwards it to the database server, and have results returned. Basically I want to have LOCAL => GATEWAY => DB_HOST forwarding.
I have a hacky solution that runs ssh.exec and MySQL on the command line, shown below, but, I'm trying to find a way that does port forwarding twice.
I tried, but haven't been successful so far.
require 'rubygems'
require 'mysql2'
require 'net/ssh/gateway'
gateway = Net::SSH::Gateway.new(
$gateway_host, $gateway_user,:password => $gateway_pass)
# This works
ssh = gateway.ssh($db_host, $db_login_user)
data = ssh.exec!("mysql -u#{$db_user} -p#{$db_pass} #{$db_name} -e '#{$sql_query}'")
puts "#{data.class} is the class type"
puts data
# Want to do something like this with port forwarding
# client = Mysql2::Client.new(:host => $db_host,
# :username => $db_user,
# :password => $db_pass,
# :database => $db_name,
# :port => port)
# results = client.query($sql_query)
puts "end stuff"
ssh.close
Any suggestions?
Your diagram lays it out fairly well, you would need a tunnel from the Gateway to the Db_Host; and a second tunnel from your local machine to the gateway. The two tunnels would effectively connect your local machine to the db_host by way of the gateway.
Here's a reference specific to tunneling MySQL over SSH

Resources