j3d
j3d

Reputation: 9724

Unable to initialize MongoDB When the Container Starts

Here is my docker-compose.yaml:

version: '3.3'
mongo:
  build:
    context: '.'
    dockerfile: 'Dockerfile'
  environment:
    MONGO_INITDB_DATABASE: 'mydb'
  ports:
    - '27017:27017'
  volumes:
    - 'data-storage:/data/db'
  networks:
    mynet:

volumes:
  data-storage:

networks:
  mynet:

Here is my Dockerfile:

FROM mongo:latest
COPY ./initdb.js /docker-entrypoint-initdb.d/

And finally here is my inidb.js:

db.createCollection("strategyitems");
db.strategyitems.createIndex( {strategy: 1 }, { unique: false } );
db.strategyitems.createIndex( {strategy: 1, symbol: 1 }, { unique: true } );
db.strategyitems.insertMany([
  { strategy: "crypto", symbol: "btcusd", eval_period: 15, buy_booster: 8.0, sell_booster: 5.0, buy_lot: 0.2, sell_lot: 0.2 },
  { strategy: "crypto", symbol: "ethusd", eval_period: 15, buy_booster: 8.0, sell_booster: 5.0, buy_lot: 0.2, sell_lot: 0.2 },
  { strategy: "crypto", symbol: "neousd", eval_period: 15, buy_booster: 8.0, sell_booster: 5.0, buy_lot: 0.2, sell_lot: 0.2 }
]);

The container builds and starts successfully... but no way to get the db statements above executed.

If I log into the container, folder /docker-entrypoint-initdb.d/ contains initdb.js... so I'd expect the db get intialized.

Am I missing something?

Upvotes: 2

Views: 1443

Answers (1)

Sathyajith Bhat
Sathyajith Bhat

Reputation: 21851

So the supplied compose file doesn't work for me, I had to edit it to get it up & running (v18.06 CE), so heads-up on that.

version: '3.3'
services:
  mongo:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      MONGO_INITDB_DATABASE: 'mydb'
    ports:
      - '27017:27017'
    volumes:
      - 'data-storage:/data/db'
    networks:
      mynet:

volumes:
  data-storage:

networks:
  mynet:

Next, if you'd run docker-compose up before adding the initdb.js file and then stopped with docker-compose down, then docker-compose down stops the containers, but doesn't remove the volume

docker ps

| CONTAINER    | ID               | IMAGE                | COMMAND | CREATED | STATUS | PORTS | NAMES |         |                          |                    |
|--------------|------------------|----------------------|---------|---------|--------|-------|-------|---------|--------------------------|--------------------|
| c412bbd9a22b | lumberjack_mongo | docker-entrypoint.s… | 7       | minutes | ago    | Up    | 6     | minutes | 0.0.0.0:27017->27017/tcp | lumberjack_mongo_1 |

docker volume ls

| DRIVER |   | VOLUME | NAME                    |
|--------|---|--------|-------------------------|
| local  |   |        | lumberjack_data-storage |

docker-compose down
Removing lumberjack_mongo_1 ... done
Removing network lumberjack_mynet

docker volume ls

| DRIVER |   | VOLUME | NAME                    |
|--------|---|--------|-------------------------|
| local  |   |        | lumberjack_data-storage |

The problem arises when docker-compose up is run when the volume exists - Docker mounts the volume before the container starts up. Mongo does some pre-checks and if it finds that the directories are present, then skips the initdb sequence.

If you remove the volume after docker-compose down and do a docker-compose up, the volume will be created from scratch, the pre-check finds nothing and initializes the mongodb

docker volume rm lumberjack_data-storage
lumberjack_data-storage

docker-compose up

Creating network "lumberjack_mynet" with the default driver
Creating volume "lumberjack_data-storage" with default driver
Creating lumberjack_mongo_1 ... done
Attaching to lumberjack_mongo_1

[....]
mongo_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/initdb.js
mongo_1  | 2018-08-04T18:08:47.699+0000 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }

mongo_1  | 2018-08-04T18:08:47.745+0000 I NETWORK  [conn2] received client metadata from 127.0.0.1:45324 conn2: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.0" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04" } }
mongo_1  | 2018-08-04T18:08:47.747+0000 I STORAGE  [conn2] createCollection: initdb.strategyitems with generated UUID: 585edb14-bc63-4879-bc5d-504867fb5e12
mongo_1  | 2018-08-04T18:08:47.851+0000 I INDEX    [conn2] build index on: initdb.strategyitems properties: { v: 2, key: { strategy: 1.0 }, name: "strategy_1", ns: "initdb.strategyitems" }
mongo_1  | 2018-08-04T18:08:47.851+0000 I INDEX    [conn2]       building index using bulk method; build may temporarily use up to 500 megabytes of RAM
mongo_1  | 2018-08-04T18:08:47.852+0000 I INDEX    [conn2] build index done.  scanned 0 total records. 0 secs
mongo_1  | 2018-08-04T18:08:47.881+0000 I INDEX    [conn2] build index on: initdb.strategyitems properties: { v: 2, unique: true, key: { strategy: 1.0, symbol: 1.0 }, name: "strategy_1_symbol_1", ns: "initdb.strategyitems" }
mongo_1  | 2018-08-04T18:08:47.881+0000 I INDEX    [conn2]       building index using bulk method; build may temporarily use up to 500 megabytes of RAM
mongo_1  | 2018-08-04T18:08:47.882+0000 I INDEX    [conn2] build index done.  scanned 0 total records. 0 secs
mongo_1  | 2018-08-04T18:08:47.886+0000 I NETWORK  [conn2] end connection 127.0.0.1:45324 (0 connections now open)
[....] 
mongo_1  | MongoDB init process complete; ready for start up.
mongo_1  |
mongo_1  | 2018-08-04T18:08:48.933+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongo_1  | 2018-08-04T18:08:48.939+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=e90c80083360

Upvotes: 1

Related Questions