英文:
How do I fix Docker getting stuck at "RUN npm run build" with Angular 15
问题
I'm trying to build an Angular 15 project in Docker, but the build always hangs at the RUN npm run build
step and never completes. This a fresh install ng new ng-sandbox-15
with the Dockerfile
, .dockerignore
, and nginx.conf
copied from a working Angular 14 fresh install.
./Dockerfile
# Copy dependency definitions
COPY package.json package-lock.json ./
# disabling ssl for npm for Dev or if you are behind proxy
RUN npm set strict-ssl false
## installing and Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm ci && mkdir /app && mv ./node_modules ./app
# Change directory so that our commands run inside this new directory
WORKDIR /app
# Get all the code needed to run the app
COPY . /app/
# Build the application
RUN npm run build
FROM nginx:1.17-alpine
## From 'builder' copy published folder
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx/nginx.conf /etc/nginx/nginx.conf
# Expose the port the app runs in
EXPOSE 4000
CMD ["nginx", "-g", "daemon off;"]
./package.json:
{
"name": "ng-sandbox-15",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "^15.2.0",
"@angular/common": "^15.2.0",
"@angular/compiler": "^15.2.0",
"@angular/core": "^15.2.0",
"@angular/forms": "^15.2.0",
"@angular/platform-browser": "^15.2.0",
"@angular/platform-browser-dynamic": "^15.2.0",
"@angular/router": "^15.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.12.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^15.2.5",
"@angular/cli": "~15.2.5",
"@angular/compiler-cli": "^15.2.0",
"@types/jasmine": "~4.3.0",
"jasmine-core": "~4.5.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.0.0",
"typescript": "~4.9.4"
}
}
As seen in the image below, the Angular build completes successfully in 26.5 seconds, but the step is still stuck 20+ minutes later.
英文:
I'm trying to build an Angular 15 project in Docker, but the build always hangs at the RUN npm run build
step and never completes. This a fresh install ng new ng-sandbox-15
with the Dockerfile
, .dockerignore
, and nginx.conf
copied from a working Angular 14 fresh install.
./Dockerfile
FROM node:16-alpine as builder
# Copy dependency definitions
COPY package.json package-lock.json ./
# disabling ssl for npm for Dev or if you are behind proxy
RUN npm set strict-ssl false
## installing and Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm ci && mkdir /app && mv ./node_modules ./app
# Change directory so that our commands run inside this new directory
WORKDIR /app
# Get all the code needed to run the app
COPY . /app/
# Build the application
RUN npm run build
FROM nginx:1.17-alpine
## From 'builder' copy published folder
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx/nginx.conf /etc/nginx/nginx.conf
# Expose the port the app runs in
EXPOSE 4000
CMD ["nginx", "-g", "daemon off;"]
./package.json:
{
"name": "ng-sandbox-15",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "^15.2.0",
"@angular/common": "^15.2.0",
"@angular/compiler": "^15.2.0",
"@angular/core": "^15.2.0",
"@angular/forms": "^15.2.0",
"@angular/platform-browser": "^15.2.0",
"@angular/platform-browser-dynamic": "^15.2.0",
"@angular/router": "^15.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.12.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^15.2.5",
"@angular/cli": "~15.2.5",
"@angular/compiler-cli": "^15.2.0",
"@types/jasmine": "~4.3.0",
"jasmine-core": "~4.5.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.0.0",
"typescript": "~4.9.4"
}
}
As seen in the image below, the Angular build completes successfully in 26.5 seconds, but the step is still stuck 20+ minutes later.
The most similar problem I've seen is https://stackoverflow.com/questions/70271194/docker-build-getting-stuck-at-npm-run-build-step, and I disagree with the only proposed answer CMD ["npm", "run", "build"]
because that will not wait for the build to complete before trying to copy the built project into the nginx html directory.
答案1
得分: 1
I face the same issue when updated my angular 13 projects to 15.
我在将我的 Angular 13 项目升级到 15 版本时遇到相同的问题。
I have 4 projects, 2 will build success and 2 will stuck after the RUN npm run build
.
我有 4 个项目,其中 2 个构建成功,而另外 2 个在执行 RUN npm run build
后卡住。
After compare the projects I found out in my case is causing by the analytics inside the angular build.
在比较这些项目后,我发现在我的情况下问题是由 Angular 构建中的分析功能引起的。
Change the parameter in angular.json
from
将 angular.json
中的参数更改为
"cli": {
"analytics": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX",
},
to
"cli": {
"analytics": false,
},
This solution solved my issue. The root cause may be causing by upload the analytics data are blocked inside docker build.
这个解决方案解决了我的问题。根本原因可能是由于在 Docker 构建中阻止了分析数据的上传。
英文:
I face the same issue when updated my angular 13 projects to 15.
I have 4 projects, 2 will build success and 2 will stuck after the RUN npm run build
. After compare the projects I found out in my case is causing by the analytics inside the angular build.
Change the parameter in angular.json
from
"cli": {
"analytics": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX",
},
to
"cli": {
"analytics": false,
},
This solution solved my issue. The root cause may be causing by upload the analytics data are blocked inside docker build
答案2
得分: 0
你能检查复制路径,应该是./
而不是. /
在Docker文件中。
另请查看以下代码:
- 在单独的层上安装和存储Node模块将防止每次构建时不必要的npm安装:
RUN npm install && mkdir /app && mv ./node_modules ./app
- 更改目录,以便我们的命令在此新目录内运行:
WORKDIR /app
- 获取运行应用程序所需的所有代码
COPY ./ /app/
英文:
Can you check the copy path it should be ./
not . /
in the docker file.
Also please look into the below code
- Installing and storing node modules on a separate layer will prevent unnecessary npm installs at each build:
RUN npm install && mkdir /app && mv ./node_modules ./app
- Change directory so that our commands run inside this new directory:
WORKDIR /app
- Get all the code needed to run the app
COPY ./ /app/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论