abp icon indicating copy to clipboard operation
abp copied to clipboard

dockerize angular app

Open luisp88 opened this issue 6 years ago • 10 comments

I'am trying to create one image with angular side and another with netcore side. Trying for angular, dockerfile:

FROM node:12.16.1 AS compile-image WORKDIR /opt/ng COPY package.json ./ RUN npm install ENV PATH="./node_modules/.bin:$PATH" COPY . ./ RUN ng build --prod FROM nginx COPY docker/nginx/default.conf /etc/nginx/conf.d/default.conf COPY --from=compile-image /opt/ng/dist/fichamedica /usr/share/nginx/html

throws: image For the out of memory, changed "ng build --prod" for "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build --prod" and throws: image btw, build commands go fine locally. Any ideas?

luisp88 avatar May 02 '20 22:05 luisp88

I had to allocate more memory to Docker, after that (and long time waiting) it finished successfully

luisp88 avatar May 07 '20 21:05 luisp88

@luisp88 the Dockerfile in the first post does not work for me. image

I don't know how your builder finds .conf file

leonkosak avatar Aug 13 '20 10:08 leonkosak

I was focusing in build problem, so probably after resolving that I encountered other problems (as the one you're showing) and updated the dockerfile. Use this instead:

### STAGE 1: Build ###
FROM node:12.16.1 AS build
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install -g @angular/cli
RUN npm install
COPY . .
RUN npm run ng-high-memory-integ
### STAGE 2: Run ###
FROM nginx
COPY --from=build /usr/src/app/dist/FichaMedica /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf

PS: I started using custom commands for ng build because I had memory problems for compiling with production target. ng-high-memory-integ in my package.json: node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng build --configuration=production (or prod)

luisp88 avatar Aug 17 '20 15:08 luisp88

Thank you @luisp88, but my dockerfile:

### STAGE 1: Build ###
FROM node:12.16.1 AS build
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install -g @angular/cli
RUN npm install
COPY . .
RUN npm run node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng build --configuration=production
### STAGE 2: Run ###
FROM nginx
COPY --from=build /usr/src/app/dist/MyApp /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf

returns this error

image

What is your Dockerfile for ASP.NET Core project? I am experiencing this issue: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin ASP.NET Core projects (.HttpApi.Host and .IdentityServer) runs inside kestrel. All three projects (IdentityServer, HttpApi.Host and Angular) are deployed to separated containers with dedicated static IP inside Docker environment.

leonkosak avatar Aug 17 '20 17:08 leonkosak

Just try node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng build in your dockerfile (without npm run). Allocating max_old_space_size is not mandatory (I had to use it because I was short in memory for build command). You can use ng build --prod or something else directly. You can use npm run X when you define X in your package.json's "scripts" section (like an alias). Example: npm run build if your scripts section has "build": "ng build", Also, use the image version you need (in my case, I am using node 12.16.1)

luisp88 avatar Aug 17 '20 18:08 luisp88

Thanks @luisp88, but there is still error on the last step: image How can I determine where is nginx.conf file?

Would you please share Dockerfile for .NET Core projects (.HttpApi.Host and .IdentityServer)? I suspect that kestrel webserver has to do with "CORS Missing Allow Origin"?

leonkosak avatar Aug 18 '20 15:08 leonkosak

Sorry, forgot to paste nginx configuration file in repo's root

server {
  listen 80;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

luisp88 avatar Aug 19 '20 16:08 luisp88

@luisp88, lease ping me on email [email protected] for more detailed situation. :) Thank you.

leonkosak avatar Aug 19 '20 17:08 leonkosak

Well, I dockerized Angular, but I have question. Is it somehow possible to set environment variables (which are inside environment.ts, environment.ts) during building on Docker installation? It would be really annoying to build Angular Docker image for each client separately.

leonkosak avatar Aug 27 '20 11:08 leonkosak

@luisp88 how to achive this in Dockerfile

  1. How to replace on the fly (when image is taken from Docker registry) and building on server:
  • environment.oAuthConfig.issuer
  • environment.apis.default.url
  1. How to run (nginx inside Angular container)

This is essential, because I do not want to create Angular image for each customer.

Thank you.

leonkosak avatar Sep 10 '20 17:09 leonkosak