How to deploy Node.js with Docker

How to deploy Node.js with Docker

Preface

Node will be used as the middle layer in the project, and docker is used to deploy node. The key points and steps of deployment are summarized and recorded here. I will not go into details about the introduction and installation of docker. There are also many related tutorials and articles on the Internet. You can search and view them if you need them.

Project Structure

`-- docker-node
  |-- data
  |-- server
    |-- app.js
    |-- Dockerfile
    |-- process.yml
    |-- package.json
  |-- docker-compose.yml

1. Create a Node.js program

app.js

const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('hello world!'));

app.listen(3008);

2. Create a Dockerfile

FROM node:8.9-alpine

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

RUN npm set registry https://registry.npm.taobao.org/

RUN npm install 

CMD ["./node_modules/pm2/bin/pm2-docker", "process.yml"]

From the Dockerfile, we can see that we run the node application through pm2-docker. With PM2, we can monitor file changes to achieve application restart, log storage and other effects. Here we need a process.yml configuration file. For related usage, please refer to the PM2 document.

process.yml

apps:
 - script : 'app.js'
  name : 'dokcer-node'
  exec_mode: 'fork'
  watch : true
  instances: 1
  log_date_format : 'YYYY-MM-DD HH:mm Z'
  ignore_watch: ['node_modules']

3. Create docker-compose.yml

In production environments, we often use more than one container. We can manage multiple Docker containers through a configuration file, and then use docker-compose to start, stop, and restart the application.

docker-compose.yml

version: "3.0"
services:
 server_node:
  container_name: server-node
  build: 
   context: ./server
  volumes:
   - ./server:/usr/src/app
  ports:
   - "3008:3008"
  environment:
   - TZ=Asia/Shanghai

 Portainer:
  image: portainer/portainer:1.11.4
  container_name: portainer
  expose:
   - "9000"
  ports:
   - "9000:9000"
  volumes:
   - /var/run/docker.sock:/var/run/docker.sock
   - ./data/portainer:/data
  environment:
   - TZ=Asia/Shanghai

4. Start the container

After the above files are created, we execute the following command to create our image and start the container

$ docker-compose -f docker-compose.yml up -d

5. Testing

1. Access local port 3008

2. Test the automatic restart function of node when the code changes. We change "hello world!" to "restart!!!!" and refresh the page again.

3. Use Portainer to manage containers. Compared with the command line, the visual interface allows us to easily and quickly view container logs, container configuration, status, and restart and delete containers. We can access the local port 9000 (configured in docker-compese.yml), and the results are as follows.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Summary of Node.js service Docker container application practice
  • How to build docker+jenkins+node.js automated deployment environment from scratch
  • Docker uses dockerfile to start node.js application
  • How to set up Node.js with Docker
  • Detailed explanation of using Docker to deploy Node.js development environment under Windows
  • Detailed steps for quickly deploying Node.js applications on Docker
  • How to package Node.js web applications into Docker containers

<<:  An elegant way to handle WeChat applet authorization login

>>:  Detailed tutorial on how to install MySQL 5.7.18 in Linux (CentOS 7) using YUM

Recommend

8 Reasons Why You Should Use Xfce Desktop Environment for Linux

For several reasons (including curiosity), I star...

Detailed explanation of MySQL alter ignore syntax

When I was at work today, the business side asked...

Sample code for a large drop-down menu implemented in pure CSS

This is a large drop-down menu implemented purely...

Simple steps to encapsulate components in Vue projects

Table of contents Preface How to encapsulate a To...

A summary of the reasons why Mysql does not use date field index

Table of contents background explore Summarize ba...

Step by step guide to build a calendar component with React

Table of contents Business Background Using Techn...

Implementation of 2D and 3D transformation in CSS3

CSS3 implements 2D plane transformation and visua...

Summary of JavaScript JSON.stringify() usage

Table of contents 1. Usage 1. Basic usage 2. The ...

CSS menu button animation

To write a drop-down menu, click the button. The ...

Summary of English names of Chinese fonts

When using the font-family property in CSS to ref...

Detailed explanation of count(), group by, order by in MySQL

I recently encountered a problem when doing IM, a...

How to migrate sqlite to mysql script

Without further ado, I will post the code for you...

A brief discussion on whether CSS animation will be blocked by JS

The animation part of CSS will be blocked by JS, ...

Some issues we should pay attention to when designing a web page

Web design, according to personal preferences and ...

CSS3 uses animation attributes to achieve cool effects (recommended)

animation-name animation name, can have multiple ...