Githubリポジトリにコミットしたら、自動的に”Docker Hub”でDockerイメージを作成させる
はじめに
こんにちわ、 @genzouw です。
僕は Docker Hub 上でDockerイメージをいくつも公開しています。
- genzouw/mkr : mkr - Command Line Tool For Mackerel written in Go.
- genzouw/gpg : Docker image that give gpg command.
- genzouw/td : This is a Docker image wrapping the command line client tool td of" Treasure Data ". Ruby installation, Gem installation, authentication information, etc. can all be managed by Docker container.
- genzouw/jq : This is Dockerfile repository that wrap jq command.
- genzouw/trim-text : This Docker image is intended to provide a UI that integrates command line text cut operations.
- genzouw/ansize : Convert image to text(ascii art).
いずれも自分の公開Githubリポジトリコミットしたタイミングで、 "Docker Hub"でイメージを自動的に作成するようにしています。
この設定方法を紹介します。
検証環境
$ uname -moi
x86_64 MacBookPro16,1 Darwin
$ bash -version | head -n 1
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
前提
1. Docker Hubのアカウントをもっていること
既に "Docker Hub" でアカウントを作成していることを前提としています。
もしまだアカウント作成されていない場合には、以下のサイトトップページの裏アカウントを作成できます。
作成しておきましょう。
2. Githubのアカウントを持っていること
今回はGithubのリポジトリを連携するため、Githubのアカウントを持っていることを前提としています。
Dockerイメージ自動作業の設定方法
1. "Docker Hub" にログインする
"Docker Hub" にログインします。
ログイン後のトップページ右上に 「Create Repository」 と言うボタンがあるので、これをクリックします。
2. Dockerイメージ名を入力する
今回作成したいDockerイメージ名を入力します。
この後に指定する、連携対象の 公開Githubリポジトリ名 を入力するのが分かりやすいと思います。
ここでは、 後ほど指定します genzouw/hey というGithubリポジトリをもとにDockerイメージを作成する想定なので、同じ名前を入力しておきました。
入力したら、一番下にあるGithubアイコンをクリックして連携設定に進みましょう。
3. 連携対象の 公開Githubリポジトリ名 を選択する
GithubアイコンをクリックしてGithubとの連携が完了すれば、先程のページでGithubリポジトリが選択できるようになっているはずです。
genzouw/hey という Githubリポジトリ を連携させるべく、選択しました。
4. 最期に "Docker Hub" でイメージビルドをさせるためのトリガーを設定する
連携したGithubリポジトリにどんな操作が行われた時にイメージに入ると実行させるかを設定します。
ここでは以下の2つの条件設定しています。いずれかの条件が満たされた場合にDockerイメージのビルド処理が実行されます。
- master ブランチに対してPushされたタイミング → Dockerのタグは latest というタグで作成
- vX.Y.Z のようなバージョンタグがPushされたタイミング → Dockerのタグは vX.Y.Z というタグで作成
設定が終了したら、 「Create & Build」 ボタンをクリックして終了です。
5. 正しく設定されたかを確認
正しく設定が行われていれば以下のようなページが表示されるはずです。
設定された情報が正しく反映されていることがわかります。
作成されたイメージを使ってみる
ことの発端はベンチマークのために hey
コマンドを使いたかったと言うことにあります。
brew
でインストールすればよかったのですが、Mac以外の環境で利用することも想定されたためにDockerイメージを作成しました。
正しくビルドされていればMac環境でもどこでも利用できるはずです。ヘルプを引いてみます。
$ docker run -it --rm genzouw/hey --help
Unable to find image 'genzouw/hey:latest' locally
latest: Pulling from genzouw/hey
b168ae6de529: Pull complete
8020684f1c70: Pull complete
762d362e9268: Pull complete
0bf0f9014622: Pull complete
Digest: sha256:c766841ea5896ee6b045c6f268d661e81e86878bdfce55bdecba5987e3d55524
Status: Downloaded newer image for genzouw/hey:latest
Usage: hey [options...] <url>
Options:
-n Number of requests to run. Default is 200.
-c Number of workers to run concurrently. Total number of requests cannot
be smaller than the concurrency level. Default is 50.
-q Rate limit, in queries per second (QPS) per worker. Default is no rate limit.
-z Duration of application to send requests. When duration is reached,
application stops and exits. If duration is specified, n is ignored.
Examples: -z 10s -z 3m.
-o Output type. If none provided, a summary is printed.
"csv" is the only supported alternative. Dumps the response
metrics in comma-separated values format.
-m HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.
-H Custom HTTP header. You can specify as many as needed by repeating the flag.
For example, -H "Accept: text/html" -H "Content-Type: application/xml" .
-t Timeout for each request in seconds. Default is 20, use 0 for infinite.
-A HTTP Accept header.
-d HTTP request body.
-D HTTP request body from file. For example, /home/user/file.txt or ./file.txt.
-T Content-type, defaults to "text/html".
-a Basic authentication, username:password.
-x HTTP Proxy address as host:port.
-h2 Enable HTTP/2.
-host HTTP Host header.
-disable-compression Disable compression.
-disable-keepalive Disable keep-alive, prevents re-use of TCP
connections between different HTTP requests.
-disable-redirects Disable following of HTTP redirects
-cpus Number of used cpu cores.
(default for current machine is 6 cores)
うまく動作しました。
ひとこと
ということで、Goで作成されたコマンドラインのベンチマークツール hey
を Dockerイメージとして公開しました 。
よかったらお使いくださいませ。
ディスカッション
コメント一覧
まだ、コメントがありません