Docker构建COPY失败

问题描述 投票:1回答:2

我正在使用来自golang的构建码头图像:alpine。

我的目的只是将执行的二进制文件复制到新的临时图像。

在我的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
docker dockerfile
2个回答
2
投票

我认为问题是环境变量$GOPATH仅存在于golang图像中而不存在于临时图像中。所以尝试将COPY-line更改为:

COPY --from=builder /go/bin/piggybank2go /go/bin/piggybank2go

0
投票

第一阶段的环境变量在第二阶段不可用。因此,在第二阶段无法正确解析“$ GOPATH”,因此出现错误。

在第二阶段,您应该确切地知道您要复制的内容和位置:

COPY --from=builder /go/bin/piggybank2go /go/bin/piggybank2go
© www.soinside.com 2019 - 2024. All rights reserved.