问题描述
对于我的新手问题,我感到很抱歉,但是我很难解决这个错误,我有一个Express应用程序,并且我试图在docker compose中运行它。
我已经使用了这个Dockerfile:
I am sorry for my very newbie question, but I am having a terrible day figuring out this error, I have an Express app and I am trying to run it in docker compose.I've used this Dockerfile:
FROM mhart/alpine-node
RUN mkdir -p /usr/src/app
RUN chmod -R 777 /usr/src/app
WORKDIR /usr/src/app
RUN npm install node-gyp -g
RUN npm install nodemon -g
ENV NODE_ENV development
EXPOSE 3000
docker-compose文件:
And this portion of my docker-compose file:
backend:
mem_limit: 100m
build:
context: .
dockerfile: dockerfiles/node/Dockerfile
command: npm start
depends_on:
- mongo
- elasticsearch
volumes:
- ./backend/:/usr/src/app
ports:
- 3000:3000
links:
- "mongo:mongo"
- "elasticsearch:elasticsearch"
当我做docker-compose up时,出现此错误:
When I do docker-compose up, I get this error:
backend_1 | npm info it worked if it ends with ok
backend_1 | npm info using [email protected]
backend_1 | npm info using [email protected]
backend_1 | npm info lifecycle [email protected]~prestart: [email protected]
backend_1 | npm info lifecycle [email protected]~start: [email protected]
backend_1 |
backend_1 | > [email protected] start /usr/src/app
backend_1 | > nodemon index.js
backend_1 |
backend_1 | [nodemon] 1.11.0
backend_1 | [nodemon] to restart at any time, enter `rs`
backend_1 | [nodemon] watching: *.*
backend_1 | [nodemon] starting `node index.js`
backend_1 | module.js:471
backend_1 | throw err;
backend_1 | ^
backend_1 |
backend_1 | Error: Cannot find module 'dotenv'
backend_1 | at Function.Module._resolveFilename (module.js:469:15)
backend_1 | at Function.Module._load (module.js:417:25)
backend_1 | at Module.require (module.js:497:17)
backend_1 | at require (internal/module.js:20:19)
backend_1 | at Object.<anonymous> (/usr/src/app/index.js:1:63)
backend_1 | at Module._compile (module.js:570:32)
backend_1 | at Object.Module._extensions..js (module.js:579:10)
backend_1 | at Module.load (module.js:487:32)
backend_1 | at tryModuleLoad (module.js:446:12)
backend_1 | at Function.Module._load (module.js:438:3)
backend_1 | [nodemon] app crashed - waiting for file changes before starting...
如果我这样做 ls -al
在 后端 容器中,我获得了我的后端应用文件夹内容的完整列表,但这听起来像是node_modules的依赖项
If I do ls -al
in the backend container, I get a full list of my backend app folder content, but it sounds like node_modules dependencies are not recognized.
推荐答案
您需要在容器中安装依赖项,而Dockerfile中缺少该依赖项。
You need to install the dependencies in the container, which is missing from your Dockerfile.
常见的方法是创建一个已经知道您的应用程序的Dockerfile,并使其复制您的 package.json
文件,然后执行 npm安装
。
The common way is to create a Dockerfile that is already aware of your application, and make it copy your package.json
file and perform an npm install
.
这可以让您的容器在以后运行应用程序时查找所有代码依赖项。
This allows your container to find all your code dependencies when you later run your application.
请参见示例此处:
示例 Dockerfile
:
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]
当然,您可能需要为 COPY
命令修改路径。
You may need to adapt paths for the COPY
command, of course.
这篇关于找不到在docker compose环境中运行的Node js应用程序的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!