如何在Docker中构建Go模块?

时间:2019-08-27 14:37:51

标签: docker go go-modules

我正在尝试在docker容器中构建go项目。

这是dockerfile:

FROM golang:1.12.9 as builder

ENV GO111MODULE=on
WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o encashment


FROM scratch
COPY --from=builder /app/encashment /encashment/
EXPOSE 8080
ENTRYPOINT ["/app/encashment"]

go.mod中只有一个依赖项:

require github.com/gorilla/mux v1.7.3

如果我在本地删除gorilla/mux并致电go mod download,一切正常。但是当我打电话给docker build .时,它会返回

go: finding github.com/gorilla/mux v1.7.3
go: github.com/gorilla/mux@v1.7.3: unknown revision v1.7.3
go: error loading module requirements

如何进行这项工作?
UPD:这是tcpdump输出的一部分:

19:00:00.220102 IP 172.17.0.2.43627 > dns.google.domain: 15472+ A? github.com. (28)
19:00:00.220115 IP 172.17.0.2.43627 > dns.google.domain: 64629+ AAAA? github.com. (28)
19:00:02.969391 IP 172.17.0.1.38261 > 239.255.255.250.1900: UDP, length 172
19:00:03.971322 IP 172.17.0.1.38261 > 239.255.255.250.1900: UDP, length 172
19:00:04.971794 IP 172.17.0.1.38261 > 239.255.255.250.1900: UDP, length 172
19:00:05.221952 IP 172.17.0.2.40395 > dns.google.domain: 15472+ A? github.com. (28)
19:00:05.221992 IP 172.17.0.2.40395 > dns.google.domain: 64629+ AAAA? github.com. (28)
19:00:05.970002 IP 172.17.0.1.mdns > 224.0.0.251.mdns: 0 PTR (QM)? _googlecast._tcp.local. (40)
19:00:05.970263 IP 172.17.0.1.mdns > 224.0.0.251.mdns: 0 PTR (QM)? _googlecast._tcp.local. (40)
19:00:05.972573 IP 172.17.0.1.38261 > 239.255.255.250.1900: UDP, length 172
19:00:06.971288 IP 172.17.0.1.mdns > 224.0.0.251.mdns: 0 PTR (QM)? _googlecast._tcp.local. (40)
19:00:06.971446 IP 172.17.0.1.mdns > 224.0.0.251.mdns: 0 PTR (QM)? _googlecast._tcp.local. (40)
19:00:08.972581 IP 172.17.0.1.mdns > 224.0.0.251.mdns: 0 PTR (QM)? _googlecast._tcp.local. (40)
19:00:08.972736 IP 172.17.0.1.mdns > 224.0.0.251.mdns: 0 PTR (QM)? _googlecast._tcp.local. (40)
19:00:10.224125 IP 172.17.0.2.43627 > dns.google.domain: 15472+ A? github.com. (28)
19:00:10.224171 IP 172.17.0.2.43627 > dns.google.domain: 64629+ AAAA? github.com. (28)

1 个答案:

答案 0 :(得分:3)

好吧,感谢@prometherion这个问题是因为docker容器内部没有Internet访问。 This SO answer为我工作。猜猜我应该在golang存储库中创建一个问题,并要求重做此错误消息。