LoginSignup
17
19

More than 5 years have passed since last update.

Dockerで独自レポジトリからgo getするお

Posted at

Dockerでbuildしたときにプライベートレポジトリからgo getしたい。

やらなければならないことは3つあります。

  • .gitconfigの変更
  • 秘密鍵の配置
  • .ssh/configの変更

これだけです。

.gitconfigの変更

GithubのプライベートレポジトリやGithubEnterprise等との通信でhttpを使っているとgo getした時に毎回パスワードを聞かれてしまい、Docker内では失敗してしまいます。
そこでsslの通信に変更します。

変更は.gitconfigで指定します。下記が例です。

.gitconfig
[url "git@ghe.example.org:"]
  insteadOf = https://ghe.example.org/

これでssl通信でアクセスするよう変更されます。

秘密鍵の配置

パスワード無しでアクセスするためにsslの通信で使う秘密鍵を配置します。
具体的には/root/.sshの中にid_rsaファイルを配置し、700の権限を付与します。

秘密鍵の作り方はgithubなどを見て下さい。

.ssh/configの変更

sshで初めて接続する場合、未知のホストからの接続という警告が出てしまいます。
それを回避するためにオプションを設定します。

.ssh/config
StrictHostKeyChecking no

これで警告がでなくなります。

Dockerファイルにすると

これらをDockerファイルにすると

Dockerfile
FROM golang:1.5

RUN mkdir -p /root/.ssh
ADD id_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host ghe.example.org\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

RUN echo "[url \"git@ghe.example.org:\"]\n  insteadOf = https://ghe.example.org/" >> ~/.gitconfig

RUN go get ghe.example.org/oreno/library/...

もちろんgo getだけでなくgoのライブラリのgbでも使えました。

17
19
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
17
19