Docker构建失败的原因是:“在可用模块之外进入目录”

时间:2019-08-09 08:52:32

标签: docker go

我在构建go服务的Docker映像时遇到问题(请参阅最后的错误消息)。我的服务代码的结构如下:

cmd
  - duc-adobe
pkg
  - adobe
  - common
.gitignore
Dockerfile
go.mod
go.sum

这是我的Dockerfile:

# build image
FROM golang:1.12-alpine as builder
RUN apk update && apk add --no-cache git ca-certificates && update-ca-certificates

WORKDIR /app

# first download dependencies so that we can utilize the docker cache
COPY go.mod .
COPY go.sum .
RUN go mod download

RUN CGO_ENABLED=0 go build ./cmd/duc-adobe -o /go/bin/app

# executable image
FROM alpine:3.9
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /go/bin/app /go/bin/app

ENTRYPOINT ["/go/bin/app"]

问题

构建失败,我不知道如何解决此问题。这是输出:

Step 1/11 : FROM golang:1.12-alpine as builder
 ---> 6b21b4c6e7a3
Step 2/11 : RUN apk update && apk add --no-cache git ca-certificates && update-ca-certificates
 ---> Using cache
 ---> 14b9eb869a9f
Step 3/11 : WORKDIR /app
 ---> Using cache
 ---> c2d0df63dc21
Step 4/11 : COPY go.mod .
 ---> Using cache
 ---> abd95c3f18eb
Step 5/11 : COPY go.sum .
 ---> Using cache
 ---> 3d49861b4f74
Step 6/11 : RUN go mod download
 ---> Using cache
 ---> 3cc3c7752c04
Step 7/11 : RUN CGO_ENABLED=0 go build ./cmd/duc-adobe -o /go/bin/app
 ---> Running in b1034f9c05bb
go: directory /go/bin/app outside available modules
The command '/bin/sh -c CGO_ENABLED=0 go build ./cmd/duc-adobe -o /go/bin/app' returned a non-zero code: 1

我不确定在什么意义上需要使go模块可访问或如何解决此问题?

1 个答案:

答案 0 :(得分:0)

请参阅构建版本用法:

shubuntu1@shubuntu1:~$ go help build
usage: go build [-o output] [-i] [build flags] [packages]

因此,您应该将-o放在命令中间,例如:

go build -o /go/bin/app ./cmd/duc-adobe