二回目である今回はDockerの基本操作について解説します。
(一回目の記事はこちら)
コンテナ操作の基本
Dockerイメージの入手
最初に、基本となるDockerイメージを入手します。Dockerイメージは、Docker Hub Registry(https://hub.docker.com/)にて公開されています。
公開されているイメージは、docker searchコマンドで検索できます。CentOSのイメージを検索した場合の例がこちらです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
Dockerイメージの検索 # docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 1842 [OK] docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 63 [OK] docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.7 x86_64 / EPEL/IUS Repos / Ope... 14 [OK] docker.io docker.io/jdeathe/centos-ssh-apache-php CentOS-6 6.7 x86_64 / Apache / PHP / PHP M... 11 [OK] docker.io docker.io/million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 9 [OK] docker.io docker.io/blalor/centos Bare-bones base CentOS 6.5 image 8 [OK] docker.io docker.io/nimmis/java-centos This is docker images of CentOS 7 with dif... 7 [OK] docker.io docker.io/torusware/speedus-centos Always updated official CentOS docker imag... 7 [OK] docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 5 [OK] docker.io docker.io/jdeathe/centos-ssh-mysql CentOS-6 6.7 x86_64 / MySQL. 4 [OK] docker.io docker.io/nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK] docker.io docker.io/centos/mariadb55-centos7 2 [OK] docker.io docker.io/nickistre/centos-lamp LAMP on centos setup 2 [OK] docker.io docker.io/feduxorg/centos-postgresql Centos Image with postgres 1 [OK] docker.io docker.io/layerworx/centos CentOS container with etcd, etcdctl, confd... 1 [OK] docker.io docker.io/lighthopper/orientdb-centos A Dockerfile for creating an OrientDB imag... 1 [OK] docker.io docker.io/nathonfowlie/centos-jira JIRA running on the latest version of CentOS 1 [OK] docker.io docker.io/softvisio/centos Centos 1 [OK] docker.io docker.io/yajo/centos-epel CentOS with EPEL and fully updated 1 [OK] docker.io docker.io/blacklabelops/centos Blacklabelops Centos 7 base image without ... 0 [OK] docker.io docker.io/januswel/centos yum update-ed CentOS image 0 [OK] docker.io docker.io/jsmigel/centos-epel Docker base image of CentOS w/ EPEL installed 0 [OK] docker.io docker.io/lighthopper/openjdk-centos A Dockerfile for creating an OpenJDK image... 0 [OK] docker.io docker.io/pdericson/centos Docker image for CentOS 0 [OK] docker.io docker.io/timhughes/centos Centos with systemd installed and running 0 [OK] |
様々なイメージを様々な人が公開しています。OFFICIALの欄に[OK]と表示されているものが、CentOSのオフィシャルイメージです。あらかじめアプリケーションをインストールしたものがいくつも公開されています。
Dockerイメージのダウンロードは、docker pullコマンドで行います。CentOS7のイメージをダウンロードする場合の実行例がこちらです。
|
Dockerイメージのダウンロード例 # docker pull centos Using default tag: latest Trying to pull repository docker.io/library/centos ... latest: Pulling from library/centos 47d44cb6f252: Pull complete 838c1c5c4f83: Pull complete 5764f0a31317: Pull complete 60e65a8e4030: Pull complete library/centos:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security. Digest: sha256:8072bc7c66c3d5b633c3fddfc2bf12d5b4c2623f7004d9eed6aae70e0e99fbd7 Status: Downloaded newer image for docker.io/centos:latest |
この例のように、いくつかのイメージが一括してダウンロードされます。ダウンロードしたイメージは、次のようにして確認することができます。
|
# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ①docker.io/centos latest 60e65a8e4030 3 weeks ago 196.6 MB ・ ・ |
Dockerのイメージは、リポジトリとタグを”:”で区切って指定します。例えば、①のCentOS 7 のイメージは、docker.io/centos:latestという名称で利用します。
コンテナの作成
コンテナの起動というのは、このイメージの上でプロセスを起動することを指します。Dockerは指定したイメージを展開し、コンテナのファイルシステムセットとして使用します。
コンテナの作成には、docker run コマンドを使います。次のような書式で利用します。
|
docker run [<options>] <image> <command> [<arg>] |
使用するイメージをで、そのイメージ上で動かすコマンドとその引数をとで指定します。
docker.io/centos:latest というイメージでbashを起動する「centos7」という名前のコンテナを作成する場合のコマンドがこちらです。
|
・基本的なコンテナの起動例 # docker run -it --name centos7 docker.io/centos:latest /bin/bash [root@b18de31e55ec /]#ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var |
この例では、docker run にオプションとして「-it」を付けています。これは対話(Interactive)モードで、TTY(端末・コンソール)を割り当てるという指定です。
起動したコンテナの中でlsコマンドなどを実行することが出来ます。もちろん viなどを使ってファイルを修正することもできます。
コンテナは、起動した/bin/bashのプロセスが動いている間だけ動作します。つまりこのシェルをexitした時点でコンテナが停止します。
|
・コンテナの停止 [root@b18de31e55ec /]# exit |
コンテナを停止することなく、操作を元のシェルに戻したい場合には、Ctrl-P + Ctrl-qでコンテナのTTYを抜けることができます。
コンテナへの再接続
動作しているコンテナに接続するには、次のようにdocker attach コマンドにコンテナ名を指定して実行します。
|
・コンテナへの再接続 # docker attach centos7 …エラーがなければ接続できている …改行するとプロンプトが出る [root@b18de31e55ec /]# [root@b18de31e55ec /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var |
動作中のコンテナの確認
動作中のコンテナを調べるには、docker ps コマンドを使います。
|
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b18de31e55ec docker.io/centos:latest "/bin/bash" 9 minutes ago Up 9 minutes centos7 -aオプションを指定すると、停止中のコンテナも表示されます。 # docker ps -a 8cc776959b0f docker.io/centos:latest "/bin/bash" 5 seconds ago Exited (0) 1 seconds ago centos7a b18de31e55ec docker.io/centos:latest "/bin/bash" 14 minutes ago Up 14 minutes centos7 |
コンテナ情報の確認
Dockerのコンテナの詳細な状態を知りたい場合には、docker inspectを使います。Dockerの起動時に指定したプロセスや、コンテナに割り当てたリソースなどを確認することができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
# docker inspect centos7 [ { "Id": "b18de31e55ec5c02721193fab1d815f9b5fd3d32eebad4b86330b792a5ce2c0a", "Created": "2016-01-19T11:17:47.272087891Z", "Path": "/bin/bash", "Args": [], "State": { "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 2563, "ExitCode": 0, "Error": "", "StartedAt": "2016-01-19T11:17:47.685386072Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "60e65a8e4030022260a4f84166814b2683e1cdfc9725a9c262e90ba9c5ae2332", "NetworkSettings": { "Bridge": "", "EndpointID": "4aeb09fdcce86c3d8c76115f222657844dc4fa7ca0d56d8b997e6d1708d42717", "Gateway": "172.17.42.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "HairpinMode": false, "IPAddress": "172.17.0.1", "IPPrefixLen": 16, "IPv6Gateway": "", "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:01", "NetworkID": "b1d897f4c186fdaffcebd0ed10a57721ed871efad61bba223f74d362ddd47b31", "PortMapping": null, "Ports": {}, "SandboxKey": "/var/run/docker/netns/b18de31e55ec", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null }, "ResolvConfPath": "/var/lib/docker/containers/b18de31e55ec5c02721193fab1d815f9b5fd3d32eebad4b86330b792a5ce2c0a/resolv.conf", "HostnamePath": "/var/lib/docker/containers/b18de31e55ec5c02721193fab1d815f9b5fd3d32eebad4b86330b792a5ce2c0a/hostname", "HostsPath": "/var/lib/docker/containers/b18de31e55ec5c02721193fab1d815f9b5fd3d32eebad4b86330b792a5ce2c0a/hosts", "LogPath": "/var/lib/docker/containers/b18de31e55ec5c02721193fab1d815f9b5fd3d32eebad4b86330b792a5ce2c0a/b18de31e55ec5c02721193fab1d815f9b5fd3d32eebad4b86330b792a5ce2c0a-json.log", "Name": "/centos7", "RestartCount": 0, "Driver": "devicemapper", "ExecDriver": "native-0.2", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LxcConf": [], "Memory": 0, "MemorySwap": 0, "CpuShares": 0, "CpuPeriod": 0, "CpusetCpus": "", "CpusetMems": "", "CpuQuota": 0, "BlkioWeight": 0, "OomKillDisable": false, "MemorySwappiness": -1, "Privileged": false, "PortBindings": {}, "Links": null, "PublishAllPorts": false, "Dns": null, "DnsSearch": null, "ExtraHosts": null, "VolumesFrom": null, "Devices": [], "NetworkMode": "default", "IpcMode": "", "PidMode": "", "UTSMode": "", "CapAdd": null, "CapDrop": null, "GroupAdd": null, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "SecurityOpt": null, "ReadonlyRootfs": false, "Ulimits": null, "LogConfig": { "Type": "json-file", "Config": {} }, "CgroupParent": "", "ConsoleSize": [ 0, 0 ] }, "GraphDriver": { "Name": "devicemapper", "Data": { "DeviceId": "7", "DeviceName": "docker-253:1-67259332-b18de31e55ec5c02721193fab1d815f9b5fd3d32eebad4b86330b792a5ce2c0a", "DeviceSize": "107374182400" } }, "Mounts": [], "Config": { "Hostname": "b18de31e55ec", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "ExposedPorts": null, "PublishService": "", "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": null, "Cmd": [ "/bin/bash" ], "Image": "docker.io/centos:latest", "Volumes": null, "VolumeDriver": "", "WorkingDir": "", "Entrypoint": null, "NetworkDisabled": false, "MacAddress": "", "OnBuild": null, "Labels": {} } } ] |
コンテナの停止
動作しているコンテナをホスト側から停止することもできます。次のようなdocker stop コマンドにコンテナ名を指定して実行します。
|
コンテナの停止 # docker stop centos7 centos7 |
この処理を行うと、コンテナプロセスには、TERMシグナル、KILLシグナルが送られます。つまり、コンテナプロセスを強制終了することになります。
コンテナの起動
停止しているコンテナを再度動かすこともできます。次のようにdocker startコマンドにコンテナ名を指定して実行します。
|
・コンテナの起動 # docker start centos7 |
コンテナの再起動
動作しているコンテナを再起動するには、docker restart コマンドを使います。このコマンドを実行すると、コンテナのプロセスを強制終了し、再度スタートします。
|
# docker restart centos7 centos7 |
コンテナのコミット
コンテナの中でファイルを編集しても、コンテナを削除するとすべて変更は削除されてしまいます。ただし、コンテナの状態を新しいイメージとして保管することができます。
保管は、docker commitで行います。
docker commit は、コンテナを指定したローカルリポジトリに保管します。タグも指定することができます。また、-aオプションで作成者を、-mオプションでメッセージを付けることができます。
centos7というコンテナをlocalrepoというリポジトリに保管する例がこちらです。
|
・コンテナのコミット # docker commit -a Takeshi_Sato -m "CentOS 7 test image" centos7 localrepo:test 32f79088593dc06e79c3fed9e0732ec7a35f179034e6faf3f4b11db906fee925 保管したイメージは、docker imageで確認することができます。 # docker images localrepo REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE localrepo test 32f79088593d 15 seconds ago 196.6 MB |
コンテナの削除
作成したコンテナを削除するには、docker rm を使います。コンテナを削除すると、コンテナ内で作成したファイルなどはすべて破棄されてしまいますので、保管が必要な場合にはあらかじめ docker commit で新しいイメージとして保管する必要があります。
|
・コンテナの削除 # docker rm -f centos7 centos7 コンテナの削除は、停止中にしか行えません。強制的にコンテナを停止して削除する場合には、-fオプションを使います。 ・コンテナの強制削除 # docker rm -f cents7 |
アプリケーション環境のコンテナ化とサービスの公開
Dockerは、KVMなどの仮想マシンに比べるとはるかに小さいサイズでイメージを管理することが出来ます。そのため、アプリケーションの実行環境とアプリケーションやコンテンツを一緒にDockerイメージとして保存し、バージョン管理なども行うことが出来ます。
Dockerコンテナ上にWWWサーバとコンテンツの環境を構築する例を使って、アプリケーション環境のコンテナ化とサービスの公開について説明します。
なお、次のような手順で作成していきます。
(1)WWWサーバの元となるコンテナを作成する
(2)WWWサーバの設定を行う
(3)WWWコンテンツを配置する
(4)イメージを保存する(コンテナ化)
(5)保存したイメージで新しいコンテナを作成し、動作確認をする
(6)コンテナサービスの公開
今後の実行例ではコンテナの上で実行すべきものと、ホスト上で実行すべきものがあります。紛らわしいため、ホスト側で実行すべきものには、(ホスト上)、コンテナ上で実行べきものには、(コンテナ上)と明記します。
(1)WWWサーバの元となるコンテナを作成する
最初に、WWWサーバの元となるコンテナを作成します。centos7の公式イメージを使って、/bin/bashを起動します。このとき、ホストからWWWコンテンツを渡すためにボリュームを共有しておきます。WWWコンテンツが入っているディレクトリ(ここでは/home/admin/html)を共有します。
|
・コンテナの作成(ホスト上) # docker run -it --name webserver-devel --volume=/home/admin/html:/mnt centos:7 /bin/bash Unable to find image 'centos:7' locally Trying to pull repository docker.io/library/centos ... 7: Pulling from library/centos f5079557f135: Pull complete 42c2aa730369: Pull complete 0e0217391d41: Pull complete 47d44cb6f252: Already exists library/centos:7: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security. Digest: sha256:8dcd2ec6183f3f4a94d4f9552ce76091624760edefcaa39a9e04441f9e2ad9f6 Status: Downloaded newer image for docker.io/centos:7 |
ホストボリュームのマウントについての解説
–volumeオプションを使って、ホストのボリューム(/home/admin/html)をコンテナの/mntにマウントしています。なおホストボリュームのマウントは、次のような用途にで利用できます。
- ホストからコンテナへのファイルの受け渡し
- コンテナ間でのファイル共有
- コンテナのデータの保存
コンテナで作成したファイルは、コンテナを削除すると削除されてしまいますが、マウントしてある領域のファイルを変更すれば、そのままホスト側に反映されます。
(2)WWWサーバの設定を行う
作成したコンテナにWWWサーバをインストールします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
・httpdのインストール(コンテナ上) [root@168f748a722c /]# yum install httpd Loaded plugins: fastestmirror, ovl base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/4): base/7/x86_64/group_gz | 155 kB 00:00:00 (2/4): extras/7/x86_64/primary_db | 101 kB 00:00:00 (3/4): base/7/x86_64/primary_db | 5.3 MB 00:00:00 (4/4): updates/7/x86_64/primary_db | 3.1 MB 00:00:06 Determining fastest mirrors * base: ftp.iij.ad.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-40.el7.centos will be installed --> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos for package: httpd-2.4.6-40.el7.centos.x86_64 --> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-40.el7.centos.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.4.8-3.el7 will be installed ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed ---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos will be installed ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================== Package Arch Version Repository Size ========================================================================================================================================== Installing: httpd x86_64 2.4.6-40.el7.centos base 2.7 M Installing for dependencies: apr x86_64 1.4.8-3.el7 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k centos-logos noarch 70.0.6-3.el7.centos base 21 M httpd-tools x86_64 2.4.6-40.el7.centos base 82 k mailcap noarch 2.1.41-2.el7 base 31 k Transaction Summary ========================================================================================================================================== Install 1 Package (+5 Dependent packages) Total download size: 24 M Installed size: 31 M Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/base/packages/apr-util-1.5.2-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for apr-util-1.5.2-6.el7.x86_64.rpm is not installed (1/6): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:00 (2/6): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:00 (3/6): httpd-2.4.6-40.el7.centos.x86_64.rpm | 2.7 MB 00:00:00 (4/6): httpd-tools-2.4.6-40.el7.centos.x86_64.rpm | 82 kB 00:00:00 (5/6): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:00 (6/6): centos-logos-70.0.6-3.el7.centos.noarch.rpm | 21 MB 00:00:02 ------------------------------------------------------------------------------------------------------------------------------------------ Total 7.7 MB/s | 24 MB 00:00:03 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@CentOS) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-3.el7.x86_64 1/6 Installing : apr-util-1.5.2-6.el7.x86_64 2/6 Installing : httpd-tools-2.4.6-40.el7.centos.x86_64 3/6 Installing : centos-logos-70.0.6-3.el7.centos.noarch 4/6 Installing : mailcap-2.1.41-2.el7.noarch 5/6 Installing : httpd-2.4.6-40.el7.centos.x86_64 6/6 Verifying : httpd-2.4.6-40.el7.centos.x86_64 1/6 Verifying : httpd-tools-2.4.6-40.el7.centos.x86_64 2/6 Verifying : apr-1.4.8-3.el7.x86_64 3/6 Verifying : mailcap-2.1.41-2.el7.noarch 4/6 Verifying : apr-util-1.5.2-6.el7.x86_64 5/6 Verifying : centos-logos-70.0.6-3.el7.centos.noarch 6/6 Installed: httpd.x86_64 0:2.4.6-40.el7.centos Dependency Installed: apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 centos-logos.noarch 0:70.0.6-3.el7.centos httpd-tools.x86_64 0:2.4.6-40.el7.centos mailcap.noarch 0:2.1.41-2.el7 Complete! |
必要があればWWWサーバの設定ファイルを編集し、設定を整えます。
|
設定ファイルの編集(コンテナ上) [root@168f748a722c /]# cp -a /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf_org [root@168f748a722c /]# vi /etc/httpd/conf/httpd.conf |
(3)WWWコンテンツを配置する
DockerコンテナにWWWコンテンツを配置します。/mntにマウントしたホストボリュームからファイルをコピーします。ただし、通常の状態のままではSELinuxの制限でファイルにアクセスすることができません。
(これはコンテナがホストに悪影響を及ぼすことを防いでいるとも言えます)。そのため、まずホスト側でコンテンツのテキストを一時的に変更します。