docker - Angular 9 Dockerizing
问题描述
我对 Angular 9 进行 dockerizing 有问题。
Dockerfile:
#############
### build ###
#############
# base image
FROM node:12.2.0 as build
# install chrome for protractor tests
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install -g @angular/cli@7.3.9
# add app
COPY . /app
# run tests
#RUN ng test --watch=false
#RUN ng e2e --port 4202
# generate build
RUN ng build --output-path=dist
############
### prod ###
############
# base image
FROM nginx:1.16.0-alpine
# copy artifact build from the 'build environment'
COPY --from=build /app/dist /usr/share/nginx/html
# expose port 80
EXPOSE 80
# run nginx
CMD ["nginx", "-g", "daemon off;"]
我在我的机器上测试了那个文件:OS Ubuntu 18.04 LTS, 16GB Ram
现在我必须将该图像部署到我在 Digital Ocean 上购买的 VPS。
VPS 配置:1 个 CPU,2GB 内存,25GB 硬盘;操作系统:Ubuntu 18.04 LTS
但是当我尝试使用这个 Dockerfile 在我的 VPS 上构建图像时
它抛出一个错误:
Generating ES5 bundles for differential loading...
An unhandled exception occurred: Call retries were exceeded
See "/tmp/ng-1cdowd/angular-errors.log" for further details.
The command '/bin/sh -c ng build --output-path=dist' returned a non-zero code: 127
解决方案
我没有安装 Chrome,也没有运行测试,但是在构建步骤时遇到了类似的问题。我最终使用了这个 Dockerfile 并且它有效。
显然,它不喜欢RUN ng build
,但更喜欢RUN npm run build
FROM node:12.16.1-alpine As builder
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build --prod
FROM nginx:1.15.8-alpine
COPY --from=builder /usr/src/app/dist/SampleApp/ /usr/share/nginx/html
您必须为您的应用程序替换“SampleApp”。
推荐阅读
- docker - 带有Docker的nginx反向代理-连接到上游时连接被拒绝
- python - 列表中的重复项保留第一个元素
- java - 如何利用存储在多维数组中的数据
- opengl - 坐标系在 OpenGL 中是如何工作的?
- mysql - 使用内连接从 mql 表中删除行
- powershell - ForEach 循环使我的变量为空
- sql - 如何从不同的 python 脚本中获取值并在另一个 python 脚本中调用它
- azure - 允许用户在 Azure 门户上管理存储帐户密钥、容器、blob 所需的角色
- python - 为什么我从 pandas 读取的 csv 文件会得到未命名的列?
- command - 在一定级别召唤一个暴徒