简体   繁体   English

使用 Docker 运行 Node 应用程序时出错:db-on-docker@1.0.0 start: `node index.js`

[英]Error while running Node application with Docker: db-on-docker@1.0.0 start: `node index.js`

I have developed a Nodejs application with Docker but when I run the application I get the error:我已经使用Docker开发了一个Nodejs应用程序,但是当我运行该应用程序时出现错误:

module.js:550
    throw err;
    ^

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/src/app/index.js:1:81)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! db-on-docker@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the db-on-docker@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-01-30T05_34_13_500Z-debug.log

Following are my files: DockerFile present within web folder:以下是我的文件: DockerFile存在于web文件夹中:

FROM node:8

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 9000

CMD ["npm","start"]

CMD tail -f /dev/null

Following is my docker-compose.yml file present within my root project folder :以下是我的root project folder中的docker-compose.yml文件:

version: '3'

services:
  db:
    container_name: db
    image: mysql:5.7
    volumes:
      - "./data/mysql:/var/lib/mysql:rw"
    environment:
      MYSQL_DATABASE: db
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: admin
      MYSQL_PASSWORD: 123456
      DATABASE_HOST: localhost
    restart: always

  web:
    container_name: web
    image: node:8
    volumes:
      - ./web:/usr/src/app
    working_dir: /usr/src/app
    depends_on:
      - db
    restart: on-failure
    command: "tail -f /dev/null"
    environment: ["project=${project}", "type=${type}"]

I did the following things:我做了以下事情:

  1. docker-compose build -d to load all images. docker-compose build -d加载所有图像。
  2. docker-comoise up -d to bring-up all the images and confirmed the same using docker-compose ps . docker-comoise up -d调出所有图像并使用docker-compose ps确认相同。
  3. Then ran the application using the command docker-compose exec web sh -c "project=myproject type=New npm start"然后使用命令docker-compose exec web sh -c "project=myproject type=New npm start"运行应用程序

When I run the 3rd step I get the error as provided above.当我运行第三步时,我得到了上面提供的错误。 This was working previously when I developed the application.当我开发应用程序时,这是以前工作的。 Now I am trying to run the same application in another system but getting this error.现在我试图在另一个系统中运行相同的应用程序,但收到此错误。 Unable to follow what might be wrong.无法理解可能出现的问题。

I have added the express using npm install so I guess its not due to that but due to the line npm ERR. Failed at the db-on-docker@1.0.0 start script.我已经express using npm install所以我猜这不是因为这个,而是由于npm ERR. Failed at the db-on-docker@1.0.0 start script. npm ERR. Failed at the db-on-docker@1.0.0 start script. , I tried to install express using npm install --save express , deleted the node_modules folder. ,我尝试使用npm install --save express express删除了node_modules文件夹。 Tried the npm install but nothing seems to work for me.尝试了npm install ,但似乎没有什么对我有用。

Can someone please let me know what I need to do?有人可以让我知道我需要做什么吗?

There is one thins missing there in your docker-compose.yml file:您的docker-compose.yml文件中缺少一处:

You don't tell docker to build your Dockerfile , instead, just grab the image node:8 from docker registry, but how does docker know that you need to install your packages? You don't tell docker to build your Dockerfile , instead, just grab the image node:8 from docker registry, but how does docker know that you need to install your packages?

You have to add this block under web service in docker-compose.yml file:您必须在docker-compose.yml文件中的web服务下添加此块:

build:
      context: ./web

One more thing to mention is that, you're overriding the default command that you declared in Dockerfile , you can remove this line from docker-compose.yml file还要提到的一件事是,您正在覆盖您在Dockerfile中声明的默认命令,您可以从docker-compose.yml文件中删除此行

command: "tail -f /dev/null"

Honestly, I don't know why you need this here.老实说,我不知道你为什么在这里需要这个。

You can also try to remove this instruction from Dockerfile :您也可以尝试从Dockerfile中删除此指令:

CMD tail -f /dev/null

I think npm start is enough我觉得npm start就够了

Note : npm start requires that you define the entry point in your package.json file, otherwise, you will get an error注意npm start要求您在package.json文件中定义入口点,否则会出现错误

One lat thing and back you the error you've mentioned herewith in your question:一件事情,并支持您在问题中提到的错误:

The reason is because the container can't find express module installed inside the image you're using node:8 in this case, because you just use the image and map your project directory to the container without installing the dependencies.原因是在这种情况下,容器找不到安装在您正在使用node:8的镜像中的express模块,因为您只使用镜像和 map 将您的项目目录安装到容器而不安装依赖项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在用 index.js 和 .env 编写 Discord bot,在终端“node index.js”中运行时遇到了这个错误 - I was writing Discord bot with index.js and .env and I messed with this error while running this in terminal "node index.js" 为 metaplex config.js 运行“node index.js”命令时出错 - error while running the 'node index.js' command for metaplex config.js npm 启动错误但节点 index.js 有效 - npm start error but node index.js works Node.js-启动命令使节点崩溃,但是index.js运行正常 - Node.js - start command crashing node, but index.js running fine 运行 node index.js 什么都不做 - Running node index.js does nothing 运行 npm start 时获取“断言 `args[1]-&gt;IsInt32()&#39; 失败”,而是运行节点 index.js 时没有错误 - Getting "Assertion `args[1]->IsInt32()' failed" running npm start, no error when running node index.js instead 启动 Nodejs 和 Docker,然后在单个语句中将参数传递给 index.js - Start the Nodejs and Docker then pass parameter to index.js in single statement 节点 js docker 未在 heroku 上运行 - node js docker is not running on heroku 从命令行运行`node index.js`是可行的,但是当我在package.json中使用启动脚本时,出现了EADDRINUSE / usr / bin / local / node错误 - Running `node index.js` from command line works but when I use the start script in my package.json I get an EADDRINUSE /usr/bin/local/node error 节点无法启动 react index.js Exit 1 - Node can´t start react index.js Exit 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM