我遇到了一个仅在 Mac 环境中出现的特殊问题。当我运行 docker build 命令来构建 Node 映像时,它无法获取包,提示存在连接问题。这只发生在 Mac 环境中。我这里有一台服务器,构建正常完成,但这最终阻止我在我的机器上进行测试。
谁能告诉我这可能是什么以及如何解决它?
Dockerfile 在这里:
FROM node:16.14-alpine3.15 as builder
ENV NODE_ENV=development
WORKDIR /home/node/app
COPY package*.json .
COPY yarn.lock .
COPY tsconfig.json .
RUN yarn install
COPY . .
RUN yarn build
FROM node:16.14-alpine3.15 as production
ENV NODE_ENV=production
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN mkdir logs
COPY package*.json .
COPY yarn.lock .
RUN yarn install
COPY --from=builder /home/node/app/dist /usr/src/app/dist
EXPOSE 3333
CMD ["yarn", "start"]
我已经尝试在纱线安装命令中添加以下标志:
RUN yarn install --frozen-lockfile --no-cache --production --network-timeout 100000
无论是一起还是单独,但都无济于事。我也删除了代理,也没有成功。
RUN yarn config delete https-proxy RUN yarn config delete proxy.
但是,我注意到这个错误只发生在 Yarn v1.22 上。当使用 berry 版本时,不会发生同样的情况。
我在这里放弃了这个答案,因为代理对我的情况没有任何帮助。
TL;博士 版本检查 URL 重定向,导致纱线混乱
因此,我继续研究典型的过时工具,例如冗长检查、curl 和 dig。
所以,首先,我尝试检查详细输出:
宾果#1。
verbose 0.181073051 Performing "GET" request to "https://yarnpkg.com/latest-version".
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.12s.
info There appears to be trouble with your network connection. Retrying...
verbose 33.254841183 Performing "GET" request to "https://yarnpkg.com/latest-version".
info There appears to be trouble with your network connection. Retrying...
verbose 66.292530305 Performing "GET" request to "https://yarnpkg.com/latest-version".
info There appears to be trouble with your network connection. Retrying...
verbose 99.329186881 Performing "GET" request to "https://yarnpkg.com/latest-version".
info There appears to be trouble with your network connection. Retrying...
verbose 132.366749502 Performing "GET" request to "https://yarnpkg.com/latest-version".
为什么没有从yarnpkg.com 得到答案?这太疯狂了...所以,让我们看看这是否能解决:
$ dig yarnpkg.com
; <<>> DiG 9.18.7-1+b1-Debian <<>> yarnpkg.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55602
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 9cf3b6f8b290069dbbe8ca8763501b11ea79617323c673e6 (good)
;; QUESTION SECTION:
;yarnpkg.com. IN A
;; ANSWER SECTION:
yarnpkg.com. 300 IN A 104.18.126.100
yarnpkg.com. 300 IN A 104.16.171.99
;; Query time: 11 msec
;; SERVER: 192.168.1.1#53(192.168.1.1) (UDP)
;; WHEN: Wed Oct 19 18:43:13 EEST 2022
;; MSG SIZE rcvd: 100
确实如此!!! (?)
那么,让我们看看通过使用curl我能得到什么
$ curl https://yarnpkg.com/latest-version
Redirecting to https://classic.yarnpkg.com/latest-version
什么?重定向?好吧,这是新的..
我尝试将检查版本的url设置为classic.yarnpkg.com,而不是yarnpkg.com,但我找不到要使用的yarn配置变量。
所以,我用了
/etc/hosts
。
; <<>> DiG 9.18.7-1+b1-Debian <<>> classic.yarnpkg.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35092
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 72636a6924283d51c4127a8463501bbb42e53d34835e8402 (good)
;; QUESTION SECTION:
;classic.yarnpkg.com. IN A
;; ANSWER SECTION:
classic.yarnpkg.com. 300 IN CNAME yarnpkg.netlify.com.
yarnpkg.netlify.com. 20 IN A 3.64.200.242
yarnpkg.netlify.com. 20 IN A 34.141.11.154
;; Query time: 59 msec
;; SERVER: 192.168.1.1#53(192.168.1.1) (UDP)
;; WHEN: Wed Oct 19 18:46:03 EEST 2022
;; MSG SIZE rcvd: 138
将第一个IP设置为yarnpkg.com
# /etc/hosts
34.141.48.9 yarnpkg.com
宾果!!!纱线命令立即完成。
$ yarn
yarn install v1.22.19
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.12s.
$
可能是因为 docker 网络阻止了您的访问
尝试
docker network ls
列出所有网络并 docker network prune
删除未使用的
然后再次尝试安装
对我来说,用这个解决了:
npm config set registry "http://registry.npmjs.org"
然后:
yarn config set registry "http://registry.npmjs.org"