使用容器进行 Node.js 开发
先决条件
概述
在本节中,您将学习如何为您的容器化应用程序设置开发环境。这包括
- 添加本地数据库并持久化数据
- 配置您的容器以运行开发环境
- 调试您的容器化应用程序
添加本地数据库并持久化数据
您可以使用容器来设置本地服务,例如数据库。在本节中,您将更新 compose.yaml
文件以定义数据库服务和卷来持久化数据。
在 IDE 或文本编辑器中打开您的
compose.yaml
文件。取消注释与数据库相关的说明。以下是更新后的
compose.yaml
文件。重要
在本节中,请勿运行
docker compose up
,直到您被告知为止。在中间点运行该命令可能会错误地初始化您的数据库。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
注意
要详细了解 Compose 文件中的说明,请参阅 Compose 文件参考.
在 IDE 或文本编辑器中打开
src/persistence/postgres.js
。您会注意到,此应用程序使用 Postgres 数据库,需要一些环境变量才能连接到数据库。compose.yaml
文件中尚未定义这些变量。添加指定数据库配置的环境变量。以下是更新后的
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 POSTGRES_HOST: db POSTGRES_USER: postgres POSTGRES_PASSWORD_FILE: /run/secrets/db-password POSTGRES_DB: example 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
将
secrets
部分添加到server
服务下,以便您的应用程序安全地处理数据库密码。以下是更新后的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 POSTGRES_HOST: db POSTGRES_USER: postgres POSTGRES_PASSWORD_FILE: /run/secrets/db-password POSTGRES_DB: example 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 secrets: - db-password 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
在
docker-nodejs-sample
目录中,创建一个名为db
的目录。在
db
目录中,创建一个名为password.txt
的文件。此文件将包含您的数据库密码。您现在应该在
docker-nodejs-sample
目录中至少有以下内容。├── docker-nodejs-sample/ │ ├── db/ │ │ └── password.txt │ ├── spec/ │ ├── src/ │ ├── .dockerignore │ ├── .gitignore │ ├── compose.yaml │ ├── Dockerfile │ ├── package-lock.json │ ├── package.json │ └── README.md
在 IDE 或文本编辑器中打开
password.txt
文件,并指定您选择的密码。您的密码必须在一行上,没有其他行。确保文件不包含任何换行符或其他隐藏字符。确保保存对您修改的所有文件的更改。
运行以下命令启动您的应用程序。
$ docker compose up --build
打开浏览器并验证应用程序是否在 https://127.0.0.1:3000 运行。
在待办事项列表中添加一些项目以测试数据持久性。
在将一些项目添加到待办事项列表后,在终端中按
ctrl+c
停止您的应用程序。在终端中,运行
docker compose rm
删除您的容器。$ docker compose rm
运行
docker compose up
重新运行您的应用程序。$ docker compose up --build
刷新浏览器中的 https://127.0.0.1:3000 并验证待办事项是否仍然存在,即使容器被删除并再次运行。
配置和运行开发容器
您可以使用绑定挂载将源代码挂载到容器中。然后,容器可以立即看到您对代码的更改,只要您保存了一个文件。这意味着您可以运行像 nodemon 这样的进程,该进程在容器中监视文件系统更改并对其进行响应。要详细了解绑定挂载,请参阅 存储概述.
除了添加绑定挂载之外,您还可以配置 Dockerfile 和 compose.yaml
文件来安装开发依赖项并运行开发工具。
更新您的 Dockerfile 以进行开发
在 IDE 或文本编辑器中打开 Dockerfile。请注意,Dockerfile 不会安装开发依赖项,也不会运行 nodemon。您需要更新 Dockerfile 以安装开发依赖项并运行 nodemon。
与其为生产环境创建一个 Dockerfile,为开发环境创建另一个 Dockerfile,不如使用一个多阶段 Dockerfile 用于两者。
将您的 Dockerfile 更新为以下多阶段 Dockerfile。
# syntax=docker/dockerfile:1
ARG NODE_VERSION=18.0.0
FROM node:${NODE_VERSION}-alpine as base
WORKDIR /usr/src/app
EXPOSE 3000
FROM base as dev
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 --include=dev
USER node
COPY . .
CMD npm run dev
FROM base as prod
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
USER node
COPY . .
CMD node src/index.js
在 Dockerfile 中,您首先在 FROM node:${NODE_VERSION}-alpine
语句中添加一个标签 as base
。这使您可以在其他构建阶段引用此构建阶段。接下来,您添加一个名为 dev
的新构建阶段来安装您的开发依赖项并使用 npm run dev
启动容器。最后,您添加一个名为 prod
的阶段,该阶段省略了开发依赖项,并使用 node src/index.js
运行您的应用程序。要详细了解多阶段构建,请参阅 多阶段构建.
接下来,您需要更新 Compose 文件以使用新阶段。
更新您的 Compose 文件以进行开发
要使用 Compose 运行 dev
阶段,您需要更新您的 compose.yaml
文件。在 IDE 或文本编辑器中打开您的 compose.yaml
文件,然后将 target: dev
指令添加到 target
以从多阶段 Dockerfile 中定位 dev
阶段。
此外,为绑定挂载将新卷添加到服务器服务。对于此应用程序,您将从本地计算机上的 ./src
挂载到容器中的 /usr/src/app/src
。
最后,为调试发布端口 9229
。
以下是更新后的 Compose 文件。所有注释都已删除。
services:
server:
build:
context: .
target: dev
ports:
- 3000:3000
- 9229:9229
environment:
NODE_ENV: production
POSTGRES_HOST: db
POSTGRES_USER: postgres
POSTGRES_PASSWORD_FILE: /run/secrets/db-password
POSTGRES_DB: example
depends_on:
db:
condition: service_healthy
secrets:
- db-password
volumes:
- ./src:/usr/src/app/src
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
运行您的开发容器并调试您的应用程序
运行以下命令,使用对 Dockerfile
和 compose.yaml
文件的新更改运行您的应用程序。
$ docker compose up --build
打开浏览器并验证应用程序是否在 https://127.0.0.1:3000 运行。
您对本地计算机上的应用程序源文件的任何更改现在将立即反映在正在运行的容器中。
在 IDE 或文本编辑器中打开 docker-nodejs-sample/src/static/js/app.js
并将第 109 行上的按钮文本从 Add Item
更新为 Add
。
+ {submitting ? 'Adding...' : 'Add'}
- {submitting ? 'Adding...' : 'Add Item'}
刷新浏览器中的 https://127.0.0.1:3000 并验证更新后的文本是否显示。
您现在可以将检查器客户端连接到您的应用程序以进行调试。有关检查器客户端的更多详细信息,请参阅 Node.js 文档.
总结
在本节中,您了解了如何设置 Compose 文件以添加模拟数据库并持久化数据。您还学习了如何创建多阶段 Dockerfile 以及如何为开发设置绑定挂载。
相关信息
下一步
在下一节中,您将学习如何使用 Docker 运行单元测试。