我正在為此糾結。我不知道哪里出了問題,但似乎 docker 容器總是執行我用 nodejs 放置的任何命令。它導致沒有錯誤的唯一方法是當我將“index.js”作為單個命令放在docker-compose.yml

我是 docker 新手,但有什么地方我應該看看嗎?
我的碼頭檔案:
FROM node:17
WORKDIR /opt/application
COPY ./certificate/server* ./certificate/
COPY package.json .
COPY config.json .
COPY tsconfig.json .
COPY ./src/ ./src
RUN npm install nodemon typescript -g
RUN npm install
RUN tsc -p .
docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "80:3000"
container_name: nodejs
volumes:
- "./src:/opt/application/src"
depends_on:
- "mongo"
command:
- "tsc -w"
- "nodemon"
mongo:
image: "mongo:latest"
ports:
- "27017:27017"
我不知道添加nodejs的配置在哪里。
任何幫助表示贊賞。謝謝
uj5u.com熱心網友回復:
假設您完全知道您已經/opt/application在容器中設定了作業目錄。嘗試:
version: "3.9"
services:
web:
build: .
ports:
- "80:3000"
container_name: nodejs
volumes:
- "./src:/opt/application/src"
depends_on:
- "mongo"
command: ["tsc","-w","nodemon"]
mongo:
image: "mongo:latest"
ports:
- "27017:27017"
uj5u.com熱心網友回復:
好吧,當然它運行每個命令都帶有node,它明確地用于完成該影像。
當我們執行 時docker history node:17 --no-trunc,我們可以看到基礎鏡像的最后兩層是:
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node"]
如果我們得到 的內容,docker-entrypoint.sh我們可以看到以下腳本:
#!/bin/sh
set -e
# Run command with node if the first argument contains a "-" or is not a system command. The last
# part inside the "{}" is a workaround for the following bug in ash/dash:
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=874264
if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ] || { [ -f "${1}" ] && ! [ -x "${1}" ]; }; then
set -- node "$@"
fi
exec "$@"
因此,由于您使用的tsc -w是第一個命令,它會識別破折號,并且該命令會以node(如預期的那樣)運行。
此外,您似乎誤解command了 Compose 檔案中的功能:您不能添加多個 command。
所以基本上你強迫的是以下內容:
CMD ["tsc -w", "nodemon"]
我想你想要實作的是以下內容:
包.json
"start": "tsc && concurrently \"tsc -w\" \"nodemon\" "
docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "80:3000"
container_name: nodejs
volumes:
- "./src:/opt/application/src"
depends_on:
- "mongo"
command: npm run start
mongo:
image: "mongo:latest"
ports:
- "27017:27017"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338565.html
標籤:节点.js 码头工人 docker-compose docker-for-windows
