dph19153
2019-04-21 19:45
浏览 108

使用mono repo在Docker容器中包含Go依赖项

I have a mono repo with the structure.

mono-repo
- serviceA
 - main.go
 - Dockerfile
-serviceB
 - main.go
 - Dockerfile
go.mod
go.sum

The Dockerfile in serviceA contains the following code.

FROM golang

ENV GO111MODULE=on

WORKDIR /app

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

ENTRYPOINT ["/app/serviceA"]

I want to build the Docker image and include the dependencies from the root of my mono-repo inside the container, I am currently receiving an error saying it can't find any of the dependency packages when I run

docker build -t serviceA .

Unless I place a go.mod inside serviceA I can't see a nice way of achieving what I want. By placing a go.mod inside the service it feels like I'm losing the advantage of services sharing dependencies within the repo.

图片转代码服务由CSDN问答提供 功能建议

我有一个具有结构的Mono仓库。

  mono  -repo 
- serviceA 
-main.go 
-Dockerfile 
-serviceB 
-main.go 
-Dockerfile 
go.mod 
go.sum 
   
  
 

serviceA中的Dockerfile包含以下代码。

  FROM golang 
 
ENV GO111MODULE = on 
 
WORKDIR / app 
 
COPY。  。
 
RUN CGO_ENABLED = 0 GOOS = linux GOARCH = amd64 go build 
 
ENTRYPOINT [“ / app / serviceA”] 
   
 
 

我要构建 Docker映像并在容器内包含来自我的单仓库的根的依赖关系,我目前收到一条错误消息,说我运行

\时找不到任何依赖关系包 n

docker build -t serviceA。

除非我将go.mod放在serviceA内,否则找不到实现我想要的东西的好方法。 通过将go.mod放入服务中,感觉就像我失去了在回购中共享服务依赖项的优势一样。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqixian8474 2019-04-21 20:22
    已采纳

    By placing a go.mod inside the service it feels like I'm losing the advantage of services sharing dependencies within the repo.

    Yet, this is an approach seen here or there, where COPY go.mod . (and COPY go.sum .) is followed by RUN go mod download.

    #This is the ‘magic’ step that will download all the dependencies that are specified in 
    # the go.mod and go.sum file.
    # Because of how the layer caching system works in Docker, the  go mod download 
    # command will _ only_ be re-run when the go.mod or go.sum file change 
    # (or when we add another docker instruction this line)
    RUN go mod download
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题