Multistage docker image build fails

I’ve setup a multistage docker build using:

Dockerfile:

### STAGE 1: Build ###

# We label our stage as 'builder'
FROM node:8-alpine as builder

RUN apk update && apk add --no-cache python libnotify git openssh

COPY pom.xml .yo-rc.json package.json yarn.lock bower.json gulpfile.js /
COPY src/ /src
COPY gulp/ /gulp

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

## Storing node modules on a separate layer will prevent unnecessary npm installs at each build

## Build the angular app in production mode and store the artifacts in dist folder
RUN yarn install && mkdir /ng-app && cp -R ./node_modules /ng-app

WORKDIR /ng-app

COPY . .

### STAGE 2: Setup ###

FROM nginx:stable-alpine

## Copy our default nginx config
COPY docker/httpd/nginx.conf /etc/nginx/conf.d/default.conf

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

## From 'builder' stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /ng-app/target/www /usr/share/nginx/html

WORKDIR /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]

and .gitlab-ci.yml

variables:
  DOCKER_DRIVER: overlay2
  CONTAINER_IMAGE: registry.gitlab.com/$CI_PROJECT_PATH

cache:
  paths:
  - ./node_modules/
  - ./src/main/webapp/bower_components/

build:
  image: node:8
  stage: build
  script:
  - yarn install
  cache:
    paths:
    - ./target/www

docker-deploy-latest:
  image: docker:git
  stage: deploy
  only:
  - develop
  services:
  - docker:dind
  before_script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
  script:
  - docker pull $CONTAINER_IMAGE:latest || true
  - PROFILE=dev docker build --cache-from $CONTAINER_IMAGE:latest --tag $CONTAINER_IMAGE:$CI_BUILD_REF --tag $CONTAINER_IMAGE:latest .
  - docker push $CONTAINER_IMAGE:$CI_BUILD_REF
  - docker push $CONTAINER_IMAGE:latest

This regularely fails with the following message:

...
Step 13/14 : COPY --from=builder /ng-app/target/www /usr/share/nginx/html
COPY failed: stat /var/lib/docker/overlay2/4b63ca987af78e3854affd491056de197dc4a1442b939e27bd6688a26a53e71a/merged/ng-app/target/www: no such file or directory
ERROR: Job failed: exit code 1

REsolved: i was writing in the base image volume which is destroyes as soon as the build starts the second stage

How did you solve it? I am having the exact same problem right now.

1 Like

@luca.orlandi how did you solve this. I am able to build locally but gitlab fails with that exact error

@ luca.orlandi @ agcty @ muchwakande

As I’m having the exact same issue with Multistage docker image build that fails, I would appreciate if one of you can take the time to explain precisely what he did to fix the issue, if I can’t find a solution I will have to resort to keep using a prebuild step with gitlab-ci unfortunately.

1 Like

Take a look at this stripped down Dockerfile from another project of mine:

FROM mariadb:10.2 as builder
    
COPY mydb.sql.gz /docker-entrypoint-initdb.d/10-load_data.sql.gz
COPY anonimize.sql /docker-entrypoint-initdb.d/40-anonimize.sql

# Need to change the datadir to something else that /var/lib/mysql because the parent docker file defines it as a volume.
# https://docs.docker.com/engine/reference/builder/#volume :
#       Changing the volume from within the Dockerfile: If any build steps change the data within the volume after
#       it has been declared, those changes will be discarded.
RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db", "--aria-log-dir-path", "/initialized-db"]

FROM mariadb:10.2

COPY --from=builder /initialized-db /var/lib/mysql

here the builder is used to load and anonimize the data that will be available in the final image.