【问题标题】:Learning NodeJS & MongoDB Docker compose学习 NodeJS 和 MongoDB Docker 编写
【发布时间】:2018-05-07 13:22:11
【问题描述】:

我有一个使用 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

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

更新 由于我仍在努力使其正常工作,因此已根据原始帖子进行了修改。

【问题讨论】:

  • 你可以在stackoverflow.com/questions/37454548/…找到类似的问题
  • 谢谢,这帮助它移动了很长时间,但我仍然无法真正让这个东西运行。我用我的观察更新了帖子。
  • 那是因为缺少包,你必须在 heimdall_jwt dockerfile 中进行更改,在启动 npm install 之前安装所有必要的包
  • 我没有看到我的 Dockerfile 不正确的地方,我创建了目录,将 package.json 复制到它们,然后运行 ​​npm install。那不安装必要的包吗?

标签: node.js mongodb docker docker-compose


【解决方案1】:

您应该在 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
...

【讨论】:

  • 我明白了:步骤 5/8:运行 npm install ---> 在 39ee34e1ce11 npm ERR 中运行! JSON 中的意外令牌 } 在位置 124674 npm ERR!可以在以下位置找到此运行的完整日志:npm ERR! /root/.npm/_logs/2017-11-28T01_28_32_360Z-debug.log 错误:服务'myapp'未能构建:命令'/bin/sh -c npm install'返回非零代码:1但我不能在任何地方找到该 .log 文件进行故障排除
  • 对您的 package.json 进行故障排除。删除您认为可能导致此问题的内容,然后重试...
  • 您在更改 COPY 和 RUN npm install 的顺序后出现错误?
  • 我是在更改 COPY 和 RUN 顺序后得到的。我找到了日志文件,现在正在查看它
  • 你应该确保 package.json 在那里。也许在复制所有内容后复制 package.json 。也许这导致了错误......
猜你喜欢
  • 2010-09-05
  • 1970-01-01
  • 2019-06-14
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 2022-12-28
相关资源
最近更新 更多