Docker化面向软件包的设计模块应用程序

时间:2019-04-15 07:28:12

标签: docker go module package

我正在按照here中所述的面向包设计的方式尝试使用模块,以便可以对多个可执行服务进行“码头化”,但我正努力在单个存储库中正确整理内容。我无法使用此方法构建成功的docker映像。在线上的大多数示例都针对单模块方法,其中main.go和dockerfile位于根文件夹中。

我的目录结构如下所示 专案1  -API  -建立    -服务1      -docker文件    -服务2      -docker文件  -cmd    -服务1      -main.go    -服务2      -main.go  -部署  -文档  -内部  - 第三方  -go.mod  -go.sum  -Makefile  -供应商

# Accept the Go version for the image to be set as a build argument.
# Default to Go 1.11
ARG GO_VERSION=1.11

# First stage: build the executable.
FROM golang:${GO_VERSION}-alpine AS builder

# Create the user and group files that will be used in the running container to
# run the process as an unprivileged user.
RUN mkdir /user && \
    echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
    echo 'nobody:x:65534:' > /user/group

# Install the Certificate-Authority certificates for the service1 to be able to make
# calls to HTTPS endpoints.
RUN apk add --no-cache ca-certificates

# Set the environment variables for the go command:
# * CGO_ENABLED=0 to build a statically-linked executable
# * GOFLAGS=-mod=vendor to force `go build` to look into the `/vendor` folder.
ENV CGO_ENABLED=0 GOFLAGS=-mod=vendor

# Set the working directory outside $GOPATH to enable the support for modules.
WORKDIR /src

# Import the code from the context.
COPY ./ ./

# Build the executable to `/service1`. Mark the build as statically linked.
RUN go build \
    -installsuffix 'static' \
    -o /service1 .

# Final stage: the running container.
FROM scratch AS final

# Import the user and group files from the first stage.
COPY --from=builder /user/group /user/passwd /etc/

# Import the Certificate-Authority certificates for enabling HTTPS.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

# Import the compiled executable from the second stage.
COPY --from=builder /service1 /service1

EXPOSE 8080

# # Run the compiled binary.
ENTRYPOINT ["/service1"]

当我运行docker build -t service1:v0.0.1 .时,由于所有依赖项都会出现此错误

 /go/src/github.com/.../project1/internal/pkg/common (from $GOPATH)
main.go:15:2: cannot find package "github.com/.../project1/internal/pkg/delegates" in any of:
        /usr/local/go/src/github.com/.../project1/internal/pkg/delegates (from $GOROOT)
        /go/src/github.com/.../project1/internal/pkg/delegates (from $GOPATH)
main.go:16:2: cannot find package "github.com/.../project1/internal/pkg/secure" in any of:
        /usr/local/go/src/github.com/.../project1/internal/pkg/secure (from $GOROOT)
        /go/src/github.com/.../project1/internal/pkg/secure (from $GOPATH)
main.go:17:2: cannot find package "github.com/.../project1/statik" in any of:
        /usr/local/go/src/github.com/.../project1/statik (from $GOROOT)
        /go/src/github.com/.../project1/statik (from $GOPATH)
main.go:18:2: cannot find package "github.com/grpc-ecosystem/grpc-gateway/runtime" in any of:
        /usr/local/go/src/github.com/grpc-ecosystem/grpc-gateway/runtime (from $GOROOT)
        /go/src/github.com/grpc-ecosystem/grpc-gateway/runtime (from $GOPATH)
main.go:19:2: cannot find package "github.com/heptiolabs/healthcheck" in any of:
        /usr/local/go/src/github.com/heptiolabs/healthcheck (from $GOROOT)
        /go/src/github.com/heptiolabs/healthcheck (from $GOPATH)
main.go:21:2: cannot find package "github.com/rakyll/statik/fs" in any of:
        /usr/local/go/src/github.com/rakyll/statik/fs (from $GOROOT)
        /go/src/github.com/rakyll/statik/fs (from $GOPATH)
main.go:22:2: cannot find package "github.com/sirupsen/logrus" in any of:
        /usr/local/go/src/github.com/sirupsen/logrus (from $GOROOT)
        /go/src/github.com/sirupsen/logrus (from $GOPATH)
main.go:23:2: cannot find package "google.golang.org/grpc" in any of:
        /usr/local/go/src/google.golang.org/grpc (from $GOROOT)
        /go/src/google.golang.org/grpc (from $GOPATH)

基本上找不到所有依赖项...

2 个答案:

答案 0 :(得分:0)

看起来您正在构建项目,但在GOPATH中却未启用实验模块。

尝试将您的源代码复制到/go/src/github.com/your-name/project1并在那里构建项目。

答案 1 :(得分:0)

错误消息指向GOPATH的事实表明go命令未在模块模式下运行。

最可能的原因是缺少go.mod文件或$GOPATH/src中的工作目录。 (我注意到您正在将工作目录显式更改为/src,但是如果$GOPATH等于/,则仍然无法激活模块模式。)

尝试将GO111MODULE=on添加到环境中。 (这至少应该解决当前的问题,但可能会发现一些新问题。)