Docker-Compose and Mongo -- Need help with starting image with an initialized database

I am beating my head against a wall with this. I’m trying to start a mongo db instance that will initialize a collection from scratch. I found this example and thought it would fix my purposes StackOverflow.

I’m still pretty new to docker and mongodb so this is all a bit overwhelming for me, but just trying to set this up elegantly.

After I run the image from docker-compose it bails out. I did verify that not including the startup-script that the mongodb server successfully runs and I am able to access the database.

My goal is to create a database called “DocNLP” with an empty collection called “ProcessedDocuments”.

From yml:

mongo:
    image: mongo
    restart: always
    ports:
    - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: pw
      MONGO_INITDB_DATABASE: DocNLP
    volumes:
     - ./NLPParserItems/Mongo/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

mongo-init.js:

let res = [
    db.auth('root', 'pw'),
    db.createCollection('ProcessedDocuments'),
    db.getCollection('ProcessedDocuments')
];

printjson(res)
PS E:\Workspace\DocNLPParser> docker-compose run --service-ports mongo
about to fork child process, waiting until server is ready for connections.
forked process: 27
2019-07-13T17:12:25.625+0000 I CONTROL  [main] ***** SERVER RESTARTED *****
2019-07-13T17:12:25.627+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] MongoDB starting : pid=27 port=27017 dbpath=/data/db 64-bit host=14b90a4baeed
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] db version v4.0.10
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] modules: none
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] build environment:
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-07-13T17:12:25.630+0000 I CONTROL  [initandlisten] options: { net: { bindIp: "127.0.0.1", port: 27017, ssl: { mode: "disabled" } }, processManagement: { fork: true, pidFilePath: "/tmp/docker-entrypoint-temp-mongod.pid" }, systemLog: { destination: "file", logAppend: true, path: "/proc/1/fd/1" } }
2019-07-13T17:12:25.630+0000 I STORAGE  [initandlisten]
2019-07-13T17:12:25.630+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-07-13T17:12:25.630+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-07-13T17:12:25.631+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1452M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-07-13T17:12:26.589+0000 I STORAGE  [initandlisten] WiredTiger message [1563037946:589485][27:0x7f4a9707da80], txn-recover: Set global recovery timestamp: 0
2019-07-13T17:12:27.111+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-07-13T17:12:27.817+0000 I CONTROL  [initandlisten]
2019-07-13T17:12:27.817+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-13T17:12:27.817+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-13T17:12:27.817+0000 I CONTROL  [initandlisten]
2019-07-13T17:12:27.818+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: 79a9a9df-ce9e-4fcd-93ea-7c9f086ffc79
2019-07-13T17:12:28.305+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2019-07-13T17:12:28.307+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: d6808a21-e2ff-477c-b7ad-fa177ae25d54
2019-07-13T17:12:28.771+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-07-13T17:12:28.772+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-07-13T17:12:28.772+0000 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: 878932d5-84c3-484a-9fa6-463501ed367c
child process started successfully, parent exiting
2019-07-13T17:12:28.809+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:55172 #1 (1 connection now open)
2019-07-13T17:12:28.809+0000 I NETWORK  [conn1] received client metadata from 127.0.0.1:55172 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.10" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04" } }
2019-07-13T17:12:28.812+0000 I NETWORK  [conn1] end connection 127.0.0.1:55172 (0 connections now open)
2019-07-13T17:12:28.853+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:55174 #2 (1 connection now open)
2019-07-13T17:12:28.854+0000 I NETWORK  [conn2] received client metadata from 127.0.0.1:55174 conn2: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.10" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04" } }
2019-07-13T17:12:28.947+0000 I STORAGE  [conn2] createCollection: admin.system.users with generated UUID: ae848008-a961-48be-beeb-564434ead22e
2019-07-13T17:12:29.958+0000 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-07-13T17:12:29.958+0000 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-07-13T17:12:30.300+0000 I COMMAND  [conn2] command admin.system.users appName: "MongoDB Shell" command: insert { insert: "system.users", bypassDocumentValidation: false, ordered: true, $db: "admin" } ninserted:1 keysInserted:2 numYields:0 reslen:45 locks:{ Global: { acquireCount: { r: 3, w: 3 } }, Database: { acquireCount: { W: 3 } }, Collection: { acquireCount: { w: 2 } } } storage:{} protocol:op_msg 1353ms
2019-07-13T17:12:30.300+0000 I COMMAND  [conn2] command admin.$cmd appName: "MongoDB Shell" command: createUser { createUser: "root", pwd: "xxx", roles: [ { role: "root", db: "admin" } ], digestPassword: true, writeConcern: { w: "majority", wtimeout: 600000.0 }, lsid: { id: UUID("90935d65-def3-4ce0-a52c-ef9a86370d49") }, $db: "admin" } numYields:0 reslen:38 locks:{ Global: { acquireCount: { r: 6, w: 4 } }, Database: { acquireCount: { W: 4 } }, Collection: { acquireCount: { w: 3 } } } storage:{} protocol:op_msg 1376ms
Successfully added user: {
		"user" : "root",
		"roles" : [
				{
						"role" : "root",
						"db" : "admin"
				}
		]
}
2019-07-13T17:12:30.303+0000 E -        [main] Error saving history file: FileOpenFailed: Unable to open() file /home/mongodb/.dbshell: Unknown error
2019-07-13T17:12:30.304+0000 I NETWORK  [conn2] end connection 127.0.0.1:55174 (0 connections now open)

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/mongo-init.js
2019-07-13T17:12:30.338+0000 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs
2019-07-13T17:12:30.338+0000 I COMMAND  [LogicalSessionCacheRefresh] command config.$cmd command: createIndexes { createIndexes: "system.sessions", indexes: [ { key: { lastUse: 1 }, name: "lsidTTLIndex", expireAfterSeconds: 1800 } ], $db: "config" } numYields:0 reslen:114 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2, W: 1 } }, Collection: { acquireCount: { w: 2 } } } storage:{} protocol:op_msg 1565ms
2019-07-13T17:12:30.340+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:55176 #3 (1 connection now open)
2019-07-13T17:12:30.340+0000 I NETWORK  [conn3] received client metadata from 127.0.0.1:55176 conn3: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.10" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04" } }
2019-07-13T17:12:30.342+0000 E -        [main] directory [/docker-entrypoint-initdb.d/mongo-init.js] doesn't have any *.js files
failed to load: /docker-entrypoint-initdb.d/mongo-init.js
2019-07-13T17:12:30.344+0000 I NETWORK  [conn3] end connection 127.0.0.1:55176 (0 connections now open)
1 Like

Have you found the solution?

did you find the solution? I was doing exactly the same thing here, startup mongodb on docker with empty collections created automatically via .js files. I am still looking for the solution.