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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to use Docker to deploy Node applications efficiently". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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). (3000, () = > console.log (3000))

"package.json"

Configure npm start to start the application

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

But this is only the simplest Node application, and there are all kinds of data storage and scheduled task scheduling in the real environment, which is enough to put aside.

A slightly more complex Node application can check out Yamayue's project whoami [5]: a simplified example of serverless and dockerize.

NODE_ENV=production

In a production environment, there is no need to install dependencies in devDependecies, and devDep is skipped when the NODE_ENV environment variable is set to production.

# install only the production environment dependent on $NODE_ENV=production npm ci by setting the environment variable # by explicitly specifying flag, only the production environment depends on $npm ci-- production

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

A simple deployment of a Node application

A typical server-oriented Node application runs like this:

Npm install

Npm run config, pull the configuration from the configuration service (consul/vault), such as the database and cached account password. In this case, you need to configure service permissions to build the server.

Npm run migrate, database migration script, perform database table column row change operation, and database access is required to build the server.

Npm start, start a Node service

Translate the run steps into Dockerfile:

# Select a small image (~ 5MB) FROM node:12-alpine # environment variable set to production environment ENV NODE_ENV production WORKDIR / code # better use cache ADD package.json package-lock.json / code RUN npm ci ADD according to Image Layer. / code # configuration Services and Database Migration RUN npm run config-- if-present & & npm run migrate-- if-present EXPOSE 3000 CMD npm start

This is enough for most Node applications. If you keep improving, you can move on to the next multi-stage build.

Node-gyp and Native Addon

There may be some Native Addon in Node that are compiled through node-gyp, and it depends on python,make and Gmail +.

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

In a mirror build with a compilation process, both the source file and the build tool cause a waste of space. Space can be used efficiently with the "multi-phase build" of mirrors. The construction of Go App and FE App also follows this rule.

Multi-stage construction of Go applications [6]

Multi-stage construction of front-end applications [7]

When building an Node application image, the first layer image is used to construct the node_modules.

# Select a small image (~ 5MB) FROM node:12-alpine as builder # environment variable set to production environment ENV NODE_ENV production # better use cache ADD package.json package-lock.json. / RUN npm ci # based on Image Layer to build the second stage FROM node:12-alpine WORKDIR / code ENV NODE_ENV production ADD. . COPY-from=builder node_modules node_modules # configuration service and database migration RUN npm run config-if-present & & npm run migrate-if-present EXPOSE 3000 CMD npm start "how to use Docker to deploy Node applications efficiently" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report