我正在从"classic"线1。x到纱线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映像。
关于如何在Dockerfile
中使用Yarn 2方法,您有什么建议吗?
@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并没有像我想象的那样复制整个目录。
我必须为.yarn
添加一个明确的COPY
:
COPY .yarn ./.yarn
帮我解决了
由于一个奇怪的catch-22与yarn 2的包安装过程,我发现这是安装yarn@berry与docker最有效的方法。可能有更好的方法,但我不知道。
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