意外令牌附近的语法错误"("执行mongo db的shell脚本时)

时间:2017-05-22 18:46:40

标签: mongodb shell docker dockerfile

我尝试创建Dockerfile来管理mongo db及其默认内容。

这里是docker文件和相关的shell脚本 -

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

default-insert.js -

var db = db.getSiblingDB('demo')

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

当我构建泊坞窗图片时,我在default-insert.js的第一行收到以下错误

syntax error near unexpected token "("

不确定这里到底出了什么问题..需要一些专家建议!

1 个答案:

答案 0 :(得分:2)

太空 - 最后的边疆。至少这看起来很可疑:

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

它使FILES为空并尝试将js文件作为shell脚本运行,从而导致错误消息(它是一个shell一次性变量赋值命令,如EDITOR=vi crontab -e)。请改用:

for f in *-insert.js; do /usr/bin/mongo 127.0.0.1:27017 "$f"; done