Starting mongo docker with docker-compose

  docker, docker-compose, mongodb

My docker container is running through docker-compose. The init script appears to be creating the user I want to login with because of this log line:

mongo    | Successfully added user: { "user" : "newUser", "roles" : [ "readWrite" ] }
mongo    | bye
mongo    | Error saving history file: FileOpenFailed Unable to open() file /home/mongodb/.dbshell: No such file or directory
mongo    | {"t":{"$date":"2021-01-29T13:45:03.397+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn3","msg":"Connection ended","attr":{"remote":"127.0.0.1:60066","connectionId":3,"connectionCount":0}}

However since I’m seeing the "unable to open file /home/mongodb/.dbshell", it appears I cannot write to my data directory.

Here’s the docker-compose for mongo:

mongo:
  image: mongo:4.4.3
  container_name: mongo
  restart: unless-stopped
  environment:
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: root1
    MONGO_INITDB_DATABASE: admin
  command: mongod --port 27017
  ports:
    - '27017:27017'
  volumes:
    - ./data/mongodb:/data/db
    - ./configs/mongodb/mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro

Is setting the "rw" on the volume for the data directory the right way to do it? The actual permissions on that folder locally are all "-rw——-" – but owned by systemd-coredump. This happens regardless if I run the mongo-init.sh script or not. What could cause this corruption?

That core dump is obviously preventing creation of the user.

This is my mongo-init.sh file. I’m not sure if I should use "newUserDB" or "admin" in the call to getSiblingDB as well.

#!/usr/bin/env bash
set -eu
mongo -- "admin" <<EOF
    var rootUser = 'root';
    var rootPassword = 'root1';
    var admin = db.getSiblingDB('admin');
    admin.auth(rootUser, rootPassword);

    var user = 'newUser';
    var passwd = 'newUserPass';
    db.createUser({user: user, pwd: passwd, roles: ["readWrite"]});
EOF

Source: Docker Questions

LEAVE A COMMENT