容器化 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 的文件,内容如下。

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.cn/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 的文件,内容如下。

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.cn/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 的文件,内容如下。

.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.cn/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://127.0.0.1:3000 查看应用程序。您应该看到一个简单的待办事项应用程序。

在终端中,按 ctrl+c 停止应用程序。

在后台运行应用程序

您可以通过添加 -d 选项将应用程序从终端分离运行。在 docker-nodejs-sample 目录中,在终端中运行以下命令。

$ docker compose up --build -d

打开浏览器,并在 https://127.0.0.1:3000 查看应用程序。

您应该看到一个简单的待办事项应用程序。

在终端中,运行以下命令停止应用程序。

$ docker compose down

有关 Compose 命令的更多信息,请参阅 Compose CLI 参考

总结

在本节中,您学习了如何使用 Docker 容器化和运行 Node.js 应用程序。

相关信息

下一步

在下一节中,您将学习如何使用容器开发应用程序。