如何在不发布的情况下在lerna monorepo中构建docker映像 [英] How to build docker images in a lerna monorepo without publishing

查看:86
本文介绍了如何在不发布的情况下在lerna monorepo中构建docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此用例是在Lerna monorepos中进行分支构建和部署.

The use case for this is branch building and deployments in Lerna monorepos.

问题是Lerna monorepos要么提升NPM中的依赖关系,要么使用纱线工作区来达到相同的效果,以收集工作区/monorepo的node_modules文件夹中的所有依赖关系.这意味着由于Docker构建上下文的工作原理,在子文件夹中构建Dockerfile时将无法访问它们.

The problem is that Lerna monorepos either hoist dependencies in NPM or use yarn workspaces to the same effect to collect all dependencies in the node_modules folder of the workspace/monorepo. Which means that they will not be accessible when building Dockerfiles in subfolders due to how docker build contexts work.

我想象这里需要的是一种降低"(而不是提升)功能,用于在运行docker build之前将包依赖项拉入Docker/package.json项目的node_modules.

I imagine what is needed here is a kind of "lower" (as opposed to hoist) function to pull package dependencies into the node_modules of the Docker/package.json project before running docker build.

问题是,是否有人有更好的主意,或知道已有的方法可以做到这一点?

The question is, does anyone have a better idea, or know of an already existing method to do this?

推荐答案

对于我自己的项目,解决方案是使用

For my own project, the solution is to use docker BuildKit to first build all the workspace and then build a docker image for the project workspace reusing the previous built files.

详细信息中,您已在docker文件中复制了带有纱线锁的顶级package.json,然后对所需工作区的package.json进行了挑选.然后运行纱线安装和纱线构建,以使一切正常.

In details you have copy in the docker file the top package.json with yarn lock and then cherrypicking the package.json of the needed workspace. Then running a yarn install and a yarn build to get everything working.

这是我的项目:

# base image
FROM @myscope/base:latest as base

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY ["package.json","yarn.lock", "./"]
COPY ./packages/server/package.json ./packages/server/
COPY ./packages/shared/package.json ./packages/shared/
COPY ./packages/app/package.json ./packages/app/

RUN yarn install --frozen-lockfile --non-interactive --production=false --ignore-scripts
COPY . /app
RUN yarn build



FROM nodejs:14.15 as serverapp

WORKDIR /app

COPY ["package.json","yarn.lock", "./"]
COPY ./packages/server/package.json ./packages/server/
COPY ./packages/shared/package.json ./packages/shared/

RUN yarn install --frozen-lockfile --non-interactive --production=true --ignore-scripts

# copy artifact build from the 'build environment'
COPY --from=base /app/packages/shared/dist /app/packages/shared/dist

COPY ["./packages/server/", "./packages/server/"]

WORKDIR /app/packages/server
VOLUME ["/app/packages/server/logs", "/app/packages/server/uploads"]
EXPOSE $PORT
CMD ["yarn", "start"]

共享是一个私有工作区,是 server 工作区的依赖项.

shared is a private workspace that is a dependency of the server workspace.

这篇关于如何在不发布的情况下在lerna monorepo中构建docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆