1

I did setup a docker-compose file that connects my app to a mongoDB database. My problem is that the database seems to never be initialized at first. My script is not executed and even tho' I can send some requests to the container, I only get connection refused errors due to authentification.

I did follow exactly this thread and I don't know what I'm missing out! (the db folder is on the same level as my docker-compose.yml)

Looking for some help on this one, thanks!

edit: None of the logs I did put in the init script are showing in the console, that's how I went to the conclusion that the file is not executed at all.

Here is my docker-compose file:


services:
  mongo:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
      MONGO_INITDB_DATABASE: test
    volumes:
      - ./db:/docker-entrypoint-initdb.d
      - ./db-data:/data/db
    ports:
      - 27017:27017
    networks:
      - api1

  app:
    restart: always
    build:
      context: .
    environment:
      DB_HOST: localhost
      DB_PORT: 27017
      DB_NAME: test
      DB_USER: developer
      DB_PASS: developer
      PORT: 3000
    ports:
      - 3000:3000
    networks:
      - api1
    depends_on:
      - mongo
    command: npm start
networks:
  api1:
    driver: bridge

Here is my init scipt:

/* eslint-disable no-undef */

try {
  print("CREATING USER")
  db.createUser(
    {
      user: "developer",
      pwd: "developer",
      roles: [{ role: "readWrite", db: "test" }]
    }
  );
} catch (error) {
  print(`Failed to create developer db user:\n${error}`);
}

And my dockerfile:

FROM node:10 as builder

RUN mkdir /home/node/app
WORKDIR /home/node/app

# Install dependencies
COPY package.json yarn.lock ./
RUN yarn install && yarn cache clean

# Copy source scripts
COPY . .

RUN yarn build

FROM node:10-alpine

RUN mkdir -p /home/node/app
WORKDIR /home/node/app

COPY --from=builder --chown=node:node /home/node/app .

USER node
EXPOSE 3000
CMD ["node", "./build/bundle.js"]
Alex Mass
  • 171
  • 2
  • 11
  • 9
    The code in the docker-entrypoint-init.d folder is only executed if the database has never been initialized before (I.e. if the /data/dB folder is empty. As such, delete the mounted ./db folder in your host machine (it will get recreated automatically) before doing docker-compose up. This will cause your scripts to be run. – camba1 Jun 13 '20 at 01:44
  • @camba1 - well spotted. This was a problem I was having. I had to clear it - docker volume prune. This practically saved me after hours of debugging. – Steve Tomlin Mar 25 '23 at 09:35

1 Answers1

0

I got the same problem when set up a mongodb by docker compose. After a few hours try, I finally make the .js method work, I was try in to work out the .sh way. Here is what set up in the docker-compose.yml

#docker-compose.yml
mongodb_dev:
  image: mongo:6.0.5 # version matters, according to the offical doc
  container_name: mongo_db_container
  restart: always
  environment:
    TZ: "Asia/Hong_Kong"
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: root
    MONGO_INITDB_DATABASE: init_db
  ports:     
    - 27017:27017
  volumes:
    - ./app-database/data/db:/data/db
    - ./app-database/data/log:/var/log/mongodb
    - ./app-database/mongod.conf:/etc/mongod.conf.  
    - ./app-database/init.d:/docker-entrypoint-initdb.d # !!important, map the folder instead of the any actual file
  networks:
    - leafiot_tree_dev

And here is how I structure the files.

file structure

I found the work around in Issue here and found some important information in the official doc here initializing-a-fresh-instance

When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongosh (mongo on versions below 6) using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.

lang sun
  • 21
  • 4