一步步使用Docker設定Node.js
瞭解如何在Docker容器中設定Node JS:
- 有一個可執行工作的NodeJS應用程式
- 通過確保程序在出錯時不退出,使節點應用程式具有彈性
- 通過在程式碼更改時自動重新啟動伺服器,使Node應用程式易於使用
-
利用Docker:
- 快速設定與生產相同的開發環境。
- 輕鬆地能夠在本地和伺服器上切換節點版本
- Docker的所有其他ofollow,noindex" target="_blank">好處
先決條件
- Docker已經安裝好了
- 至少入門級節點知識和NPM
1.獲取一個簡單的Node應用程式
我們將使用Express,因為它的設定是容易的。
在一個乾淨的目錄中,讓我們從初始化NPM開始,繼續執行此命令並按照提示進行操作:
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}!`))
啟動一個偵聽埠3000並使用Hello World響應的"/"這個URL路由。
2.設定Docker以執行我們的Node應用程式
我們將使用docker-compose.yml檔案來啟動和停止我們的Docker容器,而不是鍵入長長的Docker命令。您可以將此檔案視為多個Docker容器的配置檔案。
docker-compose.yml:
version: <font>"3"</font><font> 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 <b>this</b> <b>case</b> the tag version aligns with the version of node user: node # The user to run as in the container working_dir: </font><font>"/app"</font><font> # Where to container will assume it should run commands and where you will start out <b>if</b> you go inside the container networks: - app # Networking can get complex, but <b>for</b> all intents and purposes just know that containers on the same network can speak to each other ports: - </font><font>"3000:3000"</font><font> # <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> <b>this</b> says map the current directory from your system to the /app directory in the docker container command: </font><font>"node src/index.js"</font><font> # The command docker will execute when starting the container, <b>this</b> command is not allowed to exit, <b>if</b> it does your container will stop networks: app: </font>
讓我們用這個命令啟動docker容器。在後臺執行(-d)
docker-compose up -d
在瀏覽器中訪問http://localhost:3000並看到 Hello World!
3. 使得應用變得彈性
如果您之前使用過Node,那麼您可能知道如果應用程式中發生錯誤(如未捕獲的異常),那麼它將關閉該Node程序。這對我們來說真的是個壞訊息,因為我們的程式碼中肯定會有一個錯誤,並且無法保證我們的程式碼100%無錯誤。此問題的解決方案通常是另一個監視我們的Node應用程式並在其退出時重新啟動它的過程。有這麼多的解決方案,比如linux的supervisord,NPM包永遠和PM2等......我們只需要為本指南選擇一個。
將專注於PM2, 因為我最熟悉它,除了程序管理之外還有一些其他功能,例如檔案監視,這將在下一節中派上用場。
安裝PM2
npm install --save-prod pm2
PM2可以通過命令列使用,但我們將設定一個簡單的配置檔案,就像我們使用docker-compose.yml檔案一樣,以防止我們重複輸入長命令
ecosystem.config.js:
<b>const</b> path = require('path') module.exports = { apps: [{ name: 'app', script: 'src/index.js', <font><i>// Your entry point</i></font><font> instances: 1, autorestart: <b>true</b>, </font><font><i>// THIS is the important part, this will tell PM2 to restart your app if it falls over</i></font><font> max_memory_restart: '1G' }] } </font>
現在我們應該更改docker-compose.yml檔案以使用PM2啟動我們的應用程式,而不是直接從index.js啟動它。
docker-compose.yml(僅更改了的選項)
version: <font>"3"</font><font> 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 <b>this</b> <b>case</b> the tag version aligns with the version of node user: node # The user to run as in the container working_dir: </font><font>"/app"</font><font> # Where to container will assume it should run commands and where you will start out <b>if</b> you go inside the container networks: - app # Networking can get complex, but <b>for</b> all intents and purposes just know that containers on the same network can speak to each other ports: - </font><font>"3000:3000"</font><font> # <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> <b>this</b> says map the current directory from your system to the /app directory in the docker container command: </font><font>"npx pm2 start ecosystem.config.js --no-daemon"</font><font> # The command docker will execute when starting the container, <b>this</b> command is not allowed to exit, <b>if</b> it does your container will stop networks: app: </font>
更改docker-compose.yml檔案不會影響已經執行的容器。為了進行更改,您應該重新啟動容器:
docker-compose restart
4.使我們的應用程式易於開發
您可能已經注意到,一旦Node程序啟動,那麼在重新啟動Node程序之前,更改程式碼實際上並沒有做任何事情,對於我們而言,每次都會涉及重新啟動Docker容器以啟用我們做出的改變。如果我們在進行程式碼更改時自動為我們重新啟動Node程序,那將是理想的選擇。
在過去,我已經完成了諸如引入檔案監視實用程式和使用該檔案監視實用程式來重新啟動Docker進行檔案更改之類的操作,或者我會使用Nodemon但是在使用Docker時會有一些警告。
最近,當檔案發生變化時,我一直在使用PM2來重新啟動我的Node程序,而且由於我們已經從上一步中獲取了它,因此我們不必安裝另一個依賴項。
ecosystem.config.js(僅添加了watch選項):
const path = require('path')
module.exports = { apps: [{ name: 'app', script: 'src/index.js', instances: 1, autorestart: <b>true</b>, watch: process.env.NODE_ENV !== 'production' ? path.resolve(__dirname, 'src') : false, max_memory_restart: '1G' }] }
如果我們沒有將NODE_ENV環境變數設定為production,則上面的配置檔案現在將監視src目錄。您可以通過更改index.js檔案來測試它,除了Hello World之外還可以將其他內容列印到瀏覽器中!。在此之前,您需要重新啟動Docker容器,因為您更改了PM2執行容器的方式:
docker-compose restart
重新啟動Node程序可能需要一秒鐘才能完成,如果你想觀察它何時完成,你可以看到你的Docker日誌告訴PM2何時完成重啟你的Node Process:
docker-compose logs -f
總結
- 我們的目標之一是能夠輕鬆更改Node版本,您可以通過更改docker-compose.yml檔案中的image選項來完成此操作。
- 本地安裝依賴項是使用本地NPM和Node版本完成的,如果您的本地版本與Dockers不同,有時可能會導致衝突。使用相同的Docker容器來安裝依賴項更安全。您可以使用此命令來使用該容器來安裝依賴項,然後將其刪除
docker run --rm -i -v <absolute-path-to-your-project-locally>:/app -w /app node:10 npm install
- 如上所述,具有與Docker執行的Node不同的本地版本可能是有問題的。最好在容器內部執行命令以保持一致性。你可以進入一個容器
docker exec -it app bash
上面的命令將把你放到容器中,這樣你就可以繼續從裡面執行命令,即npm run start或npm run test
如果您不想進入容器內部,可以執行這樣的命令
docker exec -t app bash -c "npm run start"