Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why COPY package*.json ./ precedes COPY . .?

Tags:

node.js

docker

In this Node.js tutorial on Docker: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

What is the point of COPY package*.json ./?

Isn't everything copied over with COPY . .?

The Dockerfile in question:

FROM node:8  # Create app directory WORKDIR /usr/src/app  # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./  RUN npm install # If you are building your code for production # RUN npm install --only=production  # Bundle app source COPY . .  EXPOSE 8080 CMD [ "npm", "start" ] 
like image 651
Jakub Barczyk Avatar asked Jul 26 '18 07:07

Jakub Barczyk


People also ask

What is the reason for the package json?

The package. json file is the heart of any Node project. It records important metadata about a project which is required before publishing to NPM, and also defines functional attributes of a project that npm uses to install dependencies, run scripts, and identify the entry point to our package.

Can you copy and paste package json?

You can just copy-paste the dependencies manually from one package. json | by Esau Silva | Medium.

Should I save package lock json?

json intact. It is highly recommended you commit the generated package lock to source control: this will allow anyone else on your team, your deployments, your CI/continuous integration, and anyone else who runs npm install in your package source to get the exact same dependency tree that you were developing on.

How do I use copy with JSON format data?

You can specify the following options when using COPY with JSON format data: 'auto' – COPY automatically loads fields from the JSON file. 'auto ignorecase' – COPY automatically loads fields from the JSON file while ignoring the case of field names. s3://jsonpaths_file – COPY uses a JSONPaths file to parse the JSON source data.

How does copy work with a jsonpaths file?

s3://jsonpaths_file – COPY uses a JSONPaths file to parse the JSON source data. A JSONPaths file is a text file that contains a single JSON object with the name "jsonpaths" paired with an array of JSONPath expressions. If the name is any string other than "jsonpaths", COPY uses the 'auto' argument instead of using the JSONPaths file.

What are the most important things in a JSON package?

If you plan to publish your package, the most important things in your package.json are the name and version fields as they will be required. The name and version together form an identifier that is assumed to be completely unique.

Why do I lose precision when I load data from JSON?

You might lose precision when loading numbers from data files in JSON format to a column that is defined as a numeric data type. Some floating point values aren't represented exactly in computer systems. As a result, data you copy from a JSON file might not be rounded as you expect.


1 Answers

This is a common pattern in Dockerfiles (in all languages). The npm install step takes a long time, but you only need to run it when the package dependencies change. So it's typical to see one step that just installs dependencies, and a second step that adds the actual application, because it makes rebuilding the container go faster.

You're right that this is essentially identical if you're building the image once; you get the same filesystem contents out at the end.

Say this happens while you're working on the package, though. You've changed some src/*.js file, but haven't changed the package.json. You run npm test and it looks good. Now you re-run docker build. Docker notices that the package*.json files haven't changed, so it uses the same image layer it built the first time without re-running anything, and it also skips the npm install step (because it assumes running the same command on the same input filesystem produces the same output filesystem). So this makes the second build run faster.

like image 67
David Maze Avatar answered Sep 23 '22 06:09

David Maze