{
    "componentChunkName": "component---src-templates-blog-post-jsx",
    "path": "/post/how-to-clone-private-repository-in-docker-build/",
    "result": {"data":{"site":{"siteMetadata":{"title":"WEB EGG","author":"Leko - CTO at Yuimedi"}},"markdownRemark":{"id":"24d54759-19d3-5bf9-9bc0-99c7c2e1c04e","excerpt":"表題についてざっと調べると、 Using SSH Private keys securely in Docker build Forward ssh key agent into container · Issue #6396 · docker/docker…","html":"<p>表題についてざっと調べると、</p>\n<ul>\n<li><a href=\"http://blog.cloud66.com/using-ssh-private-keys-securely-in-docker-build/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Using SSH Private keys securely in Docker build</a></li>\n<li><a href=\"https://github.com/docker/docker/issues/6396\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Forward ssh key agent into container · Issue #6396 · docker/docker</a></li>\n</ul>\n<p>などの情報が出てくるのですが、そんな大げさな話じゃなく単にcloneがしたいだけなんです。<br>\n調べても意外と出てこなかったので<code>docker build</code>の待ち時間に記事として残しておきます。<br>\nなお、記事のタイトルは非正確で、厳密にはプライベートリポジトリをnpm installする話です。</p>\n<!--more-->\n<h2 id=\"やりたいこと\" style=\"position:relative;\"><a href=\"#%E3%82%84%E3%82%8A%E3%81%9F%E3%81%84%E3%81%93%E3%81%A8\" aria-label=\"やりたいこと permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>やりたいこと</h2>\n<p>npmでは、</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"dependencies\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token property\">\"some_package\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"my/private-repo#0.8.0\"</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>のように、<code>ユーザ名/リポジトリ名#タグ</code>形式でGithubのリポジトリを指定可能<sup id=\"fnref-931:1\"><a href=\"#fn-931:1\" rel=\"footnote\">1</a></sup>です。<br>\nnpmにprivate packageを公開するのは有料<sup id=\"fnref-931:2\"><a href=\"#fn-931:2\" rel=\"footnote\">2</a></sup>なので、なんとか無料で運用したい。</p>\n<p>Githubのprivateリポジトリをcloneしてくるには、事前にSSH鍵の登録が必要です。<br>\n「デプロイキー使えば良いのでは？」と思って試したら行けたので、その方法が安牌だと思います。</p>\n<h2 id=\"clone用の鍵を生成してgithubに登録\" style=\"position:relative;\"><a href=\"#clone%E7%94%A8%E3%81%AE%E9%8D%B5%E3%82%92%E7%94%9F%E6%88%90%E3%81%97%E3%81%A6github%E3%81%AB%E7%99%BB%E9%8C%B2\" aria-label=\"clone用の鍵を生成してgithubに登録 permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>clone用の鍵を生成してGithubに登録</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">ssh-keygen -t rsa -C Githubのメールアドレス</code></pre></div>\n<blockquote>\n<p>— <a href=\"http://qiita.com/suthio/items/2760e4cff0e185fe2db9\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">お前らのSSH Keysの作り方は間違っている – Qiita</a></p>\n</blockquote>\n<p>でSSH鍵を生成します。<br>\n念のため動作確認。</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ ssh -o StrictHostKeyChecking=no -i 生成した鍵のパス -T git@github.com\nWarning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.\nHi blockenio/domain! You've successfully authenticated, but GitHub does not provide shell access.</code></pre></div>\n<p>生成した鍵をコピーし、cloneしたいリポジトリの設定の「Deploy keys」に貼り付けます。<br>\n貼り付ける際に、「Allow write access」のチェックはOFFにしてください。不要です。</p>\n<p>貼り付けたら、 <strong>生成した鍵をGithubのリポジトリにコミット</strong> します。<br>\nコミットせずに開発メンバー全員が同様の方法を行う、とかでも対処可能ですが、<br>\nこのリポジトリにしか影響がないし読み取り専用にしてあるので、大した影響はないだろうということでコミットしちゃっています。</p>\n<h2 id=\"dockerfileで使う\" style=\"position:relative;\"><a href=\"#dockerfile%E3%81%A7%E4%BD%BF%E3%81%86\" aria-label=\"dockerfileで使う permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Dockerfileで使う</h2>\n<p>カレントディレクトリ以下に鍵が手に入れば、あとはなんとでもなります。<br>\nDockerfileはこんな感じ。</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">FROM node:6.9.4\n\nRUN mkdir /app /root/.ssh\nWORKDIR /app\n\nADD ./package.json /app/package.json\nADD ./certs/readonly.pub /root/.ssh/id_rsa.pub\nADD ./certs/readonly /root/.ssh/id_rsa\nRUN ssh -o StrictHostKeyChecking=no -T git@github.com || true\nRUN npm set progress=false \\\n    &amp;&amp; npm -q install</code></pre></div>\n<p><code>certs</code>に鍵のファイルを入れてある場合の書き方です。<br>\nホスト側で作った鍵だと警告が出てしまったので事前に警告を無視してknown_hostsに書き加えてからnpm installしています</p>\n<h2 id=\"まとめ\" style=\"position:relative;\"><a href=\"#%E3%81%BE%E3%81%A8%E3%82%81\" aria-label=\"まとめ permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>まとめ</h2>\n<p>ググっても出てこない情報は、自分が知っている情報の組み合わせで解消することがまれにある。 この方法が正しいのかどうか確証がもてませんが、実現はできたし考える限り大きなリスクはなさそうと判断します。</p>\n<ol>\n  <li id=\"fn-931:1\">\n    <a href=\"https://docs.npmjs.com/files/package.json#github-urls\">GitHub URLs — package.json | npm Documentation</a> <a href=\"#fnref-931:1\" rev=\"footnote\">↩</a>\n  </li>\n  <li id=\"fn-931:2\">\n    <a href=\"https://www.npmjs.com/pricing\">Pricing — npm</a> <a href=\"#fnref-931:2\" rev=\"footnote\">↩</a>\n  </li>\n</ol>  ","timeToRead":3,"frontmatter":{"title":"Dockerのビルド時にGithubのプライベートリポジトリをcloneする","tags":["Docker","Git","npm"],"date":"January 31, 2017","featuredImage":null}}},"pageContext":{"slug":"/how-to-clone-private-repository-in-docker-build/","previous":{"fields":{"slug":"/automate-deploy-to-npm-with-circleci/"},"frontmatter":{"title":"npmへのデプロイをCircleCIで自動化してみた","tags":["CircleCI","Nodejs","npm"]}},"next":{"fields":{"slug":"/dockerize-friends-lang/"},"frontmatter":{"title":"すごーい！きみはフレンズ言語をDockerizeできるフレンズなんだね！","tags":["Docker","F#","Github","Mac"]}}}},
    "staticQueryHashes": ["2585454260","2954598359"]}