Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Node applications efficiently by Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge about "Docker how to deploy Node applications efficiently". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

How do I deploy a Node app in production? [1]❞

A reasonable and efficient deployment scheme can not only achieve rapid upgrade, smooth handover, Load Balancer, application isolation and other deployment characteristics, but also be equipped with a mature and stable monitoring.

Kubernetes sees Node as a black box for server-side applications, which perfectly matches the above conditions, and more and more teams deploy nodes on k8s.

But before you can do that, you need to run your Node app on a Docker container, which is the subject of this chapter.

A Simple Node Application

「index.js」

A hello, world version of Node Web App

const http = require('http')const app = async (req, res) => { res.end('hello, world')}http.createServer(app).listen(3000, () => console.log(3000))

「package.json」

Configure npm start to launch apps

"scripts": { "start": "node index.js"},

But this is only the simplest Node application, there are various data storage and scheduled task scheduling in the real environment, and this is enough for the moment.

For a slightly more complex Node app, check out Yamagata's project whoami[5]: a simplified serverless vs. dockerize example.

NODE_ENV=production

in production environment, there is no need to install dependency in devDependency, devDep will be skipped when NODE_ENV environment variable is set to production.

#Install only production dependencies by setting environment variables $ NODE_ENV=production npm ci#Install only production dependencies by explicitly specifying flag $ npm ci --production

On the other hand, some third-party modules make unexpected configurations based on the NODE_ENV environment variable. So in the production environment pay attention to the configuration of this environment variable.

Simple deployment of a Node application

A typical server-oriented Node application runs like this:

npm installnpm run config (consume/vault) Pull configuration, such as database and cached account password. At this time, the server needs to configure the service permission npm run migrate. The database migration script executes the database table column change operation. At this time, the server needs database access permission npm start. Start a Node service. Translate the running steps into Dockerfile:#Select a small mirror (~5MB)FROM node: 12-alpine#environment variable set to production ENV NODE_ENV productionWORKDIR /code#better according to Image Layer utilization cache ADD package.json package-lock. json/codeRUN npm ciADD . /code#Configure services and database migration RUN npm run config --if-present && npm run migrate --if-presentEXPOSE 3000CMD npm start

This is enough for most Node applications, and if you improve, you can go to the next multi-phase construction.

node-gyp vs Native Addon

There may be Native Addons in Node, compiled via node-gyp, that rely on python, make, and g++.

$ apk --no-cache add python make g++

In a mirror build with compilation, both source files and build tools waste space. Efficient use of space thanks to mirrored "multi-stage construction." The Go App and FE App are built according to this rule.

Multi-phase build Go apps [6] Multi-phase build front-end apps [7] When building Node app images, the first layer of images is used to construct node_modules.

#Select a small mirror (~5MB)FROM node:12-alpine as builder#Environment variable set to production ENV NODE_ENV production#Better according to Image Layer Use cache ADD package.json package-lock.json ./ RUN npm ci#Phase 2 of Multiphase Build #Phase 2 of Multiphase Build #Phase 2 of Multiphase Build FROM node:12-alpineWORKDIR /codeENV NODE_ENV productionADD . .COPY --from=builder node_modules node_modules#Configuration services and database migration RUN npm run config --if-present && npm run migrate --if-presentEXPOSE 3000CMD npm start"Docker How to deploy Node applications efficiently" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report