Answer a question

I'm using docker-compose to containerize a flask API. The API connects to MongoDb to fetch data, so I included a link to it in the docker-compose.yml. Here is the docker-compose file:

app:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  volumes:
    - .:/app
// Link to db.
  links:
    - db
// db information
db:
  image: mongo:latest
  hostname: test_mongodb
  environment:
    - MONGO_INITDB_DATABASE=Exploit_Resources
    - MONGO_INITDB_ROOT_USERNAME=root
    - MONGO_INITDB_ROOT_PASSWORD=pass
  volumes:
    // Connection to init-db.js which will house data.
    - ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
  ports:
    - 27017:27017

The init-db.js has code similar to this:

db = db.getSiblingDB("name_of_db");
db.name_of_collection.drop();



db.EDB.insertMany([
    {
        "_id":"9",
        "file":"exploits/windows/dos/9.c",
        "name":"Apache 2.x - Memory Leak",
        "date":"2003-04-09",
        "author":"Matthew Murphy",
        "type":"dos",
        "platform":"windows",
        "port":"",
        "CVE":['CVE-2003-0132']
    },
    {
        "_id":"37060",
        "file":"exploits/windows/dos/37060.html",
        "name":"Microsoft Internet Explorer 11 - Crash (PoC) (1)",
        "date":"2015-05-19",
        "author":"Garage4Hackers",
        "type":"dos",
        "platform":"windows",
        "port":"",
        "CVE":[]
    },
    {
        "_id":"11",
        "file":"exploits/linux/dos/11.c",
        "name":"Apache 2.0.44 (Linux) - Remote Denial of Service",
        "date":"2003-04-11",
        "author":"Daniel Nystram",
        "type":"dos",
        "platform":"linux",
        "port":"",
        "CVE":['CVE-2003-0132']
    },
    {
        "_id":"13",
        "file":"exploits/windows/dos/13.c",
        "name":"Chindi Server 1.0 - Denial of Service",
        "date":"2003-04-18",
        "author":"Luca Ercoli",
        "type":"dos",
        "platform":"windows",
        "port":"",
        "CVE":[]
    },
]);

This should work, but running docker-compose up gives the following error:

db_1   | SyntaxError: "" string literal contains an unescaped line break :
db_1   | @/docker-entrypoint-initdb.d/init-db.js:60708:37
db_1   | failed to load: /docker-entrypoint-initdb.d/init-db.js
db_1   | exiting with code -3

Is there any way to resolve this error? I believe it is originating from the insertMany() method. I am fairly new to javascript, so I may be overlooking something obvious. Any help would be much appreciated.

Answers

I think the error is in your docker-compose file. Try to change it for the file below:

app:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  volumes:
    - .:/app
// Link to db.
  links:
    - db
// db information
db:
  image: mongo:latest
  hostname: test_mongodb
  environment:
    - MONGO_INITDB_DATABASE=Exploit_Resources
    - MONGO_INITDB_ROOT_USERNAME=root
    - MONGO_INITDB_ROOT_PASSWORD=pass
  volumes:
    // Connection to init-db.js which will house data.
    - ./init-db.js:/docker-entrypoint-initdb.d/init-db.js
  ports:
    - 27017:27017

I just removed :ro in the end of your entry-point path.

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐