简体   繁体   English

使用 Docker 将 Angular 项目容器化

[英]Containerizing Angular project with Docker

I've recently learned how to use Docker and am trying to containerize an Angular project that I've been working on.我最近学习了如何使用 Docker 并尝试将我一直在从事的 Angular 项目容器化。

The project's package.json is as following:该项目的package.json如下:

{
"name": "argon-dashboard-angular",
"version": "1.0.1",
"scripts": {
  "ng": "ng",
  "start": "ng serve --port 8080",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e",
  "install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && npm install && npm start"
},
"private": true,
"dependencies": {
  "@angular/animations": "7.2.4",
  "@angular/common": "7.2.4",
  "@angular/compiler": "7.2.4",
  "@angular/core": "7.2.4",
  "@angular/forms": "7.2.4",
  "@angular/http": "7.2.4",
  "@angular/platform-browser": "7.2.4",
  "@angular/platform-browser-dynamic": "7.2.4",
  "@angular/router": "7.2.4",
  "@ng-bootstrap/ng-bootstrap": "4.0.1",
  "bootstrap": "4.1.3",
  "chart.js": "2.7.3",
  "clipboard": "2.0.4",
  "core-js": "2.6.4",
  "ngx-clipboard": "11.1.9",
  "ngx-toastr": "9.1.2",
  "nouislider": "13.1.1",
  "rxjs": "6.4.0",
  "zone.js": "0.8.29"
},
"devDependencies": {
  "@angular-devkit/build-angular": "0.13.1",
  "@angular/cli": "7.3.1",
  "@angular/compiler-cli": "7.2.4",
  "@angular/language-service": "7.2.4",
  "@types/jasmine": "3.3.8",
  "@types/jasminewd2": "2.0.6",
  "@types/node": "11.9.0",
  "codelyzer": "4.5.0",
  "jasmine-core": "3.3.0",
  "jasmine-spec-reporter": "4.2.1",
  "karma": "4.0.0",
  "karma-chrome-launcher": "2.2.0",
  "karma-coverage-istanbul-reporter": "2.0.4",
  "karma-jasmine": "2.0.1",
  "karma-jasmine-html-reporter": "1.4.0",
  "protractor": "5.4.2",
  "ts-node": "8.0.2",
  "tslint": "5.12.1",
  "typescript": "3.2.4"
  }
}

My Dockerfile is as following:我的Dockerfile如下:

FROM node:10.18.0-alpine AS build
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
CMD ["npm","start"]
EXPOSE 8080

I build from the image and run my container:我从图像构建并运行我的容器:

docker build .
docker run <container-id>

Once the container is running, I tried to access http://localhost:8080, but the browser reads "localhost refused to connect".容器运行后,我尝试访问 http://localhost:8080,但浏览器显示“localhost 拒绝连接”。

Also, my Docker Desktop app shows these buttons next to my container:此外,我的 Docker 桌面应用程序在我的容器旁边显示这些按钮:

Docker 容器缺少按钮

Usually there would be another button to the left that opens the container in my browser at its port.通常左侧会有另一个按钮在我的浏览器中打开容器的端口。

Also, after looking through some tutorials, I noticed that many suggested using an Nginx Docker image to serve my web app.此外,在浏览了一些教程后,我注意到许多人建议使用 Nginx Docker 图像来为我的 web 应用程序提供服务。 Is this necessary?这是必要的吗? Why can't I serve my app through ng serve ?为什么我不能通过ng serve服务我的应用程序?

For building purpose it's better to use Multistage building like below:出于构建目的,最好使用如下所示的多阶段构建:

FROM node:10 AS builder

WORKDIR /app
RUN npm install -g @angular/cli
RUN ng new my-app --routing=true --style=css --skipGit=true --minimal=true
WORKDIR /app/my-app
RUN ng build --prod

FROM nginx
COPY --from=builder /app/my-app/dist/my-app/ /usr/share/nginx/html

https://docs.docker.com/develop/develop-images/multistage-build/ https://docs.docker.com/develop/develop-images/multistage-build/

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

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