溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

一步步教你利用Docker設(shè)置Node.js

發(fā)布時(shí)間:2020-09-05 17:31:59 來(lái)源:腳本之家 閱讀:177 作者:banq 欄目:web開(kāi)發(fā)

前言

docker是一個(gè)開(kāi)源的應(yīng)用容器引擎,可以為我們提供安全、可移植、可重復(fù)的自動(dòng)化部署的方式。docker采用虛擬化的技術(shù)來(lái)虛擬化出應(yīng)用程序的運(yùn)行環(huán)境。如上圖一樣。docker就像一艘輪船。而輪船上面的每個(gè)小箱子可以看成我們需要部署的一個(gè)個(gè)應(yīng)用。使用docker可以充分利用服務(wù)器的系統(tǒng)資源,簡(jiǎn)化了自動(dòng)化部署和運(yùn)維的繁瑣流程,減少很多因?yàn)殚_(kāi)發(fā)環(huán)境中和生產(chǎn)環(huán)境中的不同引發(fā)的異常問(wèn)題。從而提高生產(chǎn)力。

docker三個(gè)核心概念如下:

  • 鏡像(images):一個(gè)只讀的模板,可以理解為應(yīng)用程序的運(yùn)行環(huán)境,包含了程序運(yùn)行所依賴的環(huán)境和基本配置。相當(dāng)于上圖中的每個(gè)小箱子里面裝的東西。
  • 倉(cāng)庫(kù)(repository):一個(gè)用于存放鏡像文件的倉(cāng)庫(kù)??梢钥醋龊蚲itlab一樣。
  • 容器(container):一個(gè)運(yùn)行應(yīng)用程序的虛擬容器,他和鏡像最大的區(qū)別在于容器的最上面那一層是可讀可寫的。 相當(dāng)于上圖中的每個(gè)小箱子里。

本文主要是教大家了解如何在Docker容器中設(shè)置Node JS:

有一個(gè)可運(yùn)行工作的NodeJS應(yīng)用程序

通過(guò)確保進(jìn)程在出錯(cuò)時(shí)不退出,使節(jié)點(diǎn)應(yīng)用程序具有彈性

通過(guò)在代碼更改時(shí)自動(dòng)重新啟動(dòng)服務(wù)器,使Node應(yīng)用程序易于使用

利用Docker:

  • 快速設(shè)置與生產(chǎn)相同的開(kāi)發(fā)環(huán)境。
  • 輕松地能夠在本地和服務(wù)器上切換節(jié)點(diǎn)版本
  • Docker的所有其他 好處

先決條件

Docker已經(jīng)安裝好了

至少入門級(jí)節(jié)點(diǎn)知識(shí)和NPM

1.獲取一個(gè)簡(jiǎn)單的Node應(yīng)用程序

我們將使用Express,因?yàn)樗脑O(shè)置是容易的。

在一個(gè)干凈的目錄中,讓我們從初始化NPM開(kāi)始,繼續(xù)運(yùn)行此命令并按照提示進(jìn)行操作:

npm init

安裝Express:

npm install --save-prod express

編制代碼src/index.js

<b>const</b> express = require('express')
<b>const</b> app = express()
<b>const</b> port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => {console.log(`Example app listening on port ${port}!`))

啟動(dòng)一個(gè)偵聽(tīng)端口3000并使用Hello World響應(yīng)的"/"這個(gè)URL路由。

2.設(shè)置Docker以運(yùn)行我們的Node應(yīng)用程序

我們將使用docker-compose.yml文件來(lái)啟動(dòng)和停止我們的Docker容器,而不是鍵入長(zhǎng)長(zhǎng)的Docker命令。您可以將此文件視為多個(gè)Docker容器的配置文件。

docker-compose.yml:

version: "3"
services:
 app:
 container_name: app # How the container will appear when listing containers from the CLI
 image: node:10 # The <container-name>:<tag-version> of the container, in this case the tag version aligns with the version of node
 user: node # The user to run as in the container
 working_dir: "/app" # Where to container will assume it should run commands and where you will start out if you go inside the container
 networks:
 - app # Networking can get complex, but for all intents and purposes just know that containers on the same network can speak to each other
 ports:
 - "3000:3000" # <host-port>:<container-port> to listen to, so anything running on port 3000 of the container will map to port 3000 on our localhost
 volumes:
 - ./:/app # <host-directory>:<container-directory> this says map the current directory from your system to the /app directory in the docker container
 command: "node src/index.js" # The command docker will execute when starting the container, this command is not allowed to exit, if it does your container will stop

networks:
 app:

讓我們用這個(gè)命令啟動(dòng)docker容器。在后臺(tái)運(yùn)行(-d)

docker-compose up -d

在瀏覽器中訪問(wèn)http://localhost:3000并看到 Hello World!

3. 使得應(yīng)用變得彈性

如果您之前使用過(guò)Node,那么您可能知道如果應(yīng)用程序中發(fā)生錯(cuò)誤(如未捕獲的異常),那么它將關(guān)閉該Node進(jìn)程。這對(duì)我們來(lái)說(shuō)真的是個(gè)壞消息,因?yàn)槲覀兊拇a中肯定會(huì)有一個(gè)錯(cuò)誤,并且無(wú)法保證我們的代碼100%無(wú)錯(cuò)誤。此問(wèn)題的解決方案通常是另一個(gè)監(jiān)視我們的Node應(yīng)用程序并在其退出時(shí)重新啟動(dòng)它的過(guò)程。有這么多的解決方案,比如linux的supervisord,NPM包永遠(yuǎn)和PM2等......我們只需要為本指南選擇一個(gè)。

將專注于 PM2, 因?yàn)槲易钍煜に诉M(jìn)程管理之外還有一些其他功能,例如文件監(jiān)視,這將在下一節(jié)中派上用場(chǎng)。

安裝PM2

npm install --save-prod pm2

PM2可以通過(guò)命令行使用,但我們將設(shè)置一個(gè)簡(jiǎn)單的配置文件,就像我們使用docker-compose.yml文件一樣,以防止我們重復(fù)輸入長(zhǎng)命令

ecosystem.config.js:

const path = require('path')

module.exports = {
 apps: [{
 name: 'app',
 script: 'src/index.js', // Your entry point
 instances: 1,
 autorestart: true, // THIS is the important part, this will tell PM2 to restart your app if it falls over
 max_memory_restart: '1G'
 }]
}

現(xiàn)在我們應(yīng)該更改docker-compose.yml文件以使用PM2啟動(dòng)我們的應(yīng)用程序,而不是直接從index.js啟動(dòng)它。

docker-compose.yml(僅更改了的選項(xiàng))

version: "3"
services:
 app:
 container_name: app # How the container will appear when listing containers from the CLI
 image: node:10 # The <container-name>:<tag-version> of the container, in this case the tag version aligns with the version of node
 user: node # The user to run as in the container
 working_dir: "/app" # Where to container will assume it should run commands and where you will start out if you go inside the container
 networks:
 - app # Networking can get complex, but for all intents and purposes just know that containers on the same network can speak to each other
 ports:
 - "3000:3000" # <host-port>:<container-port> to listen to, so anything running on port 3000 of the container will map to port 3000 on our localhost
 volumes:
 - ./:/app # <host-directory>:<container-directory> this says map the current directory from your system to the /app directory in the docker container
 command: "npx pm2 start ecosystem.config.js --no-daemon" # The command docker will execute when starting the container, this command is not allowed to exit, if it does your container will stop

networks:
 app:

更改docker-compose.yml文件不會(huì)影響已經(jīng)運(yùn)行的容器。為了進(jìn)行更改,您應(yīng)該重新啟動(dòng)容器:

docker-compose restart

4.使我們的應(yīng)用程序易于開(kāi)發(fā)

您可能已經(jīng)注意到,一旦Node進(jìn)程啟動(dòng),那么在重新啟動(dòng)Node進(jìn)程之前,更改代碼實(shí)際上并沒(méi)有做任何事情,對(duì)于我們而言,每次都會(huì)涉及重新啟動(dòng)Docker容器以激活我們做出的改變。如果我們?cè)谶M(jìn)行代碼更改時(shí)自動(dòng)為我們重新啟動(dòng)Node進(jìn)程,那將是理想的選擇。

在過(guò)去,我已經(jīng)完成了諸如引入文件監(jiān)視實(shí)用程序和使用該文件監(jiān)視實(shí)用程序來(lái)重新啟動(dòng)Docker進(jìn)行文件更改之類的操作,或者我會(huì)使用Nodemon但是在使用Docker時(shí)會(huì)有一些警告。

最近,當(dāng)文件發(fā)生變化時(shí),我一直在使用PM2來(lái)重新啟動(dòng)我的Node進(jìn)程,而且由于我們已經(jīng)從上一步中獲取了它,因此我們不必安裝另一個(gè)依賴項(xiàng)。

ecosystem.config.js(僅添加了watch選項(xiàng)):

const path = require('path')

module.exports = {
 apps: [{
  name: 'app',
  script: 'src/index.js',
  instances: 1,
  autorestart: true,
  watch: process.env.NODE_ENV !== 'production' ? path.resolve(__dirname, 'src') : false,
  max_memory_restart: '1G'
 }]
}

如果我們沒(méi)有將NODE_ENV環(huán)境變量設(shè)置為production,則上面的配置文件現(xiàn)在將監(jiān)視src目錄。您可以通過(guò)更改index.js文件來(lái)測(cè)試它,除了Hello World之外還可以將其他內(nèi)容打印到瀏覽器中!。在此之前,您需要重新啟動(dòng)Docker容器,因?yàn)槟牧薖M2運(yùn)行容器的方式:

docker-compose restart

重新啟動(dòng)Node進(jìn)程可能需要一秒鐘才能完成,如果你想觀察它何時(shí)完成,你可以看到你的Docker日志告訴PM2何時(shí)完成重啟你的Node Process:

docker-compose logs -f

總結(jié)

  • 我們的目標(biāo)之一是能夠輕松更改Node版本,您可以通過(guò)更改docker-compose.yml文件中的image選項(xiàng)來(lái)完成此操作。
  • 本地安裝依賴項(xiàng)是使用本地NPM和Node版本完成的,如果您的本地版本與Dockers不同,有時(shí)可能會(huì)導(dǎo)致沖突。使用相同的Docker容器來(lái)安裝依賴項(xiàng)更安全。您可以使用此命令來(lái)使用該容器來(lái)安裝依賴項(xiàng),然后將其刪除
docker run --rm -i -v <absolute-path-to-your-project-locally>:/app -w /app node:10 npm install
  • 如上所述,具有與Docker運(yùn)行的Node不同的本地版本可能是有問(wèn)題的。最好在容器內(nèi)部運(yùn)行命令以保持一致性。你可以進(jìn)入一個(gè)容器
docker exec -it app bash

上面的命令將把你放到容器中,這樣你就可以繼續(xù)從里面運(yùn)行命令,即npm run start或npm run test

如果您不想進(jìn)入容器內(nèi)部,可以運(yùn)行這樣的命令

docker exec -t app bash -c "npm run start"

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI