[JAVA] Build a Node.js environment with Docker

Introduction

What you are doing is almost the same as what is written in Node.js official page. Please note.

Rough procedure

  1. Create a Node.js application
  2. Creating a Dockerfile
  3. Specify files to ignore when building Docker images
  4. Build a Docker image
  5. Start the Docker container
  6. Access confirmation

Creating a Node.js application

First, create a node.js application locally.

Creating a working directory

Create a new directory for creating a Node.js application and move to it.

$mkdir directory name
$cd directory name

Create package.json

$ 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.

Check npm version

$ npm --version

--Version 5 or later is recommended (because package-lock.json is created)

run npm install

$ 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

Creating server.js

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.

Creating a Dockerfile

Create a Dockerfile in the directory where you created the js file in the above procedure.

Get the original image

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)

Creating an application directory

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)

Install the required libraries for your application

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)

Copy the application source code into the image

Dockerfile


COPY . .

--After running the process npm install, copy (bundle) the contents of the working directory into the image.

Specify the port number to be exposed to the outside

Dockerfile


EXPOSE 8080

--The role is the same as specifying the -p option when executing the docker run command.

Execute the created js

Dockerfile


CMD [ "node", "server.js" ]

Contents of the finally completed Dockerfile

Dockerfile


FROM node:lts

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

CMD [ "node", "server.js" ]

Specify files to ignore when building Docker image

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 Docker image

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

Start Docker container

$ 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 

Access confirmation

$ 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

Check the log

$ docker logs node_container
Running on http://0.0.0.0:8080

Reference site

-Make Node.js Web Application Docker -Three points for improving lead time in Docker Build

Recommended Posts

Build a Node.js environment with Docker
Build a PureScript development environment with Docker
Build a Wordpress development environment with Docker
Build docker environment with WSL
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Create a Vue3 environment with Docker!
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Build PlantUML environment with VSCode + Docker
Easily build a Vue.js environment with Docker + Vue CLI
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
[Note] Build a Python3 environment with Docker in EC2
Build docker + laravel environment with laradock
Build a Node-RED environment with Docker to move and understand
Create a MySQL environment with Docker from 0-> 1
Build a WAS execution environment from Docker
[Docker] Create Node.js + express + webpack environment with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
How to build Rails 6 environment with Docker
Build a simple Docker + Django development environment
Build a development environment for Django + MySQL + nginx with Docker Compose
Build a development environment for Docker + Rails6 + Postgresql
[Memo] Create a CentOS 8 environment easily with Docker
Build a simple Docker Compose + Django development environment
[Win10] Build a JSF development environment with NetBeans
Prepare a scraping environment with Docker and Java
Build a development environment for Docker, java, vscode
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Create a Spring Boot development environment with docker
Build a Java development environment with VS Code
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Pytorch execution environment with Docker
[Docker] Rails 5.2 environment construction with docker
Ruby ① Build a Windows environment
React environment construction with Docker
Build DynamoDB local with Docker
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
How to build docker environment with Gradle for intelliJ
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
[Note] Create a java environment from scratch with docker
Build Java development environment with WSL2 Docker VS Code
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Docker] Build a Wordpress environment locally (Win, Mac) quickly
[Environment construction] Build a Java development environment with VS Code!
Build WordPress environment with Docker (Local) and AWS (Production)
Try to build a Java development environment using Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript