Home > Blockchain >  Cannot create a mongo database with docker
Cannot create a mongo database with docker

Time:06-02

I'm having trouble creating a mongo database using the docker-compose command. Docker desktop tells me that everything is up and running including the db, but all I get is the standard 'admin, config, local' not the db I want to create. Here's my docker-compose.yaml

version: '3'

services:

  app:
    build: ./
    entrypoint: ./.docker/entrypoint.sh
    ports:
    - 3000:3000
    volumes:
    - .:/home/node/app
    depends_on:
    - db

  db:
    image: mongo:4.4.4
    restart: always
    volumes:
      - ./.docker/dbdata:/data/db
      - ./.docker/mongo:/docker-entrypoint-initdb.d
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=root
      - MONGO_INITDB_DATABASE=nest
  
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_SERVER=db
      - ME_CONFIG_MONGODB_AUTH_USERNAME=root
      - ME_CONFIG_MONGODB_AUTH_PASSWORD=root
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=root
    depends_on:
    - db

my init.js inside .docker/mongo

db.routes.insertMany([
  {
    _id: "1",
    title: "Primeiro",
    startPosition: {lat: -15.82594, lng: -47.92923},
    endPosition: {lat: -15.82942, lng: -47.92765},
  },
  {
    _id: "2",
    title: "Segundo",
    startPosition: {lat: -15.82449, lng: -47.92756},
    endPosition: {lat: -15.82776, lng: -47.92621},
  },
  {
    _id: "3",
    title: "Terceiro",
    startPosition: {lat: -15.82331, lng: -47.92588},
    endPosition: {lat: -15.82758, lng: -47.92532},
  }
]);

and my dockerfile

FROM node:14.18.1-alpine

RUN apk add --no-cache bash

RUN npm install -g @nestjs/cli

USER node

WORKDIR /home/node/app


and this is the 'error' log I get from docker when I run the nest container with mongodb, nest app and mongo express(there is actually a lot more but SO keeps thinking that it is spam for some reason.

about to fork child process, waiting until server is ready for connections.


Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

Error saving history file: FileOpenFailed Unable to open() file /home/mongodb/.dbshell: No such file or directory

{"t":{"$date":"2022-06-01T19:39:15.542 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn2","msg":"Connection ended","attr":{"remote":"127.0.0.1:39304","connectionId":2,"connectionCount":0}}


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.js

{"t":{"$date":"2022-06-01T19:39:15.683 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:39310","connectionId":3,"connectionCount":1}}

{"t":{"$date":"2022-06-01T19:39:15.684 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn3","msg":"client metadata","attr":{"remote":"127.0.0.1:39310","client":"conn3","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.4"},"os":{"type":"Linux","name":"Ubuntu","architecture":"x86_64","version":"18.04"}}}}

{"t":{"$date":"2022-06-01T19:39:15.701 00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"conn3","msg":"createCollection","attr":{"namespace":"nest.routes","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"f689868e-af6d-4ec6-b555-dcf520f24788"}},"options":{}}}

{"t":{"$date":"2022-06-01T19:39:15.761 00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"conn3","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"nest.routes","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}

uncaught exception: ReferenceError: colection is not defined :

@/docker-entrypoint-initdb.d/init.js:23:1

failed to load: /docker-entrypoint-initdb.d/init.js

exiting with code -3



this is what running docker-compose ps shows

NAME                       COMMAND                  SERVICE             STATUS              PORTS     
nest-api-app-1             "./.docker/entrypoin…"   app                 running             0.0.0.0:3000->3000/tcp
nest-api-db-1              "docker-entrypoint.s…"   db                  running             27017/tcp 
nest-api-mongo-express-1   "tini -- /docker-ent…"   mongo-express       running             0.0.0.0:8081->8081/tcp

this what my docker desktop shows

CodePudding user response:

The MongoDB container only creates a database if no database already exists. You probably already have one, which is why a new database isn't created and your initialization script isn't run.

Delete the contents of ./.docker/dbdata on the host. Then start the containers with docker-compose and Mongo should create your database for you.

  • Related