5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Docker / Go言語 — docker コンテナに自動付与されるデフォルト名を直接生成してみる

Last updated at Posted at 2018-09-01

"names generator" が コンテナにランダムなデフォルト名を与えていそう。

忙しい人のために

Github Gistにバイナリをアップしてあるので、こちらで動作確認可能。

$ git clone https://gist.github.com/YumaInaura/0c8de43e3342db53059584661d0b491e names-generator
$ ./names-generator/names-generator
affectionate_matsumoto
$ ./names-generator/names-generator
recursing_vaughan

( ref moby/LICENSE at master · moby/moby )

「ランダムなデフォルト名」とは

次のように自動で付与される名前のこと。

  • quizzical_payne
  • sharp_pike
  • optimistic_kepler
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
dbd46a366aa3        alpine              "sleep 100"         2 seconds ago       Up 1 second                             optimistic_kepler
454ee02db352        alpine              "sleep 100"         3 seconds ago       Up 2 seconds                            sharp_pike
1d19cef9660b        alpine              "sleep 100"         4 seconds ago       Up 3 seconds                            quizzical_payne

"names generator" はGo言語製

docker 自体、ほとんどGoで作られているらしいので、それと同じく。

Dockerコードベースの90%以上を占めるGo言語の概要と特徴 | tracpath:Works

よく見るとコードジョークも入っている。

...
// GetRandomName generates a random name from the list of adjectives and surnames in this package
// formatted as "adjective_surname". For example 'focused_turing'. If retry is non-zero, a random
// integer between 0 and 10 will be added to the end of the name, e.g `focused_turing3`
func GetRandomName(retry int) string {
begin:
	name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))])
	if name == "boring_wozniak" /* Steve Wozniak is not boring */ {
		goto begin
	}

	if retry > 0 {
		name = fmt.Sprintf("%s%d", name, rand.Intn(10))
	}
	return name
}

利用例

インストール

docker/docker レポジトリが巨大なのでわりと時間がかかる。

$ go get -v github.com/docker/docker/
github.com/docker/docker (download)

ディレクトリを移動

$ cd $(go env GOPATH)/src/github.com/docker/docker/pkg/namesgenerator/

実行

$ go run cmd/names-generator/main.go
hungry_mccarthy
$ go run cmd/names-generator/main.go
mystifying_bhaskara

ビルドして実行

$ go build -o ~/tmp/names-generator cmd/names-generator/main.go
$ ~/tmp/names-generator
quirky_lederberg
$ ~/tmp/names-generator
zen_torvalds

Versions

  • Docker version 18.06.0-ce, build 0ffa825
  • go version go1.10.3 darwin/amd64

Ref

Links

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?