npm ERR! Tracker "idealTree" already exists while creating the Docker image for Node project

41,506

Solution 1

This issue is happening due to changes in NodeJS starting with version 15. When no WORKDIR is specified, npm install is executed in the root directory of the container, which is resulting in this error. Executing the npm install in a project directory of the container specified by WORKDIR resolves the issue.

Use the following Dockerfile:

# Specify a base image
FROM node:alpine

#Install some dependencies

WORKDIR /usr/app
COPY ./ /usr/app
RUN npm install

# Set up a default command
CMD [ "npm","start" ]

Solution 2

The correct answer is basically right, but when I tried it still didn't work. Here's why:

WORKDIR specifies the context to the COPY that follows it. Having already specified the context in ./usr/app it is wrong to ask to copy from ./ (the directory you are working in) to ./usr/app as this produces the following structure in the container: ./usr/app/usr/app.

As a result CMD ["npm", "start"], which is followed where specified by WORKDIR (./usr/app) does not find the package.json.

I suggest using this Dockerfile:

FROM node:alpine

WORKDIR /usr/app

COPY ./ ./

RUN npm install

CMD ["npm", "start"]

Solution 3

Global install

In the event you're wanting to install a package globally outside of working directory with a package.json, you should use the -g flag.

npm install -g <pkg>

This error may trigger if the CI software you're using like semantic-release is built in node and you attempt to install it outside of a working directory.

Solution 4

You should specify the WORKDIR prior to COPY instruction in order to ensure the execution of npm install inside the directory where all your application files are there. Here is how you can complete this:

WORKDIR /usr/app

# Install some dependencies
COPY ./ ./
RUN npm install

Note that you can simply "COPY ./ (current local directory) ./ (container directory which is now /usr/app thanks to the WORKDIR instruction)" instead of "COPY ./ /usr/app"

Now the good reason to use WORKDIR instruction is that you avoid mixing your application files and directories with the root file system of the container (to avoid overriding file system directories in case you have similar directories labels on your application directories)

One more thing. It is a good practice to segment a bit your configuration so that when you make a change for example in your index.js (so then you need to rebuild your image), you will not need to run "npm install" while the package.json has not been modified.

Your application is very basic, but think of a big applications where "npm install" should take several minutes.

In order to make use of caching process of Docker, you can segment your configuration as follows:

WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install
COPY ./ ./ 

This instructs Docker to cache the first COPY and RUN commands when package.json is not touched. So when you change for instance the index.js, and you rebuild your image, Docker will use cache of the previous instructions (first COPY and RUN) and start executing the second COPY. This makes your rebuild much quicker.

Example for image rebuild:

 => CACHED [2/5] WORKDIR /usr/app                                                                                                       0.0s
 => CACHED [3/5] COPY ./package.json ./                                                                                                 0.0s
 => CACHED [4/5] RUN npm install                                                                                                        0.0s
 => [5/5] COPY ./ ./

Solution 5

# Specify a base image
FROM node:alpine

WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm","start"]

1.This is also write if you change any of your index file and docker build and docker run it automatically change your new changes to your browser output

Share:
41,506
Jaypal Sodha
Author by

Jaypal Sodha

Being software developer, working with LoginMonitor Inc. Having more than 6 years of experience with Java Development and other frameworks like ZK, Spring, Springboot, JUnit, MSSql Server, Mockito.

Updated on January 26, 2022

Comments

  • Jaypal Sodha
    Jaypal Sodha about 2 years

    I have created one node.js project called simpleWeb. The project contains package.json and index.js.

    index.js

        const express = require('express');
        
        const app = express();
        
        app.get('/', (req, res) => {
          res.send('How are you doing');
        });
        
        app.listen(8080, () => {
          console.log('Listening on port 8080');
        });
    

    package.json

    
        {
            "dependencies": {
              "express": "*"
            },
            "scripts": {
              "start": "node index.js"
            }
          }
    

    I have also created one Dockerfile to create the docker image for my node.js project.

    Dockerfile

    # Specify a base image
    FROM node:alpine
    
    # Install some dependencies 
    COPY ./ ./
    RUN npm install
    
    # Default command
    CMD ["npm", "start"]
    

    While I am tried to build the docker image using "docker build ." command it is throwing below error.

    Error Logs

    simpleweb » docker build .                                                    ~/Desktop/jaypal/Docker and Kubernatise/simpleweb
    [+] Building 16.9s (8/8) FINISHED
     => [internal] load build definition from Dockerfile                                                                         0.0s
     => => transferring dockerfile: 37B                                                                                          0.0s
     => [internal] load .dockerignore                                                                                            0.0s
     => => transferring context: 2B                                                                                              0.0s
     => [internal] load metadata for docker.io/library/node:alpine                                                               8.7s
     => [auth] library/node:pull token for registry-1.docker.io                                                                  0.0s
     => [internal] load build context                                                                                            0.0s
     => => transferring context: 418B                                                                                            0.0s
     => [1/3] FROM docker.io/library/node:alpine@sha256:5b91260f78485bfd4a1614f1afa9afd59920e4c35047ed1c2b8cde4f239dd79b         0.0s
     => CACHED [2/3] COPY ./ ./                                                                                                  0.0s
     => ERROR [3/3] RUN npm install                                                                                              8.0s
    ------
     > [3/3] RUN npm install:
    #8 7.958 npm ERR! Tracker "idealTree" already exists
    #8 7.969
    #8 7.970 npm ERR! A complete log of this run can be found in:
    #8 7.970 npm ERR!     **/root/.npm/_logs/2020-12-24T16_48_44_443Z-debug.log**
    ------
    executor failed running [/bin/sh -c npm install]: exit code: 1
    

    The log file above it is providing one path "/root/.npm/_logs/2020-12-24T16_48_44_443Z-debug.log" where I can find the full logs.

    But, The above file is not present on my local machine.

    I don't understand what is the issue.

  • shakram02
    shakram02 over 3 years
    Please make sure to explain why an answer works, so other people can adapt for their own usecases.
  • Prince Arora
    Prince Arora over 3 years
    This issue is happening due to changes in nodejs starting version 15. When no WORKDIR is specified, npm install is executed in the root directory of the container, which is resulting in this error. Executing the npm install from within a project directory in the container resolves the issue.
  • Deekshith Anand
    Deekshith Anand about 3 years
    he has used /usr/app not ./usr/app. In the later case u end up creating duplicate sub dirs
  • mjs
    mjs about 3 years
    so what was the previous place where this was stored? does it matter?
  • Andreas Bergström
    Andreas Bergström almost 3 years
    Why would you run npm i after copying all the source files, this won't cache? I suggest you read up on Docker layers and why you should order layers from least often changed to most often changed
  • Thiago
    Thiago over 2 years
    i'm still kinda confused, isn't the root of the container the "/"? COPY ./ ./ should work, because we are copying the project files to the root dir "/"
  • Roland
    Roland over 2 years
    This answer is not that bad, because npm install needs the package.json. Unless, of course, you use the -g flag.
  • johnnyb
    johnnyb about 2 years
    This is hugely underrated. The node image doesn't do "npm install" in the root directory, so you just need a different directory.