Rob*_*uch 7 yarnpkg yarnpkg-v2
我正在将 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" ]
Run Code Online (Sandbox Code Playgroud)
也许我找错了地方,但我找不到任何关于Docker 镜像的Yarn 2 零安装设置的信息。
您对如何在 中使用 Yarn 2 方法有什么建议吗Dockerfile?
Dyl*_*rce 12
@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 }
Run Code Online (Sandbox Code Playgroud)
尽管我确实复制.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
Run Code Online (Sandbox Code Playgroud)
更新
事实证明 Docker 并没有像我想象的那样复制整个目录。
我必须添加一个明确COPY的.yarn:
COPY .yarn ./.yarn
Run Code Online (Sandbox Code Playgroud)
为我解决了。
由于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
Run Code Online (Sandbox Code Playgroud)
但是,我会注意到,yarn 旨在从本地.yarn/releases文件夹运行,因此最好的方法可能只是在本地安装yarn2,然后按照yarn的建议将其添加到存储库中。然后,作为拉入文件的初步步骤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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10659 次 |
| 最近记录: |