Docker Community Forums

Share and learn in the Docker community.

Docker-compose: unable to connect to MongoDB from another container in the same network

docker

(Naftis) #1

I’m trying to build this scenario with docker-compose:

1.a MongoDB service;
2.a run-once service, used to seed the MongoDB with a database;
3.an ASP.NET Core 2.1 web app accessing the MongoDB database.

Omitting nr.3 (as I did in this post) should make it even easier, yet I cannot have services 2 (and 3) connect to MongoDB. Probably I’m missing something obvious, could anyone give a hint?

Here is my docker-compose:

version: '3.4'

services:
  mongo:
    image: mongo
    container_name: mongo
    environment:
      - MONGO_DATA_DIR=/data/db
      - MONGO_LOG_DIR=/dev/null
    ports:
      - 27017:27017
    command: mongod --smallfiles --logpath=/dev/null # --quiet
    networks:
      - samplenetwork

  mongoseed:
    build: ./MongoSeed
    depends_on:
      - mongo
    networks:
      - samplenetwork

  sampleapi:
    image: myrepo:sampleapi
    environment:
      - ASPNETCORE_ENVIRONMENT=Production
    ports:
      - 60304:60304
    depends_on:
      - mongo
      - mongoseed
    networks:
      - samplenetwork

networks:
  samplenetwork:
    driver: bridge

The MongoSeed Dockerfile is just the mongo image with a CMD which runs mongorestore, connecting it to a host named after the mongo service in the composer (I tried with the default 127.0.0.1, but nothing changed):

FROM mongo

COPY mydb.tar.gz /mydb.tar.gz
CMD mongorestore --host mongodb://mongo:27017 --archive=/mydb.tar.gz --gzip

The mydb file is located in the same directory of this Dockerfile, anyway the issue is that it’s not connecting to the MongoDB service. Yet, this seems to be running, as it says waiting for connections on port 27017 just before the seed service is started.

When I docker compose up (I commented out the whole sampleapi service section: this should allow anyone to quickly repro the issue), I get this:

Starting mongo ... done
Starting sample_mongoseed_1 ... done
Attaching to mongo, sample_mongoseed_1
mongo        | 2018-06-14T13:11:42.673+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=8bf3032dbd8e
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten] db version v3.6.5
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten] git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten] modules: none
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten] build environment:
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten]     distmod: debian81
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongo        | 2018-06-14T13:11:42.674+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true } }
mongo        | 2018-06-14T13:11:42.674+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongo        | 2018-06-14T13:11:42.674+0000 I STORAGE  [initandlisten] 
mongo        | 2018-06-14T13:11:42.674+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongo        | 2018-06-14T13:11:42.674+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongo        | 2018-06-14T13:11:42.674+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1454M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),cache_cursors=false,log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
mongo        | 2018-06-14T13:11:44.203+0000 I STORAGE  [initandlisten] WiredTiger message [1528981904:203946][1:0x7fdcbcf8ba00], txn-recover: Main recovery loop: starting at 1/16000
mongo        | 2018-06-14T13:11:44.434+0000 I STORAGE  [initandlisten] WiredTiger message [1528981904:434276][1:0x7fdcbcf8ba00], txn-recover: Recovering log 1 through 2
mongo        | 2018-06-14T13:11:44.617+0000 I STORAGE  [initandlisten] WiredTiger message [1528981904:617864][1:0x7fdcbcf8ba00], txn-recover: Recovering log 2 through 2
mongo        | 2018-06-14T13:11:44.759+0000 I STORAGE  [initandlisten] WiredTiger message [1528981904:759836][1:0x7fdcbcf8ba00], txn-recover: Set global recovery timestamp: 0
mongo        | 2018-06-14T13:11:44.856+0000 I CONTROL  [initandlisten] 
mongo        | 2018-06-14T13:11:44.856+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongo        | 2018-06-14T13:11:44.856+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongo        | 2018-06-14T13:11:44.856+0000 I CONTROL  [initandlisten] 
mongo        | 2018-06-14T13:11:44.896+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo        | 2018-06-14T13:11:44.899+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
mongoseed_1  | 2018-06-14T13:11:47.253+0000 Failed: error connecting to db server: no reachable servers
sample_mongoseed_1 exited with code 1