Manoj Shevate Manoj Shevate -4 years ago 105
Bash Question

getting error while executing shell script for mongo db

I'm trying to create a

for managing mongo db and its default content.

here is docker file and related shell scripts -

DockerFile -

# Dockerizing MongoDB: Dockerfile for building MongoDB images
# Based on ubuntu:16.04, installs MongoDB following the instructions from:

FROM ubuntu:16.04

# Installation:
# Import MongoDB public GPG key AND create a MongoDB list file
RUN apt-key adv --keyserver hkp:// --recv EA312927
RUN echo "deb $(cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d= -f2)/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list

# Update apt-get sources AND install MongoDB
RUN apt-get update && apt-get install -y mongodb-org

# Create the MongoDB data directory
RUN mkdir -p /data/db

COPY ./scripts /data/scripts

COPY ./ /data/init/

RUN chmod +x /data/init/
RUN chmod +x /data/scripts/*

RUN /data/init/

# Expose port #27017 from the container to the host
EXPOSE 27017

# Set /usr/bin/mongod as the dockerized entry-point application
ENTRYPOINT [ "/usr/bin/mongod", "--rest"] -

# Start mongo db
/usr/bin/mongod --fork --logpath /data/mongodb.log

# Run all Create scripts
# FILES= data/scripts/*-create.js
# for f in $FILES; do /usr/bin/mongo $f; done

cd data/scripts

# Run all Insert scripts
FILES= ./*-insert.js
for f in $FILES; do /usr/bin/mongo $f; done

#Stop mongo db
/usr/bin/mongod --shutdown

and default-insert.js -

var db = db.getSiblingDB('demo')

db.names.insert({'name' : 'Manoj Shevate', 'id':1});

when I'm building docker image, I'm getting following error on first line of default-insert.js

syntax error near unexpected token "("

Not sure what exactly going wrong here.. need some expert advice!

Answer Source

Space--the final frontier. At least this looks fishy:

# Run all Insert scripts
FILES= ./*-insert.js

It makes FILES empty and tries to run the js file as a shell script, causing the error message (it's a shell one-shot variable assignment command like EDITOR=vi crontab -e). Use this instead:

for f in *-insert.js; do /usr/bin/mongo "$f"; done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download