LoginSignup
24
27

More than 5 years have passed since last update.

docker buildする際にhost側のssh keyを使ってbuildする

Last updated at Posted at 2019-02-15

docker buildする際にhost側のssh keyを使いつつbuildしたいことがある。
docker run -vなら~/.sshをmountするとかでhost側のssh keyが使えるけど、buildではできなかった。
これがDocker v18.09からできるようになった。(まだexperimental扱いではある)
keyをADDして最後に消す、みたいなワークアラウンドが不要になりそう。

buildkitの有効化

$ export DOCKER_BUILDKIT=1

Dockerfile

# syntax=docker/dockerfile:1.0.0-experimental

FROM ubuntu:18.04
RUN apt-get update

RUN apt-get install -y git
RUN apt-get install -y openssh-client
RUN apt-get install -y ruby

RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.e >> ~/.ssh/known_hosts
RUN --mount=type=ssh git clone git@github.e:org/repo.git

build

$ docker build --ssh default .
  • 先頭に# syntax=docker/dockerfile:1.0.0-experimentalを書くのがポイント
  • ssh-keyscanでknow_hostsにhost情報を書いとかないと、Host key verification failed.で蹴られてしまう
  • 上記の例だとforward agentを使っている。

hostのssh keyを直接mountする場合

RUN --mount=type=secret,id=ssh,target=/root/.ssh/id_rsa git clone 〜

build

docker build --secret id=ssh,src=$HOME/.ssh/id_rsa .
  • これはssh key以外のmountにも使える

参考

24
27
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
24
27