如何在我的Docker构建中包括本地软件包依赖关系?

发布于 2025-01-23 21:11:58 字数 703 浏览 4 评论 0原文

我有一个turborepo monorepo,使用纱线工作区,看起来像这样

├istry-packages

toserm-common├isterm-common├-上级

服务

││││IT服务/auth

/auth依赖套件/服务器common和使用纱线工作区,就像这样,

  "dependencies": {
    "server-common": "*",
  }

我想从服务/auth中构建一个docker映像,但是npm上的软件包/服务器common不是托管的,它是本地软件包。

因此,当构建以下Dockerfile时,

FROM node:16-alpine

WORKDIR /app
COPY package.json .
RUN yarn install --production
COPY . .

CMD ["npm", "start"]

它在纱线安装阶段失败,因为Docker是在本地软件包时尝试从NPM下载软件包的。

#9 8.441 error Received malformed response from registry for "server-common". The registry may be down.

有没有办法将此本地软件包包含在Docker Build中?

I have a turborepo monorepo, using yarn workspaces which looks like this

├── packages

│ ├── server-common

├── services

│ ├── auth

Services/auth is dependant on packages/server-common and using yarn workspaces, imports it like so

  "dependencies": {
    "server-common": "*",
  }

I want to build a docker image out of services/auth, however packages/server-common is not hosted on npm, it is a local package.

Hence, when building the following dockerfile

FROM node:16-alpine

WORKDIR /app
COPY package.json .
RUN yarn install --production
COPY . .

CMD ["npm", "start"]

It fails on the yarn install stage, because docker is trying to download a package from npm, when it is a local package.

#9 8.441 error Received malformed response from registry for "server-common". The registry may be down.

Is there a way that this local package can be included in the docker build?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福还没到 2025-01-30 21:11:58

解决方案是将您的本地软件包复制到容器中。

FROM node:16-alpine

COPY path/to/server-common .
WORKDIR /app
COPY package.json .
RUN yarn install --production
COPY . .

CMD ["npm", "start"]
"dependencies": {
    "server-common": "file:../server-common"
}

A solution would be to copy your local package into the container.

FROM node:16-alpine

COPY path/to/server-common .
WORKDIR /app
COPY package.json .
RUN yarn install --production
COPY . .

CMD ["npm", "start"]
"dependencies": {
    "server-common": "file:../server-common"
}
笨死的猪 2025-01-30 21:11:58

使用纱线工作区遇到类似问题。我最终只是构建了要在应用程序中使用的软件包,并重现了目录设置,以支持纱线工作空间使用的符号链接。

这是我的设置:

项目root

- package.json
- packages/
  - common/ (directory I want to install in either app1/app2)
  - app1/
  - app2/
- Dockerfile

package.json

{
   "private": true,
  "workspaces": [
    "packages/*"
  ],
  "scripts": {
    "build:common": "yarn workspace @monorepo/common build",
    "build:app1": "yarn workspace @monorepo/app1 build",
    "start:app1": "yarn workspace @monorepo/app start",
    "build:app2": "yarn workspace @monorepo/app2 build",
    "start:app2": "yarn workspace @monorepo/app2 start",
  },
  "name": "monorepo",
}

dockerfile

ARG BUILD_CONTEXT

FROM node:18.12.0-alpine AS builder
ARG BUILD_CONTEXT
ENV ENV_BUILD_CONTEXT=$BUILD_CONTEXT
WORKDIR /usr/app
COPY package.json ./
COPY yarn.lock ./
COPY packages/${BUILD_CONTEXT} /usr/app/packages/${BUILD_CONTEXT}
COPY packages/common /usr/app/packages/common
RUN yarn install
RUN yarn run build:common
RUN yarn run build:${BUILD_CONTEXT}


FROM node:18.12.0-alpine AS final
ARG BUILD_CONTEXT
ENV ENV_BUILD_CONTEXT=$BUILD_CONTEXT
WORKDIR /usr/app
COPY --from=builder ./usr/app/packages/${BUILD_CONTEXT}/build ./packages/${BUILD_CONTEXT}/build
COPY --from=builder ./usr/app/packages/common/build ./packages/common/build
COPY --from=builder ./usr/app/packages/common/package.json ./packages/common
COPY packages/${BUILD_CONTEXT}/package.json ./packages/${BUILD_CONTEXT}
COPY package.json .
COPY yarn.lock .
RUN yarn install --production
CMD yarn run start:${ENV_BUILD_CONTEXT}

如果想为App1构建图像,只需运行以下操作:docker build -t image -name -build -arg build_context = app1 -f dockerfile。

这应该有效。

nb。 为此,您希望您的脚本名称与软件包的名称匹配。

希望这会有所帮助。

Ran into a similar issue using yarn workspaces. I ended up just building the package I want to use in my app and reproducing the directory setup to support symlinks used by yarn workspaces.

This was my setup:

PROJECT ROOT

- package.json
- packages/
  - common/ (directory I want to install in either app1/app2)
  - app1/
  - app2/
- Dockerfile

package.json

{
   "private": true,
  "workspaces": [
    "packages/*"
  ],
  "scripts": {
    "build:common": "yarn workspace @monorepo/common build",
    "build:app1": "yarn workspace @monorepo/app1 build",
    "start:app1": "yarn workspace @monorepo/app start",
    "build:app2": "yarn workspace @monorepo/app2 build",
    "start:app2": "yarn workspace @monorepo/app2 start",
  },
  "name": "monorepo",
}

Dockerfile

ARG BUILD_CONTEXT

FROM node:18.12.0-alpine AS builder
ARG BUILD_CONTEXT
ENV ENV_BUILD_CONTEXT=$BUILD_CONTEXT
WORKDIR /usr/app
COPY package.json ./
COPY yarn.lock ./
COPY packages/${BUILD_CONTEXT} /usr/app/packages/${BUILD_CONTEXT}
COPY packages/common /usr/app/packages/common
RUN yarn install
RUN yarn run build:common
RUN yarn run build:${BUILD_CONTEXT}


FROM node:18.12.0-alpine AS final
ARG BUILD_CONTEXT
ENV ENV_BUILD_CONTEXT=$BUILD_CONTEXT
WORKDIR /usr/app
COPY --from=builder ./usr/app/packages/${BUILD_CONTEXT}/build ./packages/${BUILD_CONTEXT}/build
COPY --from=builder ./usr/app/packages/common/build ./packages/common/build
COPY --from=builder ./usr/app/packages/common/package.json ./packages/common
COPY packages/${BUILD_CONTEXT}/package.json ./packages/${BUILD_CONTEXT}
COPY package.json .
COPY yarn.lock .
RUN yarn install --production
CMD yarn run start:${ENV_BUILD_CONTEXT}

If you want to build an image for app1, simply run this: docker build -t image-name --build-arg BUILD_CONTEXT=app1 -f Dockerfile .

This should work.

NB. For this to work, you want your script names to match up with the names of the packages.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文