容器化是將應用程序、其依賴項和庫一起捆綁在一起,以便可以像即插即用一樣在任何類型的基礎設施上使用。每個捆綁包都被稱為容器。
為什麼要將 Node.js 服務容器化?
正如上一節中所討論的,將一個Node.js 服務容器化意味著將應用程序、其依賴項、庫甚至配置捆綁到一個容器中。容器化具有以下好處:
- 可攜式。由於我們將所有應用程序要求捆綁在一個容器中,因此相同的映像可以安裝在開發、暫存和生產環境中。
- 快速輕量。容器往往比虛擬機器(VM)或裸機快得多,因為它們僅運行應用程序的要求,而歷史上,VM或裸機將啟動整個機器和所有應用程序。
- 可擴展。有了上述兩個好處,由於容器快速、高效且易於部署,因此可擴展性變得非常容易。
在本文中,我們將專注於將 Node.js 應用程式容器化。
前提條件
工具與軟體
Docker(Docker 引擎和 Docker CLI)
我們需要安裝 Docker以管理我們的容器。Docker 引擎處理運行時,CLI 可用於通過命令列進行互動。
已安裝 Node.js 和 npm/yarn(用於初始開發/測試)
我們需要已安裝 Node.js 和 npm 以安裝庫的依賴項並運行服務。
Kubernetes 或 Docker Compose 來管理多個容器(選擇性)
我們需要 Kubernetes 或 Docker Compose 來管理多個容器。
基本原理
Node.js 應用結構:
- 讀者應該已了解專案中主文件(如
app.js
或index.js
)的角色,以及其他文件例如package.json
和yarn.lock
的作用。 - 本文也不會深入探討專案的其他方面,例如控制器、中介軟體和 路由。
基礎 Docker 命令和 Dockerfile 語法
Docker 命令:
docker ps -> Lists all the containers running on the system
docker pull -> Pulls any image from the docker hub or official registry of Docker
docker build -> Creates an image from the docker file
docker run -> Starts a container from an exiting image
docker stop -> Stops a container if it has crashed or if you want to switch the container
核心 Dockerfile 指令:
FROM -> Every DockerFile
WORKDIR -> Set the working directory inside the container
COPY (or ADD) -> Transfers the application's files to the image
RUN -> Executes commands during build time
CMD -> Sets the default command to be run when the container is started from the image
EXPOSE -> Specifies the port the container listens on
ENV -> Sets environment variables used during build and runtime
上述表格涵蓋的內容以及 Node.js 結構足以開始進行容器化並部署您的 Node.js 服務。
設置 Node.js 服務
設置 Node.js 環境 是一個簡單的過程。 確保 你 已經在你的機器上安裝了 Node.js。如果 你 有任何疑問, 請參考附錄 (1)。一旦安裝完成,打開 你的 終端並通過輸入 來驗證安裝。
node -v
npm -v
創建一個項目目錄並初始化 你的 項目,如下所示:
npm init -y
安裝 express 模組
npm install express
創建一個伺服器文件, 我們稱它為 server.mjs,在這裡我們可以添加路由和對應的邏輯。 因為這篇文章更關於容器化,我們將保持端點邏輯非常簡單。像這樣:
import express from "express";
const app = express();
const port = 8080;
app.get('/', (req, res) => {
res.send('Welcome to my demo service!');
});
app.listen(port, () => {
console.log(`Demo Service is running on port ${port}`);
});
現在您的服務已準備好啟動,請在終端機中導航至專案目錄,並執行此命令:
node server.mjs
服務已啟動並運行;如果我們訪問 http://localhost:3000,我們將看到:
“歡迎來到我的伺服器“
創建 Dockerfile
讓我們回顧一下 Dockerfile 是什麼,它包含構建 docker 映像的指令。讓我們在根目錄中創建一個 Dockerfile。在這一步中,正如我們在 Dockerfile 指令中討論的,我們需要做以下幾件事:
FROM node:18-alpine => Indicate the base image to use. Here we're using the official Nodejs 14 image.
WORKDIR /usr/src/app => Sets the working directory in the container.
COPY package*.json ./ => Duplicate the package.json and package-lock.json files to the working directory.
RUN npm install => Installs the app package dependencies.
COPY . . => Copies the remaining of the app to the working directory.
EXPOSE 8080 => Exposes the port our app is listening on.
CMD ["node", "app.js"] => Defines the command to start your Node.js application.
構建和運行 Docker 映像
從終端機的根目錄,導航到您的項目並運行以下命令:
docker build -t image-name .
其中image-name
是Docker映像的名稱。末尾的.
設置了當前目錄的上下文。
映像構建完成後,我們將創建容器並運行Node.js應用程式,使用以下命令:
docker run --name container-name -p 8080:8080 image-name
完成上述步驟後,您可以通過運行docker ps
來驗證服務是否運行,然後前往與之前相同的本地主機URL。
將映像推送到註冊表
現在您的圖像已準備就緒,是時候將它推送到註冊表中了。對於本文範圍,讓我們只推送到Docker Hub。Docker Hub是一個基於雲端的服務,用於存儲、分享和管理Docker容器映像。
在https://hub.docker.com/創建帳戶並登錄。 您的帳戶。
docker login
登錄後,本地構建的映像可以像這樣添加:
docker tag image-name:tag dockerhub-username/repository-name:tag
其中:
tag
是latest
或版本號。repository-name
是所需的存儲庫名稱。
接下來,按照以下方式推送映像:
docker push dockerhub-username/repository-name:tag
結論
正如我們所看到的,容器化通過分離依賴關係,使得複雜的工作流程和服務快速、可移植和可擴展。一旦實施,整個團隊都會從中受益。我鼓勵您探索高級功能,如多階段構建和容器網絡。同時,考慮學習管控工具(例如 Kubernetes)並集成 CI/CD 流水線,以優化您的開發工作流程。
附錄
Source:
https://dzone.com/articles/containerization-of-a-nodejs-service