Garvit Arora
Garvit Arora

Reputation: 135

Logs for failed Docker build

I have a simple nodejs app. While trying to load the libraries i used npm link library1 library2 which created symlinks to /usr/local/lib/node_modules. I am trying to build the docker image of the same using following instructions in my Dockerfile

FROM node:13-alpine
ENV MONGO_DB_USERNAME=admin \
    MONGO_DB_PWD=password
RUN mkdir -p /home/app 
COPY ./app /home/app
WORKDIR /home/app
RUN npm install
CMD ["node", "server.js"]

Now while building the image it errored out RUN npm install section with following output in the terminal

npm ERR! syscall access
npm ERR! path /home/app/node_modules/body-parser
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, access '/home/app/node_modules/body-parser'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-23T21_41_24_539Z-debug.log

I was successfully able to build the image after removing the node_modules folder in my code location. Also i was successfully able to build the image when i downloaded the libraries via npm install libary1 library2 in the code location.

My questions are

  1. Why is the image build process erroring out when i use the global libaries
  2. Where can i find the logs file indicated in the terminal. My local laptop doesn't have /root/ folder.

Upvotes: 1

Views: 8631

Answers (1)

David Maze
David Maze

Reputation: 158977

Docker containers have isolated filesystems from the host and from other containers. /usr/local/lib/node_modules in a container is completely different from the same directory on the host, and similarly, /root on the host is separate from /root in any given container. That's why the image build can't use the host-global modules, and why you're having trouble reading the detailed logs.

As far as the Dockerfile goes, the important thing is to make sure the package.json file is complete. npm link won't work; make sure you npm install body-parser and any other modules you need, and that all of the dependencies are listed out in package.json and package-lock.json.

The host's node_modules tree can be different from the image's in several ways (most notably if you're using a Linux container on a non-Linux host). You can avoid problems here, and speed up the build, by making sure node_modules is listed in a .dockerignore file in the same directory as the Dockerfile.

You say deleting the host's node_modules directory fixes the build, so excluding it from the docker build context will likely fix it as well.


If that doesn't work and you still want to see the logs, you can get a shell in a container based on the partial build, up to the last successful step. Say the output of docker build is

Step 5/7 : WORKDIR /home/app
 ---> 123456789abc
Step 6/7 : RUN npm install
 ... the error text you quoted ...

That hex number is a valid Docker image ID, so you can manually repeat the last (failing) step, starting from the results of the previous (successful) step. Run:

host$ sudo docker run --rm -it 123456789abc sh
13579bdf2468# npm install
...
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-23T21_41_24_539Z-debug.log
13579bdf2468# cat /root/.npm/_logs/2020-12-23T21_41_24_539Z-debug.log

(Very recent versions of Docker have a different build engine and somewhat different output. You may need to add --progress=plain to get more information out.)

Upvotes: 3

Related Questions