【问题标题】:Using Yarn 2 (Berry) for packaging application in a Docker image使用 Yarn 2 (Berry) 在 Docker 镜像中打包应用程序
【发布时间】:2021-10-27 07:17:27
【问题描述】:

我正在将 VueJS 应用程序从“经典”Yarn 1.x 迁移到 Yarn 2。遵循 install documentation 很简单,并且可以正常工作。

将应用程序打包到 Docker 映像中时会遇到棘手的问题。

当前 Dockerfile

FROM node:14-alpine AS build-stage

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install

COPY . ./

RUN yarn build --modern \
    && find dist -type f -exec gzip -k "{}" \;

FROM nginx:mainline-alpine as production-stage

RUN apk add --no-cache curl

HEALTHCHECK CMD curl -f http://localhost || exit 1

COPY docker/entrypoint.sh /
RUN chmod +x /entrypoint.sh

COPY docker/app.nginx /etc/nginx/conf.d/default.conf
COPY --from=build-stage /app/dist /usr/share/nginx/html

ENTRYPOINT [ "/entrypoint.sh" ]

也许我看错了地方,但我找不到任何信息,Yarn 2 Zero-Install setup 看起来像 Docker 映像。

您对如何在Dockerfile 中使用 Yarn 2 方法有什么建议吗?

【问题讨论】:

    标签: yarnpkg yarnpkg-v2


    【解决方案1】:

    由于 yarn 2 的包安装过程有一个奇怪的 catch-22,我发现这是使用 docker 安装 yarn@berry 的最有效方法。可能有更好的方法,但我不知道。

    FROM node:latest as build
    WORKDIR /app
    
    # copy only the package.json file so yarn set version can
    # correctly download its modules for berry without overwriting
    # the existing yarnrc and cache files. If the rc is added now,
    # yarn will attempt to use the berry module without it being
    # installed.
    COPY package.json .
    RUN yarn set version berry
    
    # and _now_ pull in the rest of the build files overriding
    # the rc generated by setting the yarn version
    COPY yarn.lock .yarn .yarnrc.yml ./
    RUN yarn install
    COPY . .
    
    # continue with your build process
    

    然而,我会注意到,yarn 旨在从本地 .yarn/releases 文件夹中运行,所以最好的方法可能只是在本地安装 yarn2 和 @ 987654321@。然后作为拉入package.json 文件的初步步骤,拉取必要的.yarn 文件,如上所示。这应该在大多数情况下都可以工作,但是有时它给我带来了困难,因此上面的例子。

    FROM node:latest as build
    WORKDIR /app
    
    # Copy in the package file as well as other yarn
    # dependencies in the local directory, assuming the
    # yarn berry release module is inside .yarn/releases
    # already
    COPY package.json yarn.lock .yarn .yarnrc.yml ./
    
    RUN yarn install
    COPY . .
    
    # continue with your build process
    

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 2021-06-05
      • 2020-11-09
      • 2021-11-03
      • 2016-06-25
      • 2021-09-05
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多