Search code examples
node.jsdockernpmyarnpkg-v2

How to access log files produced during docker build


I have a Dockerfile like this in my application source code

FROM node:latest

USER node
WORKDIR /work

COPY --chown=node .yarn/releases/* /work/.yarn/releases/
COPY --chown=node package.json yarn.lock .yarnrc.yml /work
RUN yarn install --immutable && yarn cache clean

# Copy source code
COPY --chown=node tsconfig* nx.json jest* /work/
COPY --chown=node apps/backend /work/apps/backend
COPY --chown=node libs /work/libs

# Copy app config
COPY --chown=node config.*.yml .env /work/

ENTRYPOINT [ "/bin/bash", "-c" ]

I use this command to build the image

docker build -t me/myimage .

When build locally on my machine, it works fine. But when building on CI server, I got this error in the RUN yarn install --immutable && yarn cache clean step

#9 80.01 ➤ YN0009: │ cypress@npm:12.14.0 couldn't be built successfully (exit code 1, logs can be found here: /tmp/xfs-c9cb8a8a/build.log)
#9 80.01 ➤ YN0009: │ esbuild@npm:0.17.19 couldn't be built successfully (exit code 1, logs can be found here: /tmp/xfs-b24e1b31/build.log)
#9 80.01 ➤ YN0009: │ @parcel/watcher@npm:2.0.4 couldn't be built successfully (exit code 1, logs can be found here: /tmp/xfs-deb3355a/build.log)
#9 80.01 ➤ YN0009: │ protobufjs@npm:6.11.3 couldn't be built successfully (exit code 1, logs can be found here: /tmp/xfs-4190fa5b/build.log)
#9 80.01 ➤ YN0007: │ nx@npm:16.3.2 [c2878] must be built because it never has been before or the last one failed
#9 80.08 ➤ YN0009: │ nx@npm:16.3.2 [c2878] couldn't be built successfully (exit code 1, logs can be found here: /tmp/xfs-a4dbfcc2/build.log)
#9 80.11 ➤ YN0007: │ amp-test-e2e@workspace:. must be built because it never has been before or the last one failed
#9 80.17 ➤ YN0009: │ amp-test-e2e@workspace:. couldn't be built successfully (exit code 1, logs can be found here: /tmp/xfs-aaad7563/build.log)
#9 80.17 ➤ YN0000: └ Completed in 21s 420ms
#9 80.19 ➤ YN0000: Failed with errors in 1m 20s
#9 ERROR: process "/bin/sh -c yarn install --immutable && yarn cache clean" did not complete successfully: exit code: 1

I want to find the /tmp/xfs-aaad7563/build.log to see what is causing the error but it seems the file cannot be found on the host machine.

Is there a way I can access these files?


Solution

  • These files are located inside of the build environment, which gets destroyed when the process ends.
    One way to access it, is to temporarily alter your Dockerfile, so instead of

    RUN yarn install --immutable && yarn cache clean
    

    you could do

    RUN yarn --immutable | grep -Eo '(/tmp/.*?\.log)' | xargs -I{} sh -c 'echo {}: && cat {}' && exit 1
    

    This won't show which log corresponds to which package, but it's usually obvious, which issue is the culprit.

    As xargs would hide the return code of yarn ... | grep ..., we'll add exit 1 at the end, so docker build would stop at the same line, and display the output.