Dockerで鍵ファイルを環境変数で受け取り、実行時に展開する方法

概要

秘密鍵のフォルダをマウントさせると色々と面倒なので引数化することにした。

手順

1.key ファイルを置換

1
sed -z 's/\n/\\n/g' id_ras

2.dockerファイルに初期値として入力

1
2
3
4
5
6
7
8
FROM ubuntu

ENV key="-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAACFwAAAAdzc2gtcn\nNhAAAAAwEAAQAAAgEAxrGqH4V9Fu1BeFnUo1n77a8qYXUs/9LECnb4lwUeenOXE3Y3LL7G\nuTcttn+B9Q6bzrN3BvDLjo3VKKtWI/X5pRuSf3wML8OMt8CDta0k4bexskmIzEzN/6yTWv\nssc12VLfXrQBtvVNnASb8r9cRKsDxjGmtG7w9eSz0x1ah7HR+Mbuva83hOBVutv+og4fN3\nmEvJMHL9B2OmtO1q+psSFzmu1xoDRhiDk406lUUWQBOwpyn8PjB1X3hrhFkosDpts099lT\nOul3KzWM+2O5+cnLhIw45TA+hXknltp7bg8cFpEdSOePDsLNrSFtTGibq4wtwT/cxXEDwG\nD9UPo6QpRWNs+wl76Be8BZ8E/xpZK4wLB94J7lt4OApH+8ERpGqA+zDz/kuZND6DPFbCDT\nLr1bmOcEqQykhCKno7vim7oPRPLjsEAc80pjlDYc2NFAp5+WoalboettXYRejXTTWNZXPN\nnqq40mhq+c+aIn+LHsyvdLvdYoCjqRx3RrQGZpITnJ5owdR13IwB70Wo1axEOb/PuBwTTu\n5ajWV4Nh6szc0xbkrEl7XrZDOfD5qxXr5EjpkOPy0vvWg932qpIKhhZplDjYzHnY5U+Zic\ngAK9Th7nQJz5jPoThV9dOX7DVLfPChx+f4ite69HbjI6g0Bm9AfPkJzkobqqR6rBR0Wc1C\n0AAAdQn9L4f5/S+H8AAAAHc3NoLXJzYQAAAgEAxrGqH4V9Fu1BeFnUo1n77a8qYXUs/9LE\nCnb4lwUeenOXE3Y3LL7GuTcttn+B9Q6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ly+FJNtISb+RxhIuKgyFP+fNQ0rtuAE15APi1eqp+r8\nJosZaqGmDfRcrjZFFXuU4irmjpyeLZO8hykwRkaVDqzPR8dfBan3Uy/CkjqLPlqnE2kcGD\niiee6oOE7ydtHkKyefIjBBVSqblgmRteqluSY+pA2lnt3f7qCZ2is+xALSj1U/Kzz628XU\nmHDfJko5V3sgT9X+aPyXvTrPgWKSbnVvbOt3SuFBwjhiF5Tqs+/qGSQFpNYePk5qLhvqTO\nKzQYv/Uj2IgnAAAAGWhhc2hpdG9ATWFjQm9vay1Qcm8ubG9jYWwB\n-----END OPENSSH PRIVATE KEY-----\n"
ADD ./run.sh /root/
RUN chmod a+x /root/run.sh && \
mkdir /root/.ssh

CMD [ "/root/run.sh" ]

3.run.shで出力してあげる

1
2
3
4
5
#!/bin/bash

echo $key |sed -z 's/\\n/\n/g' >/root/.ssh/id_ras

sleep 1500

4.ビルドして確認

1
2
3
4
5
docker build -t test .
docker exec -it test /bin/bash

>cd /root/.ssh
>cat id_ras

参考:https://orebibou.com/ja/home/201607/20160714_003/