我正在使用来自golang:alpine的构建docker映像。
我的目的仅仅是将执行的二进制文件复制到新的暂存映像中。
在我的Dockerfile下面:
############################
# STEP 1 build executable binary
############################
FROM golang@sha256:d481168873b7516b9f34d322615d589fafb166ff5fd57d93e96f64787a58887c AS builder
RUN apk update && apk add --no-cache git tzdata ca-certificates && update-ca-certificates
ADD . $GOPATH/src/piggybank2go
WORKDIR $GOPATH/src/piggybank2go
COPY . .
# Fetch dependencies.
RUN go get -u github.com/golang/dep/cmd/dep
RUN dep ensure -v
# Build executeable binary
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o $GOPATH/bin/piggybank2go
# RUN go build -o /go/bin/piggybank2go
############################
# STEP 2 build a small image
############################
FROM scratch
# Copy our static executable
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder $GOPATH/bin/piggybank2go $GOPATH/bin/piggybank2go
# Port on which the service will be exposed.
EXPOSE 8081
ENTRYPOINT ["$GOPATH/bin/piggybank2go"]
但是我得到了这个错误:
Step 12/14 : COPY --from=builder $GOPATH/bin/piggybank2go $GOPATH/bin/piggybank2go
COPY failed: stat /var/lib/docker/overlay2/b37bbe725b51ba50e3082d162e75d4cdee368499e26887c6921486415c089920/merged/bin/piggybank2go: no such file or directory
答案 0 :(得分:2)
我认为问题在于环境变量$GOPATH
仅存在于golang图像中,而不存在于临时图像中。因此,请尝试将COPY
行更改为:
COPY --from=builder /go/bin/piggybank2go /go/bin/piggybank2go
答案 1 :(得分:0)
第一阶段的环境变量在第二阶段不可用。因此,在第二阶段无法正确解析“ $ GOPATH”,从而导致错误。
在第二阶段,您应该确切地知道要复制的内容和内容:
COPY --from=builder /go/bin/piggybank2go /go/bin/piggybank2go