FROM golang:1.16.5
WORKDIR /alert # Golang官方镜像默认工作目录为/src,强烈建议先自定义工作目录
ENV GOPROXY="https://goproxy.cn,direct"
COPY go.mod go.sum main.go ./
COPY config config/
RUN cd config/ && go mod tidy
COPY message message/
RUN cd /alert
RUN go build
COPY ./config/config.yaml .
EXPOSE 13344
CMD ["./alert","./config.yaml"]
]# docker build . -t alert:from-golang --no-cache
]# docker image ls alert:from-golang
REPOSITORY TAG IMAGE ID CREATED SIZE
alert from-golang 599e2c1cd10b 10 seconds ago 872MB
FROM golang:latest
WORKDIR /alert
ENV GOPROXY="https://goproxy.cn,direct"
COPY go.mod go.sum main.go ./
COPY config config/
RUN cd config/ && go mod tidy
COPY message message/
RUN cd /alert
RUN go build
FROM ubuntu
COPY --from=0 /alert/alert .
COPY ./config/config.yaml .
EXPOSE 13344
CMD ["./alert","./config.yaml"]
]# docker build . -t alert:from-ubuntu --no-cache
]# docker image ls alert:from-ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
alert from-ubuntu a912cc9bcd24 16 seconds ago 80.5MB
原文:https://www.cnblogs.com/liy36/p/15019657.html