對于本地開發人員環境,我確保在每次啟動時通過 /bin/sh 命令清除已安裝的 node_modules 目錄,該命令應在服務器啟動之前運行,但在容器安裝并運行之后。
所需的行為,指定的 NPM 腳本應從“CMD”配置或其他 docker-compose 或 CLI 源附加
/bin/sh -c "rm -rf /usr/src/app/node_modules/* && npm run start
// OR
/bin/sh -c "rm -rf /usr/src/app/node_modules/* && npm run production
我假設這種腳本啟動行為在作為入口點運行的 .sh 檔案中更常見,但我想從 Dockerfile 指定完整命令
我當前的 Dockerfile
ENTRYPOINT ["/bin/sh", "-c", "rm -rf /usr/src/app/node_modules/* && npm run ${exec $@}"]
CMD [ "start" ]
我不確定如何處理 ENTRYPOINT 陣列中 "$@" 周圍的雙引號的轉義。
當前從容器啟動接收此輸出
start: 1: start: Bad substitution
uj5u.com熱心網友回復:
我強烈建議將此啟動程序寫入它自己的 shell 腳本:
#!/bin/sh
# Delete the library tree from the image; we're not going to use it.
rm -rf /usr/src/app/node_modules
# Interpret the command we're given as a specific `npm run` script.
exec npm run "$@"
現在您可以COPY在 Dockerfile 中使用此腳本,并將 設定ENTRYPOINT為僅運行它。不要sh -c在這里使用包裝器。
COPY entrypoint.sh ./
ENTRYPOINT ["./entrypoint.sh"] # in JSON-array syntax
CMD ["start"]
如果您要sh -c像這樣行內,則要運行的命令之后的任何引數都將作為位置引數$0, $1, 等等。通常$0是腳??本名稱(嘗試添加echo "$0"到示例腳本中以查看結果),并且sh -c您需要手動提供該引數。
ENTRYPOINT ["/bin/sh", "-c", "rm -rf node_modules && npm run \"$1\"", "script"]
CMD ["start"]
這些陣列使用 JSON 語法,引號轉義與 JSON 或 Javascript 中的相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/469190.html
