1

I’m trying to automate build on dockercloud to build a docker container for my front-end.

enter image description here


I have

web.dockerfile

### STAGE 1: Build ###
FROM node:9.3.0-alpine as builder

COPY package.json ./

RUN npm set progress=false && npm config set depth 0 && npm cache clean --force

## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i
RUN mkdir /web
RUN cp -R ./node_modules ./web

WORKDIR /web

COPY . .

## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --prod --build-optimizer

### STAGE 2: Setup ###

FROM nginx:1.13.8-alpine

COPY nginx.conf /etc/nginx/nginx.conf
COPY site.conf /etc/nginx/conf.d/default.conf
RUN rm -rf /usr/share/nginx/html/*

COPY dist /usr/share/nginx/html/

RUN touch /var/run/nginx.pid && \
  chown -R nginx:nginx /var/run/nginx.pid && \
  chown -R nginx:nginx /var/cache/nginx && \
  chown -R nginx:nginx /usr/share/nginx/html

USER nginx

as you see I already did this line :

RUN $(npm bin)/ng build --prod --build-optimizer

Which it should generate the dist/ folder for me.


I got

This long logs in my docker-cloud

enter image description here

What I don't understand is, it is working fine, when I used that Dockerfile locally.

Should I look into my webpack settings or my Dockerfile syntax ?

Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604
  • Maybe `COPY --from=builder /web/dist /usr/share/nginx/html/` ? Although I'm not sure how it's working locally without the `--from` – Matt Jan 17 '18 at 06:15
  • You probably have a `dist/` folder locally for dev, then that's what is being copied in. – Matt Jan 17 '18 at 06:17

1 Answers1

1

Use the --from argument to COPY to set the source location to a previous build stage.

COPY --from=builder /web/dist /usr/share/nginx/html/

For your local build you probably have a dist/ folder for your local development which is then included in the build context sent to Docker. The local copy is what ends up in the image.

Matt
  • 68,711
  • 7
  • 155
  • 158
  • will attempt this now. Thanks for your suggestion. :) – code-8 Jan 17 '18 at 18:11
  • My build taking so long to build. 99% of latency cause my `RUN $(npm bin)/ng build --prod --build-optimizer` do u have any suggestions for me ? Is there another way that I can achieve what I am trying to do, without build my npm build inside my container ? – code-8 Jan 17 '18 at 18:22
  • No sorry, I did see your other question but I've not attempted an angular build in Docker so don't have any tricks up my sleeve. – Matt Jan 18 '18 at 00:20