使用 Alpine 作为基础镜像时可能会遇到的常见问题的解决方法
1、使用Alpine基础镜像下载软件包时报错
Dockerfile
FROM alpine:3.11 RUN apk add --no-cache ca-certificates # To speed up building process, we copy binary directly from make # result instead of building it again, so make sure you run the # following command first before building docker image # make cb-apiserver # COPY cb-apiserver /usr/local/bin/ EXPOSE 9090 CMD ["sh"]
原因:alpine 镜像默认的软件仓库在国外,有时 docker build 的时候安装软件包会很慢。
解决方法:使用国内的软件包镜像,比如 https://mirrors.ustc.edu.cn 的镜像:
Dockerfile加上下面这行:
RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.ustc.edu.cn/!g' /etc/apk/repositories
参考:https://mozillazg.com/2020/03/use-alpine-image-common-issues.rst.html
2、go get程序包超时
Dockerfile加上下面这行:
ENV GOPROXY=https://goproxy.cn,direct