我是 Docker 和 NodeJS 新手,我有一个正在尝试容器化的应用程序,但是当我尝试在 docker 环境中加载该应用程序时,它会抛出此错误:
app-1 | node:internal/modules/cjs/loader:1148
app-1 | throw err;
app-1 | ^
app-1 |
app-1 | Error: Cannot find module '/usr/src/app/pnpm'
app-1 | at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
app-1 | at Module._load (node:internal/modules/cjs/loader:986:27)
app-1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
app-1 | at node:internal/main/run_main_module:28:49 {
app-1 | code: 'MODULE_NOT_FOUND',
app-1 | requireStack: []
app-1 | }
app-1 |
app-1 | Node.js v20.13.1
下面是我的 Dockerfile 和
docker-compose.yml
文件:
Dockerfile:
FROM node:20.13.1-bookworm-slim as base
WORKDIR /usr/src/app
COPY package.json pnpm-lock.yaml* ./
RUN apt-get update && \
apt-get install -y openssl
RUN npm install -g npm@latest && \
npm install -g pnpm && \
pnpm install && pnpm --version && \
echo "PNPM Path: $(which pnpm)"
COPY . .
RUN which pnpm
RUN pnpx prisma generate --schema=src/prisma/schema.prisma
RUN pnpm run build
# Production stage
FROM node:20.13.1-bookworm-slim as final
WORKDIR /usr/src/app
COPY --from=base /usr/src/app/dist ./dist
COPY --from=base /usr/src/app/node_modules ./node_modules
COPY --from=base /usr/src/app/package.json ./
CMD ["pnpm", "run", "dev"]
docker-compose.yml
文件:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
env_file:
- .env
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres -d ${DB_NAME}" ]
interval: 5s
timeout: 10s
retries: 5
pgadmin:
image: dpage/pgadmin4
environment:
- [email protected]
- PGADMIN_DEFAULT_PASSWORD=admin
ports:
- "5040:80"
volumes:
db-data:
# DB_HOST=localhost
# DB_PORT=5432
# DB_NAME=shuttle
# DB_USER=postgres
# DB_PASSWORD=2030
现在,一开始当我收到这个错误时,我认为这可能是我的
pnpm
在docker环境中安装的方式,所以我尝试在启动容器时添加安装pnpm
的命令,如下所示: dockerfile 中的 CMD ["npm install pnpm && pnpm install && pnpm run dev"] to the
CMD` 行,但它没有帮助,它所做的只是运行安装并给了我这个:
app-1 |
app-1 | removed 25 packages, and changed 74 packages in 54s
app-1 |
app-1 | 22 packages are looking for funding
app-1 | run `npm fund` for details
app-1 exited with code 0
这不是我想要的。我必须尝试更新我的
docker-compose.yml
文件以包含命令,但它仍然做同样的事情。我在某处读到,我不应该向 CMD 或命令脚本添加太多命令,但是删除所有内容让我回到最初的错误消息。
有人可以帮我理解我做错了什么吗,我已经这样做了两天了,谢谢。
对我有用的是:
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN apt-get update && apt-get install -y python3 build-essential openssl
ENV PYTHON=/usr/bin/python3
# Enable corepack for package management (including pnpm)
RUN corepack enable && corepack prepare [email protected] --activate
# Set the working directory
WORKDIR /usr/src/app
# Production dependencies fetch stage
FROM base as prod
# Copy only necessary files for installing dependencies
COPY package.json pnpm-lock.yaml* /usr/src/app/
# Install only production dependencies using pnpm
RUN pnpm install --frozen-lockfile
RUN pnpm fetch
# Copy the rest of the application code
COPY . .