In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "what is the method of deploying Node.js in Docker". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of deploying Node.js in Docker"?
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 worldview'); app.listen (3008)
2. Create dockerfile
From node:8.9-alpinerun mkdir-p / usr/src/appworkdir / usr/src/apprun npm set registry https://registry.npm.taobao.org/run npm install cmd [". / node_modules/pm2/bin/pm2-docker", "process.yml"]
From dockerfile, we can see that we run node applications through pm2-docker. With pm2, we can monitor file changes to achieve the effects of application restart and log storage. Here we need a process.yml configuration file, which can be used to view pm2 documents.
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 a production environment, 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: -" 3008container_name 3008 "environment:-tz=asia/shanghai portainer: image: portainer/portainer:1.11.4 container_name: portainer expose: -" 9000 "ports:--" 9000container_name 9000 "volumes:-/ var/run/ Docker.sock:/var/run/docker.sock -. / data/portainer:/data environment:-tz=asia/shanghai
4. Start the container
When the above files are created and we execute the following command, our image will be created and the container will be started
$docker-compose-f docker-compose.yml up-d
5. Test
1. Access local port 3008
2. Test contemporary code changes, node automatic restart function, we put "hello world!" Change it to "restartfully popular!" To refresh the page again
3. Use portainer to manage containers. Compared with the command line, we can easily and quickly view the container log, container configuration, status, restart, delete and other operations in the visual interface. We can access local port 9000 (configured in docker-compese.yml), and the results are as follows.
At this point, I believe you have a deeper understanding of "what is the method of deploying Node.js in Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.