James Jenkinson
James Jenkinson

Reputation: 1661

Connecting to mongo from a different container - connection refused

I'm trying to create a docker file that mongo running in a different container, to execute commands from a file, however the second container receives a connection refused error:

mongo-testdata_1  | MongoDB shell version: 3.2.9
mongo-testdata_1  | connecting to: mongo:27017/admin
mongo-testdata_1  | 2019-12-15T15:57:30.067+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
mongo-testdata_1  | 2019-12-15T15:57:30.067+0000 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
mongo-testdata_1  | @./testData.js:1:20
mongo-testdata_1  | 
mongo-testdata_1  | failed to load: ./testData.js

How would I be able to connect to the mongoDB running on the first container, using the mongo cli in the second?

commands used

docker-compose up -d mongo
docker exec my-test-mongo mongo --eval "db.createUser({ user: 'admin', pwd: 'password', roles: [ 'dbAdmin', 'readWrite' ] });" admin
docker-compose up --build mongo-testdata

mongo.dockerfile

FROM mongo:3.2.9

ADD testData.js .

ENTRYPOINT ["mongo"]

docker-compose.yml

version: '2'
services:
  mongo-testdata:
    build:
      context: .
      dockerfile: mongo.dockerfile
    command: --host mongo --port 27017 --username='admin' --password='password' admin ./testData.js
    depends_on:
      - mongo
    networks:
      - backend

  mongo:
    image: mongo:3.2.9
    container_name: my-test-mongo
    ports:
      - 27017:27017
    networks:
      - backend

networks:
  backend:
    driver: bridge

testData.js

const myDb = new Mongo().getDB('mydb');

myDb.getCollection('test').insert({
  foo: 'bar',
});

Upvotes: 0

Views: 194

Answers (2)

Jan
Jan

Reputation: 51

You need to specify host and port when you connect to mongo in testData.js.

new Mongo(<host:port>)

In your case this would be new Mongo('mongo').

https://docs.mongodb.com/v3.2/reference/method/Mongo/#Mongo

Instantiation Options Use the constructor without a parameter to instantiate a connection to the localhost interface on the default port.

Pass the parameter to the constructor to instantiate a connection to the and the default port.

Pass the <:port> parameter to the constructor to instantiate a connection to the and the .

Upvotes: 1

Adiii
Adiii

Reputation: 60074

The only depends_on will not work, as the mongo DB container process is not ready to accept to container while CLI get connection refused. You can try two option - simply put sleep for some second the DB container will ready to accept connection - Increase wait timeout in mongo shell connection

Here is way to deal with connection refused by adding 10 second seelp in import.

version: '2'
services:
  mongo_import:
    image: mongo
    command: /bin/bash -c "sleep 10 && mongo --host mongo --port 27017 --username='admin' --password='password' admin /root/testData.js"
    volumes:
      - ./testData.js:/root/testData.js
    depends_on:
      - mongo

  mongo:
    image: mongo
    container_name: my-test-mongo
    ports:
      - 27017:27017

Btw the above task you can perform in the same container without the need of any cli container.

Initializing a fresh instance

When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.

Initializing a fresh instance

Upvotes: 0

Related Questions