每当执行docker构建时,总是会出现依赖性错误



当我执行docker构建时,我得到的是:

Sending build context to Docker daemon  263.5MB
Step 1/19 : FROM node:alpine3.16 AS development
---> 789fb8adc830
Step 2/19 : WORKDIR /usr/src/app
---> Using cache
---> 75ce41f126cc
Step 3/19 : COPY --chown=node:node package*.json ./
---> 802474abc3db
Step 4/19 : RUN npm ci
---> Running in 74111097fd82
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: ajv-keywords@3.5.2
npm WARN Found: peer ajv@"^6.9.1" from the root project
npm WARN 
npm WARN Could not resolve dependency:
npm WARN peer ajv@"^6.9.1" from the root project
npm ERR! code EAI_AGAIN
npm ERR! syscall getaddrinfo
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/ajv failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-09-10T08_30_14_350Z-debug-0.log
The command '/bin/sh -c npm ci' returned a non-zero code: 1

这是用于构建图像的docker文件

FROM node:alpine3.16 AS development
# Create app directory
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./
# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci
# Bundle app source
COPY --chown=node:node . .
# Use the node user from the image (instead of the root user)
USER node

FROM node:alpine3.16 AS build
WORKDIR /usr/src/app
COPY --chown=node:node package*.json ./
# In order to run `npm run build` we need access to the Nest CLI which is a dev dependency. In the previous development stage we ran `npm ci` which installed all dependencies, so we can copy over the node_modules directory from the development image
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules
COPY --chown=node:node . .
# Run the build command which creates the production bundle
RUN npm run build
# Set NODE_ENV environment variable
ENV NODE_ENV production
# Running `npm ci` removes the existing node_modules directory and passing in --only=production ensures that only the production dependencies are installed. This ensures that the node_modules directory is as optimized as possible
RUN npm ci --only=production --omit=dev && npm cache clean --force
USER node

FROM node:alpine3.16 AS production
# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
# Start the server using the production build
CMD [ "node", "dist/main.js" ]

我该如何解决此问题?当在VSCode 中构建和执行服务时,此问题永远不会发生

您很可能遇到此问题:https://github.com/npm/cli/issues/4998.

现在,使用--force--legacy-peer-deps选项应该可以帮你。

最新更新