解决alpine构建容器镜像缓慢的问题

在构建基于Alpine的Docker镜像时,由于默认仓库在国外,导致下载软件包速度慢。为解决此问题,可以将Alpine的软件仓库地址改为国内的镜像地址,如阿里云或清华的镜像源。示例Dockerfile中展示了如何通过sed命令替换仓库地址,并使用Golang进行多阶段构建,提高构建效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用alpine做基础镜像构建容器镜像时,默认使用的境外的地址,更新或添加软件包异常缓慢,可先修改alpine基础镜像的软件仓库地址为国内的镜像地址,比如阿里云或清华:

FROM golang:1.20.2-alpine3.17 AS multistage
RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache --update git
RUN go env -w GOPROXY="https://goproxy.cn,https://mirrors.aliyun.com/goproxy,direct"
WORKDIR /go/src/httpserver
COPY . .
RUN go get -d -v \
    && go install -v \
    && go build -o /go/bin/httpserver .

FROM alpine:3.17.2
LABEL multi.label1="cncamp" multi.label2="wowlili"
COPY --from=multistage  /go/bin/httpserver /go/bin/
EXPOSE 8888
CMD [ "/go/bin/httpserver" ]

本例中,第二行中的sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories将默认的仓库地址替换成了清华的,其他可选地址(部分):

  • 阿里: mirrors.aliyun.com
  • 中科大: mirrors.ustc.edu.cn
  • 北邮: mirrors.bfsu.edu.cn

Done.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wooowlili

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值