简体   繁体   English

Docker和Angular CLI应用

[英]Docker and Angular CLI app

I'm trying to use Docker to containerize an app built using the Angular CLI. 我正在尝试使用Docker容器化使用Angular CLI构建的应用程序。 It's just the default files that come when you use ng new so that I can learn how to do this. 这只是当您使用ng new时出现的默认文件,以便我学习如何做。 I'm trying to use a two-step build, where the first step runs ng-build --prod --build-optimizer , and then the result of that in the ./dist folder is put into a container with Nginx that serves the app. 我正在尝试使用两步构建,其中第一步运行ng-build --prod --build-optimizer ,然后将./dist文件夹中的结果放入带有Nginx的容器中该应用程序。 I know that if I manually build the app from my command line, then run a Dockerfile with only the second step that I can get the Angular app to be served, but I'd like Docker to do that for me. 我知道,如果我从命令行手动构建应用程序,然后仅运行第二步即可运行Angular应用程序即可运行Dockerfile,但我希望Docker为我完成这一工作。

Here's my Dockerfile: 这是我的Dockerfile:

FROM node:8.9-alpine as angular-built
COPY package.json package.json
RUN npm install
COPY . .
RUN ng build --prod --build-optimizer

FROM nginx:alpine
LABEL author="Preston Lamb"
COPY --from=angular-built ./dist /usr/share/nginx/html
EXPOSE 80 443
ENTRYPOINT [ "nginx", "-g", "daemon off;" ]

I've tried using the node:8.9-alpine base image as well as the johnpapa/angular-cli base image on the first step of the Dockerfile. 我已经尝试在johnpapa/angular-cli使用node:8.9-alpine基本映像以及johnpapa/angular-cli基本映像。 Everything works until the RUN ng build --prod --build-optimizer command, at which point it fails with this error: 一切正常,直到RUN ng build --prod --build-optimizer命令,此时它会因以下错误而失败:

Cannot read property 'config' of null TypeError: Cannot read property 'config' of null at Class.run (/node_modules/@angular/cli/tasks/build.js:15:56) at Class.run (/node_modules/@angular/cli/commands/build.js:216:26) at resolve (/node_modules/@angular/cli/ember-cli/lib/models/command.js:261:20) at new Promise () at Class.validateAndRun (/node_modules/@angular/cli/ember-cli/lib/models/command.js:240:12) at Promise.resolve.then.then (/node_modules/@angular/cli/ember-cli/lib/cli/cli.js:140:24) at 无法读取null的属性'config'TypeError:无法读取Class.run(/ node_modules / @的Class.run(/node_modules/@angular/cli/tasks/build.js:15:56)的null属性'config' angular / cli / commands / build.js:216:26)解析(/node_modules/@angular/cli/ember-cli/lib/models/command.js:261:20)在Class.validateAndRun中的新Promise() (/node_modules/@angular/cli/ember-cli/lib/models/command.js:240:12)在Promise.resolve.then.then.then(然后// node_modules / @ angular / cli / ember-cli / lib / cli / cli.js:140:24)

After googling that, I found that it's because the CLI can't find the .angular-cli.json file. 谷歌搜索之后,我发现这是因为CLI找不到.angular-cli.json文件。 Then I read that by default, Linux doesn't copy hidden files, so it doesn't copy the .angular-cli.json file, and that's where the error comes up. 然后我读到默认情况下,Linux不会复制隐藏文件,因此它不会复制.angular-cli.json文件,这就是出现错误的地方。

My next step was trying to force that file to be copied. 我的下一步是尝试强制复制该文件。 I tried both of the following, putting this step right before the COPY . . 我尝试了以下两种方法,将这一步骤放在COPY . .之前COPY . . COPY . . command: 命令:

COPY .angular-cli.json .angular-cli.json

and

RUN shopt -s dotglob

The first one doesn't seem to do anything; 第一个似乎什么也没做; I got the same error as before. 我遇到了和以前一样的错误。 The second one produces a different error: 第二个产生另一个错误:

/bin/sh: shopt: not found

So the command I found to run to tell Linux to copy the hidden files as well doesn't work. 因此,我发现运行该命令告诉Linux也复制隐藏文件的命令不起作用。 I saw one question that was different than this but related say to change the shopt call a little to the following: 我看到了一个与此问题不同的问题,但相关的说法是将shopt调用稍微更改为以下内容:

RUN /bin/sh -c "shopt -s dotglob"

But that didn't do anything. 但这没有任何作用。 I got the same not found error. 我遇到了相同的not found错误。

So I'm not sure where to go from here. 所以我不确定从这里去哪里。 any help would be greatly appreciated. 任何帮助将不胜感激。

If you use a node alpine image to build your angular app, you need to install the angular cli. 如果使用节点高山图像来构建角度应用程序,则需要安装角度cli。 Notice the npm i -g @angular/cli in the Dockerfile below. 请注意以下Dockerfile中的npm i -g @angular/cli I also added a working directory to the first build stage. 我还在第一个构建阶段添加了一个工作目录。

Then you can run the docker compose commands (or docker build and docker run) for an out of the box angular cli app to run on nginx 然后,您可以运行docker compose命令(或docker build和docker run),以使开箱即用的cli cli应用程序在nginx上运行

Dockerfile Docker文件

FROM node:8.9-alpine as angular-built
WORKDIR /usr/src/app
RUN npm i -g @angular/cli
COPY package.json package.json
RUN npm install --silent
COPY . .
RUN ng build --prod

FROM nginx:alpine
LABEL author="John Papa"
COPY --from=angular-built /usr/src/app/dist /usr/share/nginx/html
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]

docker-compose.debug.yml docker-compose.debug.yml

version: '2.1'

services:
  clean:
    image: clean
    build: .
    environment:
      NODE_ENV: development
    ports:
      - 80:80
      - 443:443
      - 9229:9229
    ## set your startup file here
    command: [nginx-debug, '-g', 'daemon off;']

If you want to use an image based on alpine node 8.9 with the angular cli 1.5.5 already baked in, use this: 如果要使用已烘焙角度cli 1.5.5的基于高山节点8.9的图像,请使用以下命令:

Dockerfile Docker文件

FROM johnpapa/angular-cli as angular-built
WORKDIR /usr/src/app
COPY package.json package.json
RUN npm install --silent
COPY . .
RUN ng build --prod

FROM nginx:alpine
LABEL author="John Papa"
COPY --from=angular-built /usr/src/app/dist /usr/share/nginx/html
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]

BTW - the 1.5 version of the CLI has the --build-optimizer baked in, so it is not needed there 顺便说一句-CLI的1.5版本具有--build-optimizer ,因此在此不需要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM