node.js - 学习 NodeJS 和 MongoDB Docker 组合

我有一个使用 MongoDB 的 NodeJS 项目。我想在 Docker 容器中运行此服务,但是尽管我学习了许多示例,但这行不通。

这是我的 /heimdall_jwt/Dockerfile:

FROM node:9-alpine
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
RUN npm install pm2 -g
COPY . /usr/src/app
EXPOSE 3000
CMD ["pm2-docker", "start", "process.json"]

这是我的 /heimdall_jwt/docker-compose.yml

version: '2'
    # Define the services/containers to be run
services:
    myapp: #name of your service
        build: ./ # specify the directory of the Dockerfile
        ports:
          - "3000:3000" #specify ports forwarding
        links:
          - database # link this service to the database service
        volumes:
          - .:/usr/src/app
        depends_on:
          - database    
    database: # name of the service
       image: mongo # specify image to build container from

我尝试通过以下方式运行:$docker-compose up --build 这导致 mongo 正在构建和启动。这是压缩输出:

Building myapp
Step 1/8 : FROM node:9-alpine
...
Step 4/8 : RUN npm install
 ---> Using cache
 ---> befb91b1324c
...
Removing intermediate container 945eb0ad40d5
Successfully built b500f7ec9b89
Successfully tagged heimdalljwt_myapp:latest
Creating heimdalljwt_database_1 ...
Creating heimdalljwt_database_1 ... done
Creating heimdalljwt_myapp_1 ...
Creating heimdalljwt_myapp_1 ... done
Attaching to heimdalljwt_database_1, heimdalljwt_myapp_1
database_1  | 2017-11-25T21:15:39.001+0000 I INDEX    [initandlisten]    building index using bulk method; build may temporarily use up to 500 megabytes of RAM
database_1  | 2017-11-25T21:15:39.002+0000 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
database_1  | 2017-11-25T21:15:39.003+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
database_1  | 2017-11-25T21:15:39.005+0000 I NETWORK  [thread1] waiting for connections on port 27017
myapp_1     | 0|heimdall | Error: Cannot find module 'bcryptjs'
myapp_1     | 0|heimdall |     at Function.Module._resolveFilename (module.js:542:15)
myapp_1     | 0|heimdall |     at Function.Module._load (module.js:472:25)
myapp_1     | 0|heimdall |     at Module.require (module.js:585:17)
myapp_1     | 0|heimdall |     at require (internal/module.js:11:18)
myapp_1     | 0|heimdall |     at Object.<anonymous> (/usr/src/app/user/User.js:2:14)
myapp_1     | 0|heimdall |     at Module._compile (module.js:641:30)
myapp_1     | 0|heimdall |     at Object.Module._extensions..js (module.js:652:10)
myapp_1     | 0|heimdall |     at Module.load (module.js:560:32)
myapp_1     | 0|heimdall |     at tryModuleLoad (module.js:503:12)
myapp_1     | 0|heimdall |     at Function.Module._load (module.js:495:3)
myapp_1     | 0|heimdall |     at Module.require (module.js:585:17)
myapp_1     | 0|heimdall |     at require (internal/module.js:11:18)
myapp_1     | 0|heimdall |     at Object.<anonymous> (/usr/src/app/user/UserController.js:12:12)
myapp_1     | 0|heimdall |     at Module._compile (module.js:641:30)
myapp_1     | 0|heimdall |     at Object.Module._extensions..js (module.js:652:10)
myapp_1     | 0|heimdall |     at Module.load (module.js:560:32)
myapp_1     | PM2        | App name:heimdall_app id:0 disconnected

我不确定这里发生了什么,但我猜测依赖项要么没有被安装,要么没有被复制到工作目录中。如何

更新 从原始帖子修改,因为我仍在努力让它发挥作用。

最佳答案

您应该在 npm install 之后复制文件。现在您正在安装依赖项,然后复制所有内容,因此您实际上取消了该安装并且您没有依赖项。

在您的 Dockerfile 中,您有:

...
RUN npm install
RUN npm install pm2 -g
COPY . /usr/src/app
...

应该是:

...
COPY . /usr/src/app
RUN npm install
RUN npm install pm2 -g
...

https://stackoverflow.com/questions/47457828/

相关文章:

mongodb - 如何为 mongodb 副本集配置 grails 3

javascript - 如何在 Node 议程中以编程方式定义多个具有相同名称的作业

node.js - Mongoose(或类似的 ODM)内存记录注册表?

node.js - mongoose.connection.collections.collecti

c# - MongoDB 身份验证错误

java - 从 JAVA 应用程序使用 SSL 连接到 MongoDb

javascript - 如何将文件名设置为与数据库中的对象 ID 相同?

linux - 从 bash 提示符和脚本向 docker exec 传递参数

node.js - Mongoose 复合索引创建字段顺序

mongodb - 为什么 mongo dot notation 会替换整个子文档?