2

I am trying to build mongo inside docker and I want to push database, collection and document inside the collection I tried with docker build and below my Dockerfile

FROM mongo


RUN mongosh mongodb://127.0.0.1:27017/demeter --eval 'db.createCollection("Users")'
RUN mongosh mongodb://127.0.0.1:27017/demeter --eval 'var document = {"_id": "61912ebb4b6d7dcc7e689914","name": "Test Account","email":"test@test.net", "role": "admin", "company_domain": "test.net","type": "regular","status": "active","createdBy": "61901a01097cb16e554f5a19","twoFactorAuth": false, "password": "$2a$10$MPDjDZIboLlD8xpc/RfOouAAAmBLwEEp2ESykk/2rLcqcDJJEbEVS"}; db.Users.insert(document);'


EXPOSE 27017

and using Docker Compose

version: '3.9'
services:
  web:
    build: 
      context: ./server
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
  demeter_db:
    image: "mongo"
    volumes:
      - ./mongodata:/data/db
    ports:
      - "27017:27017"
    command: mongosh mongodb://127.0.0.1:27017/demeter --eval 'db.createCollection("Users")'
  demeter_redis:
    image: "redis"

I want to add the below records because the Web Server is using them in backend. if there is a better way of doing it I would be thankful.

What I get is the below error

demeter_db_1     | Current Mongosh Log ID:  61dc697509ee790cc89fc7aa
demeter_db_1     | Connecting to:       mongodb://127.0.0.1:27017/demeter?directConnection=true&serverSelectionTimeoutMS=2000
demeter_db_1     | MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

Knowing when I connect to interactive shell inside mongo container and add them manually things works fine.

root@8b20d117586d:/# mongosh 127.0.0.1:27017/demeter --eval 'db.createCollection("Users")'
Current Mongosh Log ID: 61dc64ee8a2945352c13c177
Connecting to:      mongodb://127.0.0.1:27017/demeter?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:      5.0.5
Using Mongosh:      1.1.7

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting:
   2022-01-10T16:52:14.717+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2022-01-10T16:52:15.514+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

{ ok: 1 }
root@8b20d117586d:/# exit
exit

Cheers

MXA Music
  • 83
  • 6
  • make sure that mongo daemon process is running first – Abhishek D K Jan 10 '22 at 17:55
  • You usually can't create an image based on the standard Docker Hub database images with preloaded data, nor can you connect to a database from a Dockerfile. – David Maze Jan 10 '22 at 18:55
  • 1
    [How do I seed a mongo database using docker-compose?](https://stackoverflow.com/questions/31210973/how-do-i-seed-a-mongo-database-using-docker-compose) has a couple of suggestions on using a separate container to do the seeding, does that approach work for you? – David Maze Jan 10 '22 at 18:55
  • @DavidMaze Yeah this will work I will try it out – MXA Music Jan 10 '22 at 19:40

0 Answers0