dongwei5740 2016-07-27 16:22
浏览 109

加速Docker构建以进行Golang供应

I have a Dockerfile which is FROM golang:1.6-alpine. I’d like to use the docker images cache as much as possible while performing docker build to speed things up.

For this, I thought I’d add ADD vendor /go/src/…/mypackage/vendor as one of the first steps, and eventually run go install -v …/mypackage. However, this causes the vendored packages (which are the bulk of the build time) to only be built then.

Is there anyway to explicitly build all vendored packages (i.e. sources inside a …/vendor dir), but only them?

I'd like my Dockerfile to look something like this:

FROM golang:1.6-alpine

ADD vendor /go/src/github.com/myuser/package/vendor
# missing: magic command to build only the vendored packages above

ADD *.go /go/src/github.com/myuser/package
RUN go install -v "github.com/myuser/package
  • 写回答

1条回答 默认 最新

  • douzhui8531 2016-07-28 09:42
    关注
    • In the host (outside the docker container), the packages are still stored in the /vendor folder. This allows for convenient management of these dependencies. Personally I use godep to manage this.
    • Inside the container, instead of adding the /vendor folder to be a subdir of my package, I add all the packages inside the /vendor folder under the source directory /go/src directly.
    • Since go's vendoring is all about changing import paths, and build artifacts are stored in the /pkg directory regardless of whether they were 'vendored' or not, this solution is stable.
    • In order to enjoy the speed-up, and not having to rebuild all vendored packages unless they were changed, the Dockerfile runs a go install ./... on the src dir after adding the vendor folder, then proceeds with the rest of the build.

    Dockerfile:

    FROM golang:1.6-alpine
    
    # Add and install all vendored packages.
    ADD vendor /go/src/
    RUN cd /go/src && go install -v ./...
    
    # ...
    
    # Add and install our package's source files, without the vendor folder
    ADD *.go "/go/src/github.com/myuser/mypackage"
    RUN go install -v "/go/src/github.com/myuser/mypackage"
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)