0

I am using docker to containerize a Flask API. The Flask API is accessing MongoDB using the "links" keyword in the docker-compose.yml file shown below:

app:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  volumes:
    - .:/app
  #Linking db.
  links:
    - db

db:
  image: mongo:latest
  hostname: test_mongodb
  environment:
    - MONGO_INITDB_DATABASE=Exploit_Resources
    - MONGO_INITDB_ROOT_USERNAME=root
    - MONGO_INITDB_ROOT_PASSWORD=pass
  # Data provider.
  volumes:
    - ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
  ports:
    - 27017:27017

The link is working fine, and the file given under volumes (init-db.js) is supposed to provide the Mongo container with data. Here is my init-db.js file:

db = db.getSiblingDB("Exploit_Resources");
db.EDB.drop();

db.EDB.insertMany([
   // Insert data here.
]);

The data I want to provide MongoDB comes from a remote csv file. I tried using PapaParse to access and feed the data, but I was having some difficulties importing it.

I tried this code inside the insertMany method:

const {StringStream} = require("scramjet");
const request = require("request");

request.get("https://srv.example.com/main.csv")   // fetch csv
    .pipe(new StringStream())                       // pass to stream
    .CSVParse()                                   // parse into objects
    .consume(object => console.log("Row:", object))  // do whatever you like with the objects
    .then(() => console.log("all done"))

I got the code from here. It did not work because a ReferenceError was thrown, saying that 'require' is not defined.

Is there a way to fix this code or another way to get data from a remote csv file and provide it to the MongoDB container from the init-db.js?

Incepnizer
  • 27
  • 4
  • 1
    Bit of an aside, but links have been deprecated in favour of networks now, it's probably best to change https://docs.docker.com/compose/compose-file/compose-file-v2/#links – TheQueenIsDead Aug 16 '21 at 02:20
  • Please clarify "some difficulties" – OneCricketeer Aug 16 '21 at 03:46
  • @OneCricketeer Sorry, I edited the question and added that. The tldr of it is that a ReferenceError was thrown saying 'require' is not defined. – Incepnizer Aug 16 '21 at 20:36
  • I see... I highly doubt RequireJS is available from the Javascript evaulator that Mongo is running – OneCricketeer Aug 16 '21 at 21:31
  • How about you make your python code populate the database when it starts up? Or wrap your `docker-compose up` command in a script that downloads the data, starts the database, inserts it, etc? – OneCricketeer Aug 16 '21 at 21:33

0 Answers0