Home > OS >  Docker build runs 2nd stage before the 1st
Docker build runs 2nd stage before the 1st

Time:06-22

I'm following this documentation to write a multi-stage build.

My Dockerfile:

FROM ubuntu:trusty
RUN apt-get update && apt-get install apt-transport-https -y
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install google-chrome-stable -y

FROM node:alpine
COPY . ./
RUN npm install
RUN npm run lighthouse

I'm trying to install Google Chrome onto the image before running Google Lighthouse. However, according to the logs, the build runs the 2nd stage first.

 => CACHED [stage-1 2/4] COPY . ./                                                                                   0.0s 
 => [stage-1 3/4] RUN npm install                                                                                  100.8s
 => ERROR [stage-1 4/4] RUN npm run lighthouse   

Why is this happening?

CodePudding user response:

They are running parallel, cause neither of the stages depend on eachother.. If you are doing this just to understand multi stage builds in docker; Here is a sample:

FROM ubuntu:trusty
RUN apt-get update && apt-get install apt-transport-https -y
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install google-chrome-stable -y

FROM someangularapp:alpine as builder
COPY . ./
RUN npm install
RUN ng build 

##Above stage generates a `dist` folder in its workspace

FROM nginx:latest as deployer
COPY --from=builder /app/dist /usr/share/nginx/html/

Now whenever you run: docker build -t someimagename --target deployer .

The builder stage executes before deployer stage... because deployer uses --from=builder which means it has a dependecy on builder stage to copy some files in this case.

  • Related