I recently started getting into Docker. I am a NodeJS developer so thats what i focused my research on. I found out that people often do the following :
COPY package*.json ./
RUN npm install
COPY . .
Why cant we just use one COPY? I would expect it to look like this :
COPY . .
RUN npm install
Doesnt this copy the package.json too?
It's because of caching. Each line in the Dockerfile creates a layer of the image.
By writing your COPY
of the package.json
and RUN npm install
in their own lines, you don't execute those two commands unless package.json
changes again and thus you get a speed up of the build process!
Reason to improve the your image building time in your docker and use the existing cached. Let me tell you some real-time example what happens when you do the below command while building the image
COPY . .
RUN npm install
npm install
will run again and pull all the dependencies again Above Slows the building image
Solution Below
COPY package*.json ./
RUN npm install
When you Copy the package*.json it will compare the old one and new one
if there is no change it won't flush cache and npm install will remains the same
For the purpose of the speeding up building the image
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With