Skip to content

MongoDB container not always initialising after clearing volumes #677

Description

@pharapeti

Problem

When trying to re-initialise MongoDB (IE: perform a clean wipe), it seems that the docker-entrypoint.sh is failing to detect a 'uninitialised' instance of Mongo.

Some times when I execute the following steps I do see the following message within the mongo container logs.

MongoDB init process complete; ready for start up.

Other times, it does not print the message above and therefore does not initialise.

Steps to reproduce

Steps to run

docker volume rm mongodb-data
docker volume rm mongodb_configdb_data

docker volume create mongodb-data
docker volume create mongodb_configdb_data

# When this runs, it shows that the `/data/db` directory is EMPTY
docker run --rm \
  -v mongodb-data:/data/db:Z \
  -v mongodb_configdb_data:/data/configdb:Z  \
   alpine ls -R /data/db

docker compose up -d -V mongo
docker container logs -f mongo

# NOTE: in the logs, the container has initialised successfully IF you see the following message
# "MongoDB init process complete; ready for start up."

Reference: docker-compose.yml

# other services...

services:
  mongo:
    image: mongo:5.0.8
    networks:
      - myNetwork
    volumes:
      - mongodb-data:/data/db:Z
      - mongodb_configdb_data:/data/configdb:Z
     environment:
       MONGO_INITDB_ROOT_USERNAME: someUsername
       MONGO_INITDB_ROOT_PASSWORD: somePassword
       MONGO_INITDB_DATABASE: someDbName
    entrypoint:
      - bash
      - -c
      - | # NOTE: This is coming from a Docker secret
          cp /usr/local/data/replica.key /usr/local/data/replication.container.key
          chmod 400 /usr/local/data/replication.container.key
          chown 999:999 /usr/local/data/replication.container.key
          exec docker-entrypoint.sh $$@
    command: "--bind_ip_all --replSet rs0 --keyFile /usr/local/data/replication.container.key"
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/${MONGO_DB_NAME} --quiet
      interval: 10s
      timeout: 30s
      retries: 10

Other info

My Docker volumes are NFS mounted volumes running in AWS EFS. When the MongoDB container starts and does not initialise - I see my EFS volume throughput spike and become pinned at 100% until I kill the now-stuck MongoDB container.

Image tag: mongo:5.0.8
Docker version: v25.0.1
Docker Compose version: v2.24.2

docker inspect mongodb-data

[
    {
        "CreatedAt": "2024-03-15T06:39:47Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/mongodb-data/_data",
        "Name": "mongodb-data",
        "Options": {
            "device": ":/mongodb-data",
            "o": "nfsvers=4.1,rsize=1048576,wsize=1048576,bg,hard,nolock,noatime,nointr,timeo=600,retrans=2,noresvport,addr=fs-redacted.efs.ap-southeast-2.amazonaws.com,rw",
            "type": "nfs"
        },
        "Scope": "local"
    }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions