容器化 Node.js 應用程式
先決條件
- 您已安裝最新版本的 Docker Desktop。
- 您擁有一個 git 客戶端。本節中的示例使用基於命令列的 git 客戶端,但您可以使用任何客戶端。
概述
本節將引導您完成容器化和執行 Node.js 應用程式的過程。
獲取示例應用程式
克隆示例應用程式以用於本指南。開啟終端,更改目錄到您要工作的目錄,並執行以下命令克隆儲存庫
$ git clone https://github.com/docker/docker-nodejs-sample
初始化 Docker 資源
現在您已經擁有了應用程式,可以建立必要的 Docker 資源來容器化您的應用程式。您可以使用 Docker Desktop 內建的 Docker Init 功能來幫助簡化流程,也可以手動建立資源。
在 docker-nodejs-sample
目錄中,在終端中執行 docker init
命令。docker init
提供了一些預設配置,但您需要回答一些有關您的應用程式的問題。請參考以下示例以回答 docker init
中的提示,並使用相同的答案來回答您的提示。
$ docker init
Welcome to the Docker Init CLI!
This utility will walk you through creating the following files with sensible defaults for your project:
- .dockerignore
- Dockerfile
- compose.yaml
- README.Docker.md
Let's get started!
? What application platform does your project use? Node
? What version of Node do you want to use? 18.0.0
? Which package manager do you want to use? npm
? What command do you want to use to start the app: node src/index.js
? What port does your server listen on? 3000
如果您沒有安裝 Docker Desktop 或更喜歡手動建立資源,您可以在專案目錄中建立以下檔案。
建立一個名為 Dockerfile
的檔案,內容如下。
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.net.tw/go/dockerfile-reference/
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
ARG NODE_VERSION=18.0.0
FROM node:${NODE_VERSION}-alpine
# Use production node environment by default.
ENV NODE_ENV production
WORKDIR /usr/src/app
# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.npm to speed up subsequent builds.
# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into
# into this layer.
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci --omit=dev
# Run the application as a non-root user.
USER node
# Copy the rest of the source files into the image.
COPY . .
# Expose the port that the application listens on.
EXPOSE 3000
# Run the application.
CMD node src/index.js
建立一個名為 compose.yaml
的檔案,內容如下。
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Docker Compose reference guide at
# https://docs.docker.net.tw/go/compose-spec-reference/
# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
server:
build:
context: .
environment:
NODE_ENV: production
ports:
- 3000:3000
# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker-compose up`.
# depends_on:
# db:
# condition: service_healthy
# db:
# image: postgres
# restart: always
# user: postgres
# secrets:
# - db-password
# volumes:
# - db-data:/var/lib/postgresql/data
# environment:
# - POSTGRES_DB=example
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
# expose:
# - 5432
# healthcheck:
# test: [ "CMD", "pg_isready" ]
# interval: 10s
# timeout: 5s
# retries: 5
# volumes:
# db-data:
# secrets:
# db-password:
# file: db/password.txt
建立一個名為 .dockerignore
的檔案,內容如下。
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.net.tw/go/build-context-dockerignore/
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.next
**/.cache
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose.y*ml
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/build
**/dist
LICENSE
README.md
現在您應該至少在 docker-nodejs-sample
目錄中擁有以下內容。
├── docker-nodejs-sample/
│ ├── spec/
│ ├── src/
│ ├── .dockerignore
│ ├── .gitignore
│ ├── compose.yaml
│ ├── Dockerfile
│ ├── package-lock.json
│ ├── package.json
│ └── README.md
要了解有關這些檔案的更多資訊,請參閱以下內容
執行應用程式
在 docker-nodejs-sample
目錄中,在終端中執行以下命令。
$ docker compose up --build
開啟瀏覽器,並在 https://:3000 檢視應用程式。您應該看到一個簡單的待辦事項應用程式。
在終端中,按 ctrl
+c
停止應用程式。
在後臺執行應用程式
您可以透過新增 -d
選項將應用程式從終端分離執行。在 docker-nodejs-sample
目錄中,在終端中執行以下命令。
$ docker compose up --build -d
開啟瀏覽器,並在 https://:3000 檢視應用程式。
您應該看到一個簡單的待辦事項應用程式。
在終端中,執行以下命令停止應用程式。
$ docker compose down
有關 Compose 命令的更多資訊,請參閱 Compose CLI 參考。
總結
在本節中,您學習瞭如何使用 Docker 容器化和執行 Node.js 應用程式。
相關資訊
下一步
在下一節中,您將學習如何使用容器開發應用程式。