简体   繁体   中英

Node / express / mongodb docker-compose file fails because of "shift 2" in shell file

I am trying to build this app https://github.com/dwgebler/node-express-example . I want to use it as a template for making a Mongodb http express server. I tried to build the docker compose file in this repo which looks like this:

version: "3.8"
services:
  database:
    image: mongo
    container_name: mongo-database
    restart: always
    volumes:
      - "mongodata:/data/db"
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
  mongo-express:    
    image: mongo-express
    container_name: mongo-express
    restart: always
    ports:
      - "8081:8081"
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_SERVER: mongo-database
  web:
    image: node:14-alpine
    container_name: node-server
    restart: always
    user: "node"
    ports:
      - "9443:443"
    volumes:
      - "./app:/var/app/"
    working_dir: "/var/app"
    environment:
      NODE_ENV: dev
      DB_USER: testdb
      DB_PASSWORD: testpwd
      DB_NAME: myapp
    command: "./wait.sh mongo-database 27017 'npm start'"
volumes:
  mongodata:

But when I run the command docker compose up -d in the same directory as the docker-compose.yml file, I get this in the node-server container logs:

SyntaxError: Unexpected number
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47
/var/app/wait.sh:4
shift 2

The wait.sh file where the error is occuring looks like this:

#!/bin/sh
host="$1"
port="$2"
shift 2
cmd="$@"
until nc -z "$host" "$port"; do
  >&2 echo "Mongo is unavailable - sleeping"
  sleep 1
done
>&2 echo "Mongo started"
exec $cmd

So why is "shift 2" causing this SyntaxError: Unexpected number error? How do I fix this?

The Node image has a script defined in it's ENTRYPOINT that tries to determine if the command passed is something for node or a native Linux command. In your case, it thinks that it's a command for Node, so the script passes your command to Node.

Node then starts running your wait.sh script as a Javascript file and fails.

To get it to run your script, you should override the entrypoint. So instead of

command: "./wait.sh mongo-database 27017 'npm start'"

do

entrypoint: "./wait.sh mongo-database 27017 'npm start'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM