Update build-images.md (#12843)

* Update build-images.md

Small typos in the documentation, which may lead to confusion.

* Inspiration from node.js docs

Added documentation for why we copy `package*.json` before copying the whole directory. Checkout: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/#creating-a-dockerfile. 
The node.js docs has more optimized code too (using `npm ci` instead of `npm install`) which can be used here too.

* Fixed review provided and removed some weird whitespaces
This commit is contained in:
Aaryan Porwal 2021-07-30 18:26:05 +05:30 committed by GitHub
parent 33576a4efb
commit 6531e8ef21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -3,7 +3,7 @@ title: "Build your Node image"
keywords: containers, images, node.js, node, dockerfiles, node, coding, build, push, run
description: Learn how to build your first Docker image by writing a Dockerfile
redirect_from:
- /get-started/nodejs/build-images/
- /get-started/nodejs/build-images/
---
{% include_relative nav.html selected="1" %}
@ -40,12 +40,12 @@ Now, lets add some code to handle our REST requests. Well use a mock serve
Open this working directory in your IDE and add the following code into the `server.js` file.
```js
const ronin = require( 'ronin-server' )
const mocks = require( 'ronin-mocks' )
const ronin = require('ronin-server')
const mocks = require('ronin-mocks')
const server = ronin.server()
server.use( '/', mocks.server( server.Router(), false, true ) )
server.use('/', mocks.server(server.Router(), false, true))
server.start()
```
@ -139,13 +139,14 @@ WORKDIR /app
Usually the very first thing you do once youve downloaded a project written in Node.js is to install npm packages. This ensures that your application has all its dependencies installed into the `node_modules` directory where the Node runtime will be able to find them.
Before we can run `npm install`, we need to get our `package.json` and `package-lock.json` files into our images. We use the `COPY` command to do this. The `COPY` command takes two parameters. The first parameter tells Docker what file(s) you would like to copy into the image. The second parameter tells Docker where you want that file(s) to be copied to. Well copy the `package.json` and `package-lock.json` file into our working directory `/app`.
Before we can run `npm install`, we need to get our `package.json` and `package-lock.json` files into our images. We use the `COPY` command to do this. The `COPY` command takes two parameters. The first parameter tells Docker what file(s) you would like to copy into the image. The second parameter tells Docker where you want that file(s) to be copied to. Well copy the `package.json` and `package-lock.json` file into our working directory `/app`.
```dockerfile
COPY ["package.json", "package-lock.json*", "./"]
```
Once we have our `package.json` files inside the image, we can use the `RUN` command to execute the command npm install. This works exactly the same as if we were running npm install locally on our machine, but this time these Node modules will be installed into the `node_modules` directory inside our image.
Note that, rather than copying the entire working directory, we are only copying the package.json file. This allows us to take advantage of cached Docker layers.
Once we have our files inside the image, we can use the `RUN` command to execute the command npm install. This works exactly the same as if we were running npm install locally on our machine, but this time these Node modules will be installed into the `node_modules` directory inside our image.
```dockerfile
RUN npm install --production