错误 发生意外错误:"https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz: getaddrinfo EAI_AGAIN reg



我遵循Docker官方指南中的指南,指南->Get started->Part 2: Containerize an application.

  1. git clone https://github.com/docker/getting-started.git

  2. 然后我创建了一个Dockerfile,内容如下:

    # syntax=docker/dockerfile:1
    FROM node:18-alpine
    WORKDIR /app
    COPY . .
    RUN yarn install --production
    CMD ["node", "src/index.js"]
    EXPOSE 3000
    
  3. 我试图构建它,但发生错误

docker build -t getting-started .

错误信息如下:

[+] Building 101.8s (12/12) FINISHED
=> [internal] load build definition from Dockerfile                                                                                                                                    0.0s
=> => transferring dockerfile: 32B                                                                                                                                                     0.0s
=> [internal] load .dockerignore                                                                                                                                                       0.0s
=> => transferring context: 2B                                                                                                                                                         0.0s
=> resolve image config for docker.io/docker/dockerfile:1                                                                                                                             13.7s
=> CACHED docker-image://docker.io/docker/dockerfile:1@sha256:9ba7531bd80fb0a858632727cf7a112fbfd19b17e94c4e84ced81e24ef1a0dbc                                                         0.0s
=> [internal] load build definition from Dockerfile                                                                                                                                    0.0s
=> [internal] load .dockerignore                                                                                                                                                       0.0s
=> [internal] load metadata for docker.io/library/node:18-alpine                                                                                                                      11.9s
=> [1/4] FROM docker.io/library/node:18-alpine@sha256:b3f383c13d71066a4e2380d42f4563ac14ac76b3035a5bea4db84209e14665d5                                                                 0.0s
=> [internal] load build context                                                                                                                                                       0.0s
=> => transferring context: 2.49kB                                                                                                                                                     0.0s
=> CACHED [2/4] WORKDIR /app                                                                                                                                                           0.0s
=> CACHED [3/4] COPY . .                                                                                                                                                               0.0s
=> ERROR [4/4] RUN yarn install --production                                                                                                                                          20.8s
------
> [4/4] RUN yarn install --production:
#10 0.456 yarn install v1.22.19
#10 0.516 [1/4] Resolving packages...
#10 0.689 [2/4] Fetching packages...
#10 5.792 error An unexpected error occurred: "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz: getaddrinfo EAI_AGAIN registry.yarnpkg.com".
#10 5.792 info If you think this is a bug, please open a bug report with the information provided in "/app/yarn-error.log".
#10 5.792 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
------
executor failed running [/bin/sh -c yarn install --production]: exit code: 1

这是我的环境:

  • WIN10 Family Edition 22H2
  • Docker Desktop 4.15.0 (93002)
    我已经尝试添加命令
yarn config set registry http://registry.npm.taobao.org/

在Dockerfile中,但这仍然发生了。修改后的Dockerfile如下:

# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn config set registry http://registry.npm.taobao.org/ && yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000

仍然没有修复错误。我找不到日志文件/app/yarn-error.log

你可能想要阅读docker build giving error in "yarn install"它详细地讨论了这个问题。

总而言之,这是一个网络和安全问题,尽管似乎有办法通过改变Dockerfile的内容来解决这个问题。,最简单的方法就是直接连接到另一个网络。

我用两台连接到同一网络的Windows 10电脑(在我家里,而不是公司网络)测试了这一点,两台电脑在docker构建时都失败了。

然后我将两台计算机从该网络断开,并将它们连接到移动网络,并成功构建并运行getting started

请注意,我不需要更改docker入门教程中的任何内容。

最新更新