dongping1012 2019-04-15 07:28
浏览 68

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

I am in the process of trying out go modules in a package oriented design fashion as described here so that multiple executable services can be 'dockerized' but I am struggling to get things wired up properly in a single repo. I am unable to get successful docker image built using this approach. Most of the examples online are geared towards single module approach where main.go is and dockerfile are in the root folder.

My directory structure looks like this project1 - api - build - service1 - dockerfile - service2 - dockerfile - cmd - service1 - main.go - service2 - main.go - deployments - docs - internal - third_party - go.mod - go.sum - Makefile - vendor

# 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"]

When I run a docker build -t service1:v0.0.1 . I get this error for all my dependencies

 /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)

Basically it cannot find all dependencies ...

  • 写回答

2条回答 默认 最新

  • dongxinm279890 2019-04-15 13:15
    关注

    This looks like you are building the project without experimental modules enabled while out of GOPATH.

    Try copying your sources into /go/src/github.com/your-name/project1 and build the project there.

    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播