Manoj Shevate Manoj Shevate - 1 year ago 110
Bash Question

syntax error near unexpected token "(" while executing shell script for mongo db

I'm trying to create a

Dockerfile
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:
# http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

FROM ubuntu:16.04

# Installation:
# Import MongoDB public GPG key AND create a MongoDB list file
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
RUN echo "deb http://repo.mongodb.org/apt/ubuntu $(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 ./run.sh /data/init/run.sh

RUN chmod +x /data/init/run.sh
RUN chmod +x /data/scripts/*

RUN /data/init/run.sh

# 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"]


Run.sh -

# 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 127.0.0.1:27017/demo $f; done

cd data/scripts

# Run all Insert scripts
FILES= ./*-insert.js
for f in $FILES; do /usr/bin/mongo 127.0.0.1:27017 $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 127.0.0.1:27017 "$f"; done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download