Dockerの基本的な使い方メモ

連休になったので気になってたdocker弄り。
公式チュートリアルを追うので、入門的な何かです。

前提知識

◆wiki
http://ja.wikipedia.org/wiki/Docker

◆公式
https://www.docker.com/

◆公式 – document
https://docs.docker.com/
対応OSやサンプルの実装が書いてあります
今のところ正式対応してるOS↓ 2015-04-27

Ubuntu
Mac OS X
Microsoft Windows
Amazon EC2
Arch Linux
Binaries
CentOS
CRUX Linux
Debian
Fedora
FrugalWare
Google Cloud Platform
Gentoo
IBM Softlayer
Joyent Compute Service
Microsoft Azure
Rackspace Cloud
Red Hat Enterprise Linux
Oracle Linux
SUSE
Docker Compose

◆公式 – document – Windows Boot2Docker
https://docs.docker.com/installation/windows/
https://github.com/boot2docker/boot2docker
Windowsにも対応してるようですね。
しかしネイティブ実装ではなくVirtualBox上で軽量Linuxを動かすタイプ!…linuxに依存しすぎ
dockerの構成上、sandboxie的な気軽な使い方は困難な模様
あくまでサーバーサイド用で、GUIが絡んでくる物は動かせない。
Xをコンテナ上でって思ったけど画面の出力先が無さそう。Xの転送があるって?
とりあえずGUIが絡む仮想化技術とは競合しなさそうですね。

◆Docker Hub Registry
https://registry.hub.docker.com/
github的なDockerのイメージを置くサービス。
dockerのコマンドが、直接 Docker Hub Registry からデータを読み込んでる辺り、根幹的なエコシステムの一部みたいですね

手始めに以下のコマンドでインストールと確認

$ sudo apt-get install docker.io
以下の追加パッケージがインストールされます:
aufs-tools cgroupfs-mount git git-man liberror-perl
提案パッケージ:
btrfs-tools debootstrap lxc rinse git-daemon-run git-daemon-sysvinit git-doc
git-el git-email git-gui gitk gitweb git-arch git-cvs git-mediawiki git-svn
以下のパッケージが新たにインストールされます:
aufs-tools cgroupfs-mount docker.io git git-man liberror-perl
アップグレード: 0 個、新規インストール: 6 個、削除: 0 個、保留: 0 個。
7,843 kB のアーカイブを取得する必要があります。
この操作後に追加で 44.3 MB のディスク容量が消費されます。
$ dpkg -L docker.io
/.
/usr
/usr/bin
/usr/bin/docker
/usr/share
/usr/share/docker.io
/usr/share/docker.io/contrib
/usr/share/docker.io/contrib/mkimage-rinse.sh
/usr/share/docker.io/contrib/mkimage-busybox.sh
/usr/share/docker.io/contrib/mkimage-yum.sh
/usr/share/docker.io/contrib/mkimage-unittest.sh
/usr/share/docker.io/contrib/mkimage-crux.sh
/usr/share/docker.io/contrib/mkimage-arch-pacman.conf
/usr/share/docker.io/contrib/desktop-integration
/usr/share/docker.io/contrib/desktop-integration/chromium
/usr/share/docker.io/contrib/desktop-integration/chromium/Dockerfile
/usr/share/docker.io/contrib/desktop-integration/README.md
/usr/share/docker.io/contrib/desktop-integration/gparted
/usr/share/docker.io/contrib/desktop-integration/gparted/Dockerfile
/usr/share/docker.io/contrib/mkimage-alpine.sh
/usr/share/docker.io/contrib/mkimage-arch.sh
/usr/share/docker.io/contrib/mkimage-debootstrap.sh
/usr/share/docker.io/contrib/nuke-graph-directory.sh
/usr/share/docker.io/contrib/mkseccomp.sample
/usr/share/docker.io/contrib/mkseccomp.pl
/usr/share/docker.io/contrib/mkimage
/usr/share/docker.io/contrib/mkimage/busybox-static
/usr/share/docker.io/contrib/mkimage/.febootstrap-minimize
/usr/share/docker.io/contrib/mkimage/rinse
/usr/share/docker.io/contrib/mkimage/mageia-urpmi
/usr/share/docker.io/contrib/mkimage/debootstrap
/usr/share/docker.io/contrib/mkimage.sh
/usr/share/docker.io/contrib/host-integration
/usr/share/docker.io/contrib/host-integration/manager.sh
/usr/share/docker.io/contrib/host-integration/Dockerfile.min
/usr/share/docker.io/contrib/host-integration/manager.go
/usr/share/docker.io/contrib/host-integration/manager
/usr/share/docker.io/contrib/host-integration/manager/systemd
/usr/share/docker.io/contrib/host-integration/manager/upstart
/usr/share/docker.io/contrib/host-integration/Dockerfile.dev
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/docker.io
/usr/share/zsh
/usr/share/zsh/vendor-completions
/usr/share/zsh/vendor-completions/_docker
/usr/share/doc
/usr/share/doc/docker.io
/usr/share/doc/docker.io/copyright
/usr/share/doc/docker.io/README.Debian
/usr/share/doc/docker.io/README.md.gz
/usr/share/doc/docker.io/changelog.Debian.gz
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/docker-stop.1.gz
/usr/share/man/man1/docker-diff.1.gz
/usr/share/man/man1/docker-restart.1.gz
/usr/share/man/man1/docker-pull.1.gz
/usr/share/man/man1/docker-rm.1.gz
/usr/share/man/man1/docker-tag.1.gz
/usr/share/man/man1/docker-info.1.gz
/usr/share/man/man1/docker-unpause.1.gz
/usr/share/man/man1/docker-history.1.gz
/usr/share/man/man1/docker-ps.1.gz
/usr/share/man/man1/docker-images.1.gz
/usr/share/man/man1/docker-save.1.gz
/usr/share/man/man1/docker-build.1.gz
/usr/share/man/man1/docker-attach.1.gz
/usr/share/man/man1/docker-load.1.gz
/usr/share/man/man1/docker-rmi.1.gz
/usr/share/man/man1/docker.1.gz
/usr/share/man/man1/docker-pause.1.gz
/usr/share/man/man1/docker-logout.1.gz
/usr/share/man/man1/docker-create.1.gz
/usr/share/man/man1/docker-kill.1.gz
/usr/share/man/man1/docker-rename.1.gz
/usr/share/man/man1/docker-logs.1.gz
/usr/share/man/man1/docker-events.1.gz
/usr/share/man/man1/docker-top.1.gz
/usr/share/man/man1/docker-start.1.gz
/usr/share/man/man1/docker-login.1.gz
/usr/share/man/man1/docker-cp.1.gz
/usr/share/man/man1/docker-export.1.gz
/usr/share/man/man1/docker-run.1.gz
/usr/share/man/man1/docker-push.1.gz
/usr/share/man/man1/docker-import.1.gz
/usr/share/man/man1/docker-inspect.1.gz
/usr/share/man/man1/docker-commit.1.gz
/usr/share/man/man1/docker-wait.1.gz
/usr/share/man/man1/docker-exec.1.gz
/usr/share/man/man1/docker-stats.1.gz
/usr/share/man/man1/docker-version.1.gz
/usr/share/man/man1/docker-port.1.gz
/usr/share/man/man1/docker-search.1.gz
/usr/share/man/man5
/usr/share/man/man5/Dockerfile.5.gz
/usr/lib
/usr/lib/docker.io
/usr/lib/docker.io/dockerinit
/lib
/lib/systemd
/lib/systemd/system
/lib/systemd/system/docker.service
/lib/systemd/system/docker.socket
/lib/udev
/lib/udev/rules.d
/lib/udev/rules.d/80-docker.io.rules
/etc
/etc/default
/etc/default/docker
/etc/init.d
/etc/init.d/docker
/etc/bash_completion.d
/etc/bash_completion.d/docker
/etc/init
/etc/init/docker.conf

コマンド一覧を確認

勝手に翻訳。
チュートリアルで利用するコマンドにハイライト。

attach    実行中のコンテナに接続
build     Dockerfileからイメージを構築
commit    コンテナの変化から新しいイメージを作成
cp        ホストパスにコンテナのファイルシステムからファイル/フォルダをコピー
create    新しいコンテナを作成
diff      コンテナのファイルシステム上の変更を点検
events    サーバからのリアルタイムイベントを取得
exec      実行中のコンテナ内のコマンドを実行
export    tarアーカイブとしてコンテナの内容をストリーミング
history   イメージの履歴を表示
images    イメージの一覧表示
import    tarボールの内容から新しいファイルシステムイメージを作成
info      システム全体情報の表示
inspect   コンテナまたはイメージの低レベルの情報を返す
kill      実行中のコンテナを停止させる
load      tarアーカイブからイメージをロード
login     Docker registry server サーバにログイン
logout    Docker registry server からログアウト
logs      コンテナのログをフェッチ
port      ホストのポートとコンテナのポート紐付ける
pause     コンテナ内のすべてのプロセスを一時停止
ps        コンテナの一覧表示
pull      Docker registry server からイメージまたはリポジトリをプール
push      Docker registry server にイメージやリポジトリを プッシュ
rename    既存のコンテナの名前を変更
restart   実行中のコンテナを再起動
rm        1つまたは複数のコンテナを削除する
rmi       1つまたは複数のイメージを削除する
run       新しいコンテナでコマンドを実行する
save      tarアーカイブにイメージを保存する
search    Docker Hub上のイメージを検索
start     停止したコンテナを再開
stats     1以上のコンテナ'リソースの使用状況統計のライブストリームを表示
stop      実行中のコンテナを停止
tag       リポジトリのイメージにタグをつける
top       コンテナの実行中のプロセスを検索
unpause   一時停止したコンテナの一時停止を解除
version   Dockerのバージョン情報を表示
wait      コンテナが停止するまで待つ

何から手を付ければいいか

まずDocker公式のチュートリアルをやるべき。
チュートリアルは個人の環境からも出来るように公式リポジトリにチュートリアル用イメージが置いてある模様です。

The best way to understand Docker is to try it!
https://www.docker.com/tryit/

指定したコマンドを入力して順に使い方を理解するタイプのチュートリアルです。
Tipsを見れば大体何をすれば良いのか解ります。
どうしても分からない場合は Show the answer をクリックすれば答えが出てきます。
正解を入力すると右上に 「Well done! Let’s move to the next assignment.」の文字とともにNextボタンが現れるのでクリック。

以下ネタバレ。

>>チュートリアル0
Docker コマンドを入力してみよう的な。
まずversionの表示ですね

you@tutorial:~$ docker version
Docker Emulator version 0.1.3

Emulating:
Client version: 0.5.3
Server version: 0.5.3
Go version: go1.1

>>チュートリアル1
Dockerコンテナの元となる”イメージ”を「Docker Hub Registry」から検索する。
tutorialという名前の”イメージ”を探します

you@tutorial:~$ docker search tutorial
Found 1 results matching your query ("tutorial")
NAME DESCRIPTION
learn/tutorial An image for the interactive tutorial

>>チュートリアル2
1で見つけた Docker”イメージ”を docker pull コマンドでダウンロード。
正式名は learn/tutorial という名称。 / はディレクティブな表現みたいですね

you@tutorial:~$ docker pull learn/tutorial
Pulling repository learn/tutorial from https://index.docker.io/v1
Pulling image 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c (precise) from ubuntu
Pulling image b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc (12.10) from ubuntu
Pulling image 27cf784147099545 () from tutorial

>>チュートリアル3
pullしてきたチュートリアル用”イメージ”で Hello World する。

you@tutorial:~$ docker run learn/tutorial echo "hello world"
hello world

>>チュートリアル4
“イメージ”に apt-get で ping コマンドをインストールする。
“イメージ”に変更が入った時点で、”コンテナ”が作成されます。
つまり”コンテナ”は”イメージ”からの差分データの事。

you@tutorial:~$ docker run learn/tutorial apt-get install -y ping
Reading package lists...
Building dependency tree...
The following NEW packages will be installed:
iputils-ping
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 56.1 kB of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 56.1 kB in 1s (50.3 kB/s)
Selecting previously unselected package iputils-ping.
(Reading database ... 7545 files and directories currently installed.)
Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ...
Setting up iputils-ping (3:20101006-1ubuntu1) ...

>>チュートリアル5
pingコマンドをインストールした”コンテナ”を別名でコミットします。(イメージ化)
具体的にはlearn/tutorial”イメージ”と、698″コンテナ”を元に、learn/ping”イメージ”を作成する
コミットする”コンテナ”は ps コマンドで確認出来ます。

you@tutorial:~$ docker ps -l
ID IMAGE COMMAND CREATED STATUS PORTS
6982a9948422 ubuntu:12.04 apt-get install ping 1 minute ago Exit 0

IDが 6982a9948422 となってますが、重複しない限り先頭数文字の 698 で認識できるようです。

you@tutorial:~$ docker commit 698 learn/ping
effb66b31edb

>>チュートリアル6
コミットして出来上がった”イメージ”にpingコマンドを実行させる。

you@tutorial:~$ docker run learn/ping ping google.com
PING www.google.com (74.125.239.129) 56(84) bytes of data.
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=1 ttl=55 time=2.23 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=2 ttl=55 time=2.30 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=3 ttl=55 time=2.27 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=4 ttl=55 time=2.30 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=5 ttl=55 time=2.25 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=6 ttl=55 time=2.29 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=7 ttl=55 time=2.23 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=8 ttl=55 time=2.30 ms
64 bytes from nuq05s02-in-f20.1e100.net (74.125.239.148): icmp_req=9 ttl=55 time=2.35 ms
-> This would normally just keep going. However, this emulator does not support Ctrl-C, so we quit here.

>>チュートリアル7
“イメージ”情報の確認。
変化内容がバイナリではなくjsonという事がわかります。
つまり、テキストベースの設定として扱われる様です。

you@tutorial:~$ docker inspect eff
[2013/07/30 01:52:26 GET /v1.3/containers/efef/json
{
  "ID": "efefdc74a1d5900d7d7a74740e5261c09f5f42b6dae58ded6a1fde1cde7f4ac5",
  "Created": "2013-07-30T00:54:12.417119736Z",
  "Path": "ping",
  "Args": [
      "www.google.com"
  ],
  "Config": {
      "Hostname": "efefdc74a1d5",
      "User": "",
      "Memory": 0,
      "MemorySwap": 0,
      "CpuShares": 0,
      "AttachStdin": false,
      "AttachStdout": true,
      "AttachStderr": true,
      "PortSpecs": null,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": null,
      "Cmd": [
          "ping",
          "www.google.com"
      ],
      "Dns": null,
      "Image": "learn/ping",
      "Volumes": null,
      "VolumesFrom": "",
      "Entrypoint": null
  },
  "State": {
      "Running": true,
      "Pid": 22249,
      "ExitCode": 0,
      "StartedAt": "2013-07-30T00:54:12.424817715Z",
      "Ghost": false
  },
  "Image": "a1dbb48ce764c6651f5af98b46ed052a5f751233d731b645a6c57f91a4cb7158",
  "NetworkSettings": {
      "IPAddress": "172.16.42.6",
      "IPPrefixLen": 24,
      "Gateway": "172.16.42.1",
      "Bridge": "docker0",
      "PortMapping": {
          "Tcp": {},
          "Udp": {}
      }
  },
  "SysInitPath": "/usr/bin/docker",
  "ResolvConfPath": "/etc/resolv.conf",
  "Volumes": {},
  "VolumesRW": {}

>>チュートリアル8
出来上がった”イメージ”を「Docker Hub Registry」へpushする。

you@tutorial:~$ docker push learn/ping
The push refers to a repository [learn/ping] (len: 1)
Processing checksums
Sending image list
Pushing repository learn/ping (1 tags)
Pushing 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c
Image 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c already pushed, skipping
Pushing tags for rev [8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c] on {https://registry-1.docker.io/v1/repositories/learn/p
ing/tags/latest}
Pushing a1dbb48ce764c6651f5af98b46ed052a5f751233d731b645a6c57f91a4cb7158
Pushing 11.5 MB/11.5 MB (100%)
Pushing tags for rev [a1dbb48ce764c6651f5af98b46ed052a5f751233d731b645a6c57f91a4cb7158] on {https://registry-1.docker.io/v1/repositories/learn/p
ing/tags/latest}

チュートリアル最後の push は実際にはアカウント登録が必要な模様
https://hub.docker.com/account/signup/

基本的に、Docker Hub Registry から始まり Docker Hub Registry に終わるという流れのようです。
元となるイメージをpullし、好みに編集し、コミット、最後にpush。
pushは任意か。

投稿者:

コメントを残す

メールアドレスが公開されることはありません。