在使用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.