[英]MODULE_NOT_FOUND error in Docker-Swarm for NodeJS Application
我在 NodeJS 中編寫了一個簡單的 Hello World 應用程序,並嘗試將其部署在單節點 Docker-Swarm 集群上。 但是,在(一個)主節點上,命令
docker service create --with-registry-auth --name test-backend --env APP_HOST=0.0.0.0 --env APP_PORT=3000 --network custom-overlay-net registry.example.com/<user>/<image>:latest test-backend
產生以下錯誤:
internal/modules/cjs/loader.js:985
throw err;
^
Error: Cannot find module '/home/node/test-backend'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
我想知道的是,當我將映像作為獨立的 docker 容器或通過 docker-compose 運行時,一切都按預期工作。 此外,當我用--entrypoint "/bin/bash -c \"node./index.js\""
service create”命令中的入口點時,一切正常。
index.js 的內容:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
if (!process.env.APP_HOST) {
console.error('Please specify the APP_HOST environment variable');
}
if (!process.env.APP_PORT) {
console.error('Please specify the APP_PORT environment variable');
}
server.listen(parseInt(process.env.APP_PORT), process.env.APP_HOST, () => {
console.log(`Server running at http://${process.env.APP_HOST}:${process.env.APP_PORT}/`);
});
Dockerfile的內容:
FROM node:12.16.1
WORKDIR /home/node/
# copy all resources
COPY . .
CMD ["node", "./index.js"]
文件結構:
當然,我可以簡單地覆蓋入口點以使事情正常運行,但我想了解為什么使用默認入口點腳本會導致問題。
我很感激任何幫助。 提前致謝。
您不需要test-backend
作為參數,因為您已經使用--name
指定了服務。
以下應該足夠了:
docker service create --with-registry-auth --name test-backend --env APP_HOST=0.0.0.0 --env APP_PORT=3000 --network custom-overlay-net registry.example.com/<user>/<image>:latest
注意:正如@Dmytro Sirant 所指出的,當附加
test-backend
時 - 它被視為CMD ["test-backend"]
,這將導致上述錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.