0

Making a Dockerfile to bundle a microservices project with a MongoDB instance. I get an error about EOF missing in my script. The code is based on the official MongoDB entry-point script.

ENTRYPOINT echo '#!/bin/bash\n\
    pidfile="${TMPDIR:-/tmp}/docker-entrypoint-temp-mongod.pid" \n\
    rm -f "$pidfile" \n\
    mongod --logpath /var/log/mongodb.log --dbpath /data/db/ --fork --pidfilepath "$pidfile" \n\
    mongo=( mongo --host 127.0.0.1 --port 27017 --quiet ) \n\
    # check to see that our "mongod" actually did start up (catches "--help", "--version", MongoDB 3.2 being silly, slow prealloc, etc) \n\
    # https://jira.mongodb.org/browse/SERVER-16292 \n\
    tries=30 \n\
    while true; do \n\
        if ! { [ -s "$pidfile" ] && ps "$(< "$pidfile")" &> /dev/null; }; then \n\
            # bail ASAP if "mongod" is not even running \n\
            echo >&2 \n\
            echo >&2 "error: mongod does not appear to have stayed running -- perhaps it had an error?" \n\
            echo >&2 \n\
            exit 1 \n\
        fi \n\
        if mongo admin --eval "quit(0)" &> /dev/null; then \n\
            # success! \n\
            break \n\
        fi \n\
        (( tries-- )) \n\
        if [ "$tries" -le 0 ]; then \n\
            echo >&2 \n\
            echo >&2 "error: mongod does not appear to have accepted connections quickly enough -- perhaps it had an error?" \n\
            echo >&2 \n\
            exit 1 \n\
        fi \n\
        sleep 1 \n\
    done \n\
    mongo -- admin <<-EOF \n\
    db.createUser({user:"user", pwd:"admin123", roles:[{role:"root", db:"admin"}]}); \n\
EOF ' >> ./init_mongo.sh && cat ./init_mongo.sh && chmod +x ./init_mongo.sh && ./init_mongo.sh && /opt/myproject/start.sh all

When I run the container, it gives the below error:

about to fork child process, waiting until server is ready for connections.
forked process: 13
child process started successfully, parent exiting
./init_mongo.sh: line 28: warning: here-document at line 26 delimited by end-of-file (wanted `EOF')
MongoDB shell version v3.4.22
connecting to: mongodb://127.0.0.1:27017/admin
MongoDB server version: 3.4.22
Successfully added user: {
    "user" : "user",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
2019-09-10T11:45:05.776+0000 E QUERY    [thread1] ReferenceError: EOF is not defined :
@(shell):1:1
bye

Just in case, I even cat the script and see nothing wrong with it. Any ideas/suggestions? Seems like maybe a missing or trailing whitespace or something... Checked whatever I could think of...

Thank you

Igor Shmukler
  • 1,742
  • 3
  • 15
  • 48
  • 3
    I think your problem is the space after the closing `EOF` ([reference](https://stackoverflow.com/questions/12503581/bash-warning-here-document-at-line-delimited-by-end-of-file-wanted-eof)) ; `EOF` should be the whole line yet there's a space after it -> it's not recognized as the end of the heredoc -> the heredoc continues up to the end of the file -> the warning & the fact that it includes an extra `EOF` that mongoDB doesn't know what to do with – Aaron Sep 10 '19 at 12:19
  • 4
    You could break this shell script out into a separate file and COPY it into your image. It’d be much easier to edit and maintain. Don’t forget to `exec "$@"` at the end so that it runs the main container command. – David Maze Sep 10 '19 at 13:15

0 Answers0