Node/Express/MongoDB Docker Compose启动失败:wait.sh中shift 2报错
问题原因与解决方法
核心原因
这个错误是因为你的wait.sh脚本被Node.js而非Shell解释器执行了。shift是Shell的内置命令,Node.js无法识别该语法,因此抛出SyntaxError: Unexpected number错误。
解决步骤
1. 修正Dockerfile的启动命令
检查项目的Dockerfile,确认启动命令是用Shell来运行wait.sh,而非Node.js。
- 错误示例:
CMD ["node", "wait.sh"] - 正确写法(二选一):
# 使用sh执行脚本 CMD ["sh", "wait.sh"] # 或者赋予脚本执行权限后直接运行 CMD ["./wait.sh"]
2. 确保脚本包含正确的Shebang行
在wait.sh文件的第一行添加Shebang声明,指定脚本的解释器:
#!/bin/sh
这会告诉容器使用POSIX Shell来执行该脚本。
3. 为脚本添加执行权限
在Dockerfile中添加命令,确保wait.sh具备可执行权限:
RUN chmod +x wait.sh
4. 重新构建并启动容器
执行以下命令重新生成镜像并启动服务:
docker compose down docker compose build --no-cache docker compose up -d
内容的提问来源于stack exchange,提问作者ChristianOConnor
相关产品推荐
相关产品推荐

