FROM golang:1.16 AS builder WORKDIR /go/src/github.com/alexellis/href-counter/ RUN go get -d -v golang.org/x/net/html COPY app.go ./ RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o app .
FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./ # 意味COPY来自builder阶段的/go/src/github.com/alexellis/href-counter/app文件到当前目录 CMD ["./app"]
FROM golang:1.20-alpine AS base WORKDIR /src COPY go.mod go.sum . RUN --mount=type=cache,target=/go/pkg/mod/ \ go mod download -x COPY . .
FROM base AS build-client RUN --mount=type=cache,target=/go/pkg/mod/ \ go build -o /bin/client ./cmd/client FROM scratch AS server COPY --from=build-server /bin/server /bin/ ENTRYPOINT [ "/bin/server" ]
ARG GO_VERSION=1.20 FROM golang:${GO_VERSION}-alpine AS base WORKDIR /src RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=bind,source=go.sum,target=go.sum \ --mount=type=bind,source=go.mod,target=go.mod \ go mod download -x FROM base AS build-client RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=bind,target=. \ go build -o /bin/client ./cmd/client
ARG GO_VERSION=1.20 FROM golang:${GO_VERSION}-alpine AS base WORKDIR /src RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=bind,source=go.sum,target=go.sum \ --mount=type=bind,source=go.mod,target=go.mod \ go mod download -x FROM base AS build-client RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=bind,target=. \ go build -o /bin/client ./cmd/client