LoginSignup
15
6

More than 5 years have passed since last update.

`dep ensure -vendor-only=true` の使い所

Posted at

TL;DR

docker buildの高速化。
Gopkg.tomlGopkg.lockに変更がない場合はキャッシュが利用されてビルドの時間を短縮できる。

FROM golang:1.10

WORKDIR /go/src/github.com/munisystem/test
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure -v -vendor-only=true

COPY . .
RUN dep ensure -v
RUN go build

解説

docker buildCOPY及びADDのレイヤでキャッシュが効くか否かは対象のfileのhash値によって決まる。

例えばRailsアプリケーションのDockerfileでは以下のような記述が多く見受けられる。

FROM ruby:2.5.0

COPY Gemfile Gemfile.lock ./
RUN bundle install

COPY . .

こういったDockerfileを書くことにより、コードベースに変更を加えてもGemfileGemfile.lockに変更がない限りbundle installをスキップすることが出来る。

ではGolangのパッケージ管理ツールであるdepではどうだろうか。
dep ensureGopkg.toml Gopkg.lockの他にGoのコードから依存を解決しようとするため、カレントに対象となるファイルがない場合は実行に失敗してしまう。

-vendor-only=trueをビルドの引数として与えると、Goのコードは一切参照せずにGopkg.lockから依存を引っ張ってきてくれる。
これを利用してdep ensureによる大量の依存関係のダウンロードにキャッシュを効かせるのが今回のアプローチ。

15
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
6