[英]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:我做了以下事情:
docker-compose build -d
to load all images. docker-compose build -d
加载所有图像。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
确认相同。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 yourpackage.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.