Node.js 模块未在Docker Swarm中为NodeJS应用程序找到错误
我在NodeJS中编写了一个简单的Hello World应用程序,并尝试将其部署在单节点Docker Swarm集群上。 但是,在(一个)主节点上,命令Node.js 模块未在Docker Swarm中为NodeJS应用程序找到错误,node.js,docker,docker-compose,docker-swarm,Node.js,Docker,Docker Compose,Docker Swarm,我在NodeJS中编写了一个简单的Hello World应用程序,并尝试将其部署在单节点Docker Swarm集群上。 但是,在(一个)主节点上,命令 docker服务创建--使用注册表身份验证--名称测试后端--env APP_HOST=0.0.0.0--env APP_PORT=3000--网络自定义覆盖网络注册表.example.com/:最新测试后端 导致以下错误: internal/modules/cjs/loader.js:985 throw err; ^ Erro
docker服务创建--使用注册表身份验证--名称测试后端--env APP_HOST=0.0.0.0--env APP_PORT=3000--网络自定义覆盖网络注册表.example.com/:最新测试后端
导致以下错误:
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\“节点./index.js\”
覆盖“docker服务创建”命令中的入口点时,一切正常。
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"]
文件结构:
- index.js
- Dockerfile
- package.json
谢谢你的帮助!提前感谢。您不需要将
测试后端作为参数,因为您已使用--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
docker服务创建--使用注册表身份验证--名称测试后端--env APP_HOST=0.0.0.0--env APP_PORT=3000--网络自定义覆盖网络注册表。example.com//:latest
注:正如@Dmytro Sirant所指出的,当添加测试后端时,其被视为CMD[“测试后端”]
,这将导致上述错误
测试后端
对应什么?它是你项目的一部分吗?它是npm项目和容器的名称,所以没有什么特别的。只是一小部分关于发生了什么的更多信息。命令中的最后一个“test backend”转换为CMD,而不是从Dockerfile中运行CMD,而是从CMD[“test backend”]中运行正在使用的映像的入口点。感谢您的解释-这很有效。我没想到答案会这么简单。