1

我正在尝试通过已将 yarn.lock 中的所有当前软件包安装在映像上来加速启动 docker。我认为我的纱线安装不正确,它在其他地方工作吗?

dockerfile 的相关部分:

# Create a dir
WORKDIR /(WORKDIR)
# Time to install all our dependencies
COPY package.json /(WORKDIR)/package.json
COPY yarn.lock /(WORKDIR)/yarn.lock

# Need the executables to be in the path
ENV PATH /(WORKDIR)/node_modules/.bin:$PATH
RUN yarn check --verify-tree || yarn install --frozen-lockfile

我认为我的最后一行是不正确的。它安装在某个地方,但不是安装在包本身上?无论是那个还是缓存都可能是一个问题。如果我启动图像,我发现输出yarn check --verify-tree仍然是图像的当前状态。

4

2 回答 2

1

yarn install在我的 Docker 构建期间我无法做到。我收到网络错误(getaddrinfo EAI_AGAIN):

Step 6/8 : COPY yarn.lock /app/yarn.lock
 ---> Using cache
 ---> e55488a3d051
Step 7/8 : RUN yarn
 ---> Running in 5bb8d663d00b
yarn install v1.22.15
[1/4] Resolving packages...
[2/4] Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/app-root-path/-/app-root-path-3.0.0.tgz: getaddrinfo EAI_AGAIN registry.yarnpkg.com".
info If you think this is a bug, please open a bug report with the information provided in "/app/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
ERROR: Service 'web-svc' failed to build: The command '/bin/sh -c yarn' returned a non-zero code: 1

我最近更新了我的 Ubuntu Linux 系统,并认为这可能是原因。所以,我重新启动docker.service并解决了这个问题。

sudo systemctl restart docker.service
于 2021-12-05T18:34:51.760 回答
0

只需RUN yarn确保. COPY_yarn

FROM        node:12.14.0-alpine3.11

ENV         NODE_ENV=production
WORKDIR     /app

COPY        package.json ./
COPY        yarn.lock ./
RUN         yarn

COPY        src ./

我在我的机器上测试它,你可以看看我是否改变了yarn.lock. 如果我不改变我的yarn.lock

$ docker build -t demo .
Step 1/6 : FROM        node:12.14.0-alpine3.11
 ---> 1cbcaddb8074
Step 2/6 : ENV         NODE_ENV=production
 ---> Using cache
 ---> dc7f1a2f7d90
Step 3/6 : WORKDIR     /app
 ---> Using cache
 ---> eec9363713a5
Step 4/6 : COPY        package.json ./
 ---> Using cache
 ---> fde6cf7bb577
Step 5/6 : COPY        yarn.lock ./
 ---> 6a1369622d79
Step 6/6 : RUN         yarn
 ---> Running in ff6433969bea
yarn install v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
warning sha.js@2.4.11: Invalid bin entry for "sha.js" (in "sha.js").
warning url-loader@1.1.2: Invalid bin field for "url-loader".
info fsevents@1.2.9: The platform "linux" is incompatible with this module.
info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > styled-components@5.0.1" has unmet peer dependency "react-is@>= 16.8.0".
[4/4] Building fresh packages...
Done in 35.97s.
Removing intermediate container ff6433969bea
 ---> 8dcd2124289d
Successfully built 8dcd2124289d

$docker build -t demo .
Step 1/6 : FROM        node:12.14.0-alpine3.11
 ---> 1cbcaddb8074
Step 2/6 : ENV         NODE_ENV=production
 ---> Using cache
 ---> dc7f1a2f7d90
Step 3/6 : WORKDIR     /app
 ---> Using cache
 ---> eec9363713a5
Step 4/6 : COPY        package.json ./
 ---> Using cache
 ---> fde6cf7bb577
Step 5/6 : COPY        yarn.lock ./
 ---> Using cache
 ---> 6a1369622d79
Step 6/6 : RUN         yarn
 ---> Using cache
 ---> 8dcd2124289d
Step 7/7 : COPY        src ./
 ---> 13474b882e11
于 2020-05-29T03:39:11.077 回答