【发布时间】:2018-02-10 17:05:50
【问题描述】:
我正在使用 Elastic beanstalk 来启动一个使用 nodejs 容器和 mongodb 容器的应用程序。我已经为节点 Web 应用程序创建了自己的映像,并且可以使用该映像在本地和 EC2 实例上创建容器,没有任何问题。但是,当使用 Beanstalk 启动应用程序时,CMD“npm run prod”无法找到我的 package.json 文件。以下是关于我的设置和问题的一些说明:
- 使用 Elastic Beanstalk 多容器 Docker 环境
- 使用 Dockerrun.aws.json 文件和两个镜像:Mongo 和 dockerhub 上托管的自定义 nodejs Web 应用程序镜像
- 我可以拉下映像并使用“Docker run npm run prod”运行它,它可以正常工作(在本地和 EB 创建的 EC2 实例上)
- 当 EB 尝试启动映像(使用相同的命令)时,它会出错并说它找不到 package.json(有关详细信息,请参阅下面的错误)。
我不确定我是否对 Elastic Beanstalk 的工作方式有误解,或者我是否还缺少其他东西。
Dockerrun.aws.json 文件(镜像名称和环境变量已删除):
{
"AWSEBDockerrunVersion": 2,
"volumes": [
{
"name": "mongo-vol",
"host": {
"sourcePath": "/var/app/mongo-vol"
}
},
{
"name": "web-vol",
"host": {
"sourcePath": "/var/app/web-vol"
}
}
],
"containerDefinitions": [
{
"name": "mongo",
"image": "mongo:3.4",
"essential": false,
"memory": 128
},
{
"name": "web",
"image": "<IMAGE NAME>",
"essential": true,
"memory": 128,
"portMappings": [
{
"hostPort": 80,
"containerPort": 3000
}
],
"links": [
"mongo"
],
"mountPoints": [
{
"sourceVolume": "web-vol",
"containerPath": "/starter",
"readOnly": false
}
]
}
]
}
节点网络应用程序的 Dockerfile:
FROM node:6-slim
COPY . /starter
COPY package.json /starter/package.json
WORKDIR /starter
ENV NODE_ENV production
RUN yarn install --production
RUN npm install forever -g
CMD npm run prod
EXPOSE 8888
来自 package.json 的 npm 脚本:
"prod": "forever app.js",
来自 docker 容器的错误日志:
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm ERR! Linux 4.9.43-17.38.amzn1.x86_64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "prod"
npm ERR! node v6.11.2
npm ERR! npm v3.10.10
npm ERR! path /starter/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/starter/package.json'
npm ERR! enoent ENOENT: no such file or directory, open '/starter/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /starter/npm-debug.log
将 CMD 更改为 bash -c "pwd && ls -alh && npm run prod" 后的错误日志
/starter
total 12K
drwxr-xr-x 2 root root 4.0K Sep 1 16:01 .
drwxr-xr-x 22 root root 4.0K Sep 1 16:01 ..
-rw-r--r-- 1 root root 885 Sep 1 16:01 npm-debug.log
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm ERR! Linux 4.9.43-17.38.amzn1.x86_64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "prod"
npm ERR! node v6.11.2
npm ERR! npm v3.10.10
npm ERR! path /starter/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/starter/package.json'
npm ERR! enoent ENOENT: no such file or directory, open '/starter/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /starter/npm-debug.log
【问题讨论】:
-
将 CMD 更改为
bash -c "pwd && ls -alh && npm run prod"。在这些更改之后发布新日志 -
我在帖子中添加了错误日志。它表明该文件不存在。根本不存在任何文件。如果我 ssh 进入 EC2 实例并运行 Docker run -it
bash,然后 ls in /starter 文件存在。同样,我不确定我是否在这里误解了某些东西,但这对我来说很奇怪。 -
如果我 ssh 进入 EC2 实例并运行 sudo docker run
bash -c "pwd && ls -alh && npm run prod" 它可以工作,并且所有文件都存在。很迷茫 -
在我的容器定义(Dockerrun.aws.json 文件)中,我的挂载点定义为“/starter”的容器路径...我想知道这是否覆盖了文件路径?
-
是的,这是你的问题。坐骑会遮住里面的一切
标签: node.js amazon-web-services docker npm amazon-elastic-beanstalk