1

我正在将 VueJS 应用程序从“经典”Yarn 1.x 迁移到 Yarn 2。按照安装文档进行操作很简单,并且可以正常工作。

将应用程序打包到 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 零安装设置对于 Docker 映像的样子的信息。

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

4

2 回答 2

1

@Ethan 的回答是有道理的,它应该可以工作。但对我来说,我在构建过程中遇到了这个奇怪的错误:

 > [ 7/10] RUN yarn --version:                                                                                                               
#11 1.430 internal/modules/cjs/loader.js:905                                                                                                 
#11 1.430   throw err;                                                                                                                       
#11 1.430   ^                                                                                                                                
#11 1.430                                                                                                                                    
#11 1.430 Error: Cannot find module '/base/.yarn/releases/yarn-3.1.1.cjs'
#11 1.430     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
#11 1.430     at Function.Module._load (internal/modules/cjs/loader.js:746:27)
#11 1.430     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
#11 1.430     at internal/main/run_main_module.js:17:47 {
#11 1.430   code: 'MODULE_NOT_FOUND',
#11 1.430   requireStack: []
#11 1.430 }

即使我肯定复制.yarn到图像,多么好。

我必须在构建中实际安装 yarn v2:

FROM node:14.17.1 as build

WORKDIR /base
COPY package.json .
RUN yarn set version berry

RUN yarn install --frozen-lockfile

更新

事实证明 Docker 并没有像我想象的那样复制整个目录。

我不得不COPY.yarn

COPY .yarn ./.yarn

为我解决了。

于 2022-02-05T18:11:57.377 回答
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 并按照 yarn 的建议将其添加到 repo 中。然后作为拉入文件的初步步骤,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
于 2021-09-02T13:08:59.203 回答