dph19153 2019-04-21 19:45
浏览 133
已采纳

使用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.

  • 写回答

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分