What you are doing is almost the same as what is written in Node.js official page. Please note.
First, create a node.js application locally.
Create a new directory for creating a Node.js application and move to it.
$mkdir directory name
$cd directory name
$ touch package.json
package.json
{
"name": "docker_node_js",
"version": "1.0.0",
"description": "Node.js on Docker",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
--This time, we will use the Express.js framework.
$ npm --version
--Version 5 or later is recommended (because package-lock.json is created)
$ npm install
--If npm version is 5 or later, package-lock.json will be created automatically after this execution. --The package is installed under the node_modules folder
Create server.js in the directory where package.json is located
$ touch server.js
server.js
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
//Listen for connections on the specified port number
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
--See here for listening to connections.
Create a Dockerfile in the directory where you created the js file in the above procedure.
Dockerfile
FROM node:lts
--This time, create an image based on the image of Node.js in Docker Hub
--Select lts
as the tag (** lts: long time support **) (See here for Laravel's lts)
Dockerfile
WORKDIR /usr/src/app
--Create a directory in the image to store the source code of this application. --It is recommended to use an absolute path (Reference)
Dockerfile
COPY package*.json ./
RUN npm install
--Node.js and NPM are already installed in the image used this time, so install the necessary packages as the next step.
--By using a wildcard (*), both package.json and package-lock.json should be copied.
--By copying only the files required for npm install
, you can use the cache when ** package * .json has not changed, so you can reduce the processing load ** (If you copy all the files, even if there is no change in package * .json, npm install
will be executed just because any file in the working directory is changed, and the cache will be used effectively. Can not do it)
Dockerfile
COPY . .
--After running the process npm install
, copy (bundle) the contents of the working directory into the image.
Dockerfile
EXPOSE 8080
--The role is the same as specifying the -p
option when executing the docker run
command.
Dockerfile
CMD [ "node", "server.js" ]
Dockerfile
FROM node:lts
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "node", "server.js" ]
In the .dockerignore file, specify the file to be ignored when building.
$ touch .dockerignore
.dockerignore
node_modules
npm-debug.log
--This description prevents locally installed modules and debug logs from being copied onto the Docker image or overwriting the image-like modules.
Build the image based on the created Dockerfile.
$ docker build -t node_tutorial:latest .
--You can add a name and tag to the image by writing it in the format of -t
: name: tag
.
Check the created image
$ docker images node_tutorial:latest
$ docker run -p 49160:8080 -d node_tutorial:latest
---p
: Associate the port specified in server.js with the port of localhost
---d
: Start in background
Check the running Docker image
$ docker ps
$ curl -i localhost:49160
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
...
Hello, World!
---i
: Output both Header and Body of Response
$ docker logs node_container
Running on http://0.0.0.0:8080
-Make Node.js Web Application Docker -Three points for improving lead time in Docker Build
Recommended Posts