alpine镜像常见坑

FROM alpine:latest
RUN apk add --no-cache libc6-compat #二进制文件无法通过./执行
ADD localtime /etc/localtime #修复时区问题
RUN apk add --no-cache ca-certificates #在go程序中无法访问https链接
RUN echo "hosts: files dns" > /etc/nsswitch.conf #go程序在alpine下不解析hosts文件

golang在alpine请求一个hosts定义的域名时不生效

解决方案
echo "hosts: files dns" > /etc/nsswitch.conf

golang在alpine镜像出现可执行文件找不到情况

解决方案:

RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

时区设置:

RUN echo "http://mirrors.ustc.edu.cn/alpine/v3.3/main/" > /etc/apk/repositories
# 时区设置
RUN apk add --no-cache tzdata
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" >> /etc/timezone

解决gomod问题:

RUN export GO111MODULE=on \
&& go mod vendor
FROM golang:1.12 as builder

ENV BUILDPATH=/git.name.com/name/Bigproject/src/project

ENV GO111MODULE=auto

WORKDIR /go/src/${BUILDPATH}

COPY ./ /go/src/${BUILDPATH}

RUN go env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
&& go build -tags netgo -ldflags "-w -s"  -o admin-client main.go

FROM alpine:latest

ENV RUN_MODE=prod
ENV BUILDPATH=git.zituo.net/huyongqiang/ymzy-v3/src/Admin-Client
ENV TZ Asia/Shanghai

RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

RUN apk --no-cache add ca-certificates tzdata\
&& sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.ustc.edu.cn/!g' /etc/apk/repositories \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata \
&& mkdir -p /root/ymzy/Admin-Client/logs



WORKDIR /root/ymzy/Admin-Client
COPY --from=builder go/src/git.zituo.net/huyongqiang/ymzy-v3/src/Admin-Client/admin-client .
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /usr/local/go/lib/time/zoneinfo.zip
COPY --from=builder go/src/${BUILDPATH}/conf/ ./conf/
RUN chmod +x /root/ymzy/Admin-Client/admin-client

#日志
VOLUME /root/ymzy/Admin-Client/logs
LABEL maintainer = "jia.yunji@qq.com"
LABEL api = "ymzy-admin-client"

EXPOSE 8090

CMD ["./admin-client"]