环境:Alibaba Cloud Linux 3.2104 LTS 64位
安装docker
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce
启动docker
systemctl start docker
编写docker file
# 使用官方 Node.js 镜像 (基于 Debian)
FROM node:20-bullseye
LABEL authors="houmo"
# 设置环境变量,确保在非交互式环境中正确运行
ENV DEBIAN_FRONTEND=noninteractive
# 更换 apt 源为清华源,并更新包列表及安装必要的软件包,包括 Playwright 所需的依赖项
RUN sed -i 's|http://deb.debian.org/debian|https://mirrors.tuna.tsinghua.edu.cn/debian|g' /etc/apt/sources.list && \
apt-get update && \
apt-get install -y \
curl \
gnupg \
ca-certificates \
lsb-release \
git \
build-essential \
libnss3 \
libnspr4 \
libdbus-1-3 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libcups2 \
libdrm2 \
libxkbcommon0 \
libatspi2.0-0 \
libxcomposite1 \
libxdamage1 \
libxfixes3 \
libxrandr2 \
libgbm1 \
libasound2 \
&& rm -rf /var/lib/apt/lists/*
# 设置工作目录
WORKDIR /usr/src/app
# 复制项目文件到容器中
COPY . .
# 更换 npm 源为清华源
RUN npm config set registry https://registry.npmmirror.com/
# 安装项目依赖
RUN npm install --loglevel verbose
# 安装 Playwright 浏览器及其依赖项
RUN npx playwright install-deps && npx playwright install
# 暴露端口
EXPOSE 3000
# 设置默认运行命令
CMD [ "npm", "run", "dev" ]
构建镜像
docker build -t app .
运行容器
docker run -d -p 3000:3000 --name app app