0

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/

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.