npm run build 不会在 elasticbeanstalk 上创建构建目录

Posted

技术标签:

【中文标题】npm run build 不会在 elasticbeanstalk 上创建构建目录【英文标题】:npm run build does not create build directory on elasticbeanstalk 【发布时间】:2019-05-26 19:04:04 【问题描述】:

我仍然对为什么在我构建 dockerfile 时 elasticbeanstalk 实例上的 npm run build 命令不生成构建文件夹感到困惑。这绝对不是权限问题,因为我可以mkdir 并分别触摸新目录和文件。我什至在 dockerfile 中列出了它,我可以确认构建文件夹不存在。 npm install 也有效,我可以看到那里所有已安装的库

但是,当我在本地构建相同的 dockerfile 时,我可以看到它创建了一个 build 文件夹。所以它绝对是一个环境问题。我在某处读到有时 npm install 会在t2.micro 上超时。所以我什至升级到t2.small。问题仍然存在。 有人可以帮我弄清楚这里发生了什么吗?

下面是我的dockerfile

FROM node:alpine as builder
WORKDIR '/app'
COPY package.json ./
RUN npm install
COPY ./ ./
RUN ls
RUN pwd
CMD ["npm", "run" ,"build"] 
RUN mkdir varun
RUN touch var
RUN ls

FROM nginx
EXPOSE 80
RUN pwd
COPY --from=builder ./app/build /usr/share/nginx/html 

下面是 elasticbeanstalk 日志

i-0276e4b74ee15c98f Severe  29 minutes  18  --  --  --  --  --  --  --  --  --  --  0.03    0.28    0.2 0.1 99.7    0.0
Application update failed at 2018-12-29T06:18:23Z with exit status 1 and error: Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/03build.sh failed.

cat: Dockerrun.aws.json: No such file or directory
cat: Dockerrun.aws.json: No such file or directory
cat: Dockerrun.aws.json: No such file or directory
alpine: Pulling from library/node
7fc670963d22: Pull complete
Digest: sha256:d2180576a96698b0c7f0b00474c48f67a494333d9ecb57c675700395aeeb2c35
Status: Downloaded newer image for node:alpine
Successfully pulled node:alpine
Sending build context to Docker daemon 625.7kB 
Step 1/15 : FROM node:alpine as builder
---> 9036ebdbc59d
Step 2/15 : WORKDIR '/app'
---> Running in e623a08307d5
Removing intermediate container e623a08307d5
---> b4e9fe3e4b82
Step 3/15 : COPY package.json ./
---> cb5e6a9b109b
Step 4/15 : RUN npm install
---> Running in 8a00eb1143a5
[91mnpm[0m[91m [0m[91mWARN[0m[91m

Removing intermediate container 8a00eb1143a5
---> c568ef0a4bc3
Step 5/15 : COPY ./ ./
---> cfb3e22fc373
Step 6/15 : RUN ls
---> Running in f6aad2a0f22e
Dockerfile
Dockerfile.dev
README.md
docker-compose.yml
node_modules
package-lock.json
package.json
public
src
Removing intermediate container f6aad2a0f22e
---> 016d1ded2f97
Step 7/15 : RUN pwd
---> Running in eaae644b1d96
/app
Removing intermediate container eaae644b1d96
---> 61285a5062ea
Step 8/15 : CMD ["npm", "run" ,"build"]
---> Running in 5cbca2213f4f
Removing intermediate container 5cbca2213f4f
---> 8566953eebaa
Step 9/15 : RUN mkdir varun
---> Running in a078760b6dcb
Removing intermediate container a078760b6dcb
---> 34c25b5aab32
Step 10/15 : RUN touch var
---> Running in d725dafc9409
Removing intermediate container d725dafc9409
---> 70195ffecb54
Step 11/15 : RUN ls
---> Running in b96bc198883c
Dockerfile
Dockerfile.dev
README.md
docker-compose.yml
node_modules
package-lock.json
package.json
public
src
var
varun
Removing intermediate container b96bc198883c
---> 1b205ffb5e3f
Step 12/15 : FROM nginx
latest: Pulling from library/nginx
bbdb1fbd4a86: Pull complete
Digest: sha256:304008857c8b73ed71fefde161dd336240e116ead1f756be5c199afe816bc448
Status: Downloaded newer image for nginx:latest
---> 7042885a156a
Step 13/15 : EXPOSE 80
---> Running in 412e17c44274
Removing intermediate container 412e17c44274
---> e1e1ea0c7dfb
Step 14/15 : RUN pwd
---> Running in 1bc298a11ef1
/
Removing intermediate container 1bc298a11ef1
---> 291575f13e2f
Step 15/15 : COPY --from=builder ./app/build /usr/share/nginx/html
COPY failed: stat /var/lib/docker/devicemapper/mnt/e2b112f1a046c00990aa6fc01e9fabc9e147420a214682a06637ef8cbcb9414a/rootfs/app/build: no such file or directory
Failed to build Docker image aws_beanstalk/staging-app, retrying...
Sending build context to Docker daemon 625.7kB

【问题讨论】:

【参考方案1】:

我的天啊.. 终于在 aws 上修整了一整天之后,终于搞清楚了。我使用CMD 而不是RUN 来执行命令npm run build。当您构建 dockerfile 时,CMD 实际上并没有运行它。所以我的第二张图片实际上并不存在该文件夹。

【讨论】:

以上是关于npm run build 不会在 elasticbeanstalk 上创建构建目录的主要内容,如果未能解决你的问题,请参考以下文章

关于vue的npm run dev和npm run build

在打包(npm run build)的时候不执行了,但是也不报错,输入npm run dev可以启动

npm run build

npm run build 时的 warning

npm run build 给出 npm ERR!代码生命周期

npm run build打包后自定义动画没有执行