Npm webpack build failes in docker multistage setup when build folder is going to be copied

54 Views Asked by At

I run into a strange npm / yarn / webpack build error in a docker multistage build process.

  • Docker version 24.0.5
  • Docker Compose version v2.17.2
  • DOCKER_BUILDKIT=1

I use docker-compose up --build but I get the same result when building with docker build.

Scenario:

  1. Build a node_builder and install node_modules
FROM node:${NODE_VERSION}-bullseye-slim AS node_builder
WORKDIR /srv/assets
COPY --link --chown=node:node ./src/assets/ .
ENV PATH=/srv/bin:/srv/assets/node_modules/.bin:$PATH
RUN --mount=type=ssh,uid=1000,gid=1000 \\
    yarn install --no-audit

Result: node_modules are present, path is correct.

  1. Build a different stage from node_builder and run the build command (using webpack)
FROM node_builder AS assets
COPY --link --chown=node:node ./public/view/assets public/view/assets
RUN yarn run build:website

Result: Everything as expected, /srv/assets/public/view/build has been build correctly.

  1. Build another stage from nginx
FROM nginx:${NGINX_VERSION}-alpine AS nginx_builder
WORKDIR /srv/app

FROM nginx_builder AS nginx
RUN mkdir -p public/view
COPY --from=assets --chown=www-data:www-data /srv/assets/public/view/build /srv/app/public/view/build

Result:

=> ERROR [assets 3/3] RUN yarn run build:website
[assets 3/3] RUN yarn run build:website:                                                         
yarn run v1.22.19        
sh: 1: webpack: not found

When I remove the last COPY command everything again runs as expected.

How can a COPY-command in a later stage lead to an error in this build process?

Edit

Here is the complete Dockerfile
#syntax=docker/dockerfile:1.4
ARG NODE_VERSION=16.19
ARG ALPINE_VERSION=3.21
ARG PHP_VERSION=7.4.32
ARG NGINX_VERSION=1.23.4
ARG APP_DIR="/srv/app"

# -------------------------------------------------------------
# nodejs build default image
# -------------------------------------------------------------
FROM node:${NODE_VERSION}-bullseye-slim AS node_builder

WORKDIR /srv/assets

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    tini openssh-client git vim \
    && rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man \
    && apt-get clean \
    && chown node:node -R /srv/assets \
    && npm install -g [email protected]

COPY --link .docker/node/docker-entrypoint.sh /usr/local/bin/docker-node-entrypoint

USER node

ARG NODE_ENV="production"
ARG COMPOSE_PROJECT_NAME="example"
ENV NODE_ENV="${NODE_ENV}" \
    GIT_SSH_COMMAND="ssh" \
    USER="node" \
    PATH=/srv/bin:/srv/assets/node_modules/.bin:$PATH

RUN mkdir -p ~/.ssh ~/.cache/yarn/v6 && chmod 0700 ~/.ssh \
    && ssh-keyscan -H git.example.de >> ~/.ssh/known_hosts

COPY --link --chown=node:node ./src/assets/ .
COPY --link --chown=node:node ./src/tmpl /srv/tmpl

RUN --mount=type=ssh,uid=1000,gid=1000 \
    yarn install --no-audit

ENTRYPOINT ["/usr/bin/tini", "--", "docker-node-entrypoint"]

# -------------------------------------------------------------
# node assets image
# -------------------------------------------------------------
FROM node_builder AS assets
WORKDIR /srv/assets
ARG NODE_ENV="production"
ENV NODE_ENV="${NODE_ENV}"

ARG NAMESPACE="eos"
ENV NAMESPACE=${NAMESPACE}
ARG PATH_PUBLIC="/"
ENV PATH_PUBLIC=${PATH_PUBLIC}

COPY --link --chown=node:node ./public/view/assets public/view/assets

RUN yarn run build:website

## -------------------------------------------------------------
## nginx build default image
## -------------------------------------------------------------
FROM nginx:${NGINX_VERSION}-alpine AS nginx_builder

COPY --link .docker/nginx/preconfigure-user.sh /preconfigure-user.sh
COPY --link .docker/nginx/nginx.conf /etc/nginx/nginx.conf
COPY --link .docker/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf

RUN chmod +x /preconfigure-user.sh && /preconfigure-user.sh && rm /preconfigure-user.sh

WORKDIR /srv/app

## -------------------------------------------------------------
## nginx image
## -------------------------------------------------------------
FROM nginx_builder AS nginx

RUN mkdir -p public/view
COPY --from=assets --chown=www-data:www-data /srv/assets/public/view/build /srv/app/public/view/build
0

There are 0 best solutions below