[Docker] Docker for Linux 入門

概要を知りたく、手を動かしてみる。

[markdown]

WEB+DB PRESS Vol.86
WEB+DB PRESS Vol.86

posted with amazlet at 16.06.29
結城 洋志 沖元 謙治 足永 拓郎 林 健太郎 大竹 智也 内田 誠悟 伊藤 直也 中山 裕司 hiroki.o 泉水 翔吾 佐藤 太一 高橋 俊幸 西尾 泰和 舘野 祐一 中島 聡 橋本 翔 はまちや2 竹原 麻植 泰輔
技術評論社
売り上げランキング: 237,581

ドットインストール様のお世話になる。

> * [Docker入門 (全11回) – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_docker)

以下の段取りになっている。

1. VirtualBox(Vagrant) 上に Ubuntu をインストール。
2. Ubuntu 上に Docker をインストール。
3. Docker 上でコンテナを実行する。

## ドキュメント

> * [Docker Docs](https://docs.docker.com/)
> * [Docker ドキュメント日本語化プロジェクト — Docker-docs-ja 1.12.RC ドキュメント](http://docs.docker.jp/index.html#)

## Vagrant のインストール

VirtualBox もインストール済。

“`prettyprinted
% vagrant -v
Vagrant 1.8.4
% vboxmanage –version
5.0.22r108108
“`

> * [Vagrant by HashiCorp](https://www.vagrantup.com/)

ローカルの box をチェック。

“`prettyprinted
% vagrant box outdated –global
* ‘ubuntu/trusty64’ (v20160621.0.0) is up to date
* ‘miya0001/vccw’ (v2.19.0) is up to date
* ‘bento/centos-6.7’ (v2.2.7) is up to date
“`

ubuntu/trusty64 を利用する。
Vagrantfile を生成する。

“`prettyprinted
% vagrant init ubuntu/trusty64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
“`

Vagrantfile を編集する。

“`:Vagrantfile
config.vm.network “private_network”, ip: “192.168.55.44”
“`

`vagrant up` で立ち上げ。

“`prettyprinted
% vagrant up
“`

## Docker のインストール

“`prettyprinted
$ docker –version
Docker version 1.11.2, build b9f10c9
“`

> * [Docker – Build, Ship, and Run Any App, Anywhere](https://www.docker.com/)

`vagrant ssh` で guest OS へ接続する。

“`prettyprinted
% vagrant ssh
“`

公式ドキュメントを確認しながらインストールを進める。

> * [Installation on Ubuntu](https://docs.docker.com/engine/installation/linux/ubuntulinux/)

“`prettyprinted
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv –keyserver hkp://p80.pool.sks-keyservers.net:80 –recv-keys 58118E89F3A912897C070ADBF76221572C52609D
“`

ファイルを作成し、設定を追記する。

“`prettyprinted
$ sudo touch /etc/apt/sources.list.d/docker.list
$ sudo vim /etc/apt/sources.list.d/docker.list
“`

“`text:/etc/apt/sources.list.d/docker.list
deb https://apt.dockerproject.org/repo ubuntu-trusty main
“`

さらに進める。

“`prettyprinted
$ sudo apt-get update
$ sudo apt-get purge lxc-docker
$ apt-cache policy docker-engine
“`

さらに進める。

“`prettyprinted
$ sudo apt-get update
$ sudo apt-get install linux-image-extra-$(uname -r)
“`

さらに進める。

“`prettyprinted
$ sudo apt-get update
$ sudo apt-get install docker-engine
“`

これでインストール完了した模様。

“`prettyprinted
$ docker –version
Docker version 1.11.2, build b9f10c9
$ which docker
/usr/bin/docker
“`

動作を確認する。

“`prettyprinted
$ sudo service docker start
$ sudo docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
a9d36faac0fe: Pull complete
Digest: sha256:e52be8ffeeb1f374f440893189cd32f44cb166650e7ab185fa7735b7dc48d619
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
“`

## Docker コマンド

Docker コマンドについて学ぶ。

### Image

`search` で image を検索する。

“`prettyprinted
$ sudo docker search centos | more
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2381 [OK]
ansible/centos7-ansible Ansible on Centos7 78 [OK]
jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8… 25 [OK]
jdeathe/centos-ssh-apache-php CentOS-6 6.7 x86_64 / Apache / PHP / PHP M… 18 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif… 12 [OK]
:
“`

`pull` でイメージを取得する。

“`prettyprinted
$ sudo docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a3ed95caeb02: Pull complete
da71393503ec: Pull complete
Digest: sha256:1a62cd7c773dd5c6cf08e2e28596f6fcc99bd97e38c9b324163e0da90ed27562
Status: Downloaded newer image for centos:latest
“`

`images` で一覧する。

“`prettyprinted
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 693bce725149 2 weeks ago 967 B
centos latest 904d6c400333 3 weeks ago 196.8 MB
$ sudo docker inspect centos:latest
“`

`rmi` でイメージを削除する。

### Container

centos のイメージでコンテナを起動。
`echo` してみる。

“`prettyprinted
$ sudo docker run centos echo “hello world”
“`

`ps` で実行中のコンテナを一覧。
`-a` オプションで過去に実行したコンテナを一覧する。
`-n=5` オプションで最新5件まで表示する。

“`prettyprinted
$ sudo docker ps -a -n=5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6b304e76677 centos “echo ‘hello world'” 29 seconds ago Exited (0) 29 seconds ago pensive_babbage
c5e614800beb hello-world “/hello” 21 hours ago Exited (0) 21 hours ago boring_ride
“`

`rm` でコンテナを削除する。

“`prettyprinted
$ sudo docker rm c6b3
c6b3
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5e614800beb hello-world “/hello” 21 hours ago Exited (0) 21 hours ago boring_ride
“`

`run` でコンテナを実行する。
`-d` オプション(detach)でバックグラウンド実行する。コンテナ ID が返ってくる。

“`prettyprinted
$ sudo docker run -d centos free -s 3
e1adc106db3e697c4e35b842b653e07fc74af385a2b27641434019997d875c14
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1adc106db3e centos “free -s 3” About a minute ago Up About a minute insane_raman
“`

`logs` でログを表示する。

“`prettyprinted
$ sudo docker logs e1adc106db3e6
total used free shared buff/cache available
Mem: 501716 110020 233752 784 157944 371543
Swap: 0 0 0
:
“`

`attach` でフォアグラウンドに持ってこれる。
`–sig-proxy=false` は `ctl-c` で抜けるために必要なオプション。

“`prettyprinted
$ sudo docker attach –sig-proxy=false e1adc106db3e6
“`

`stop` もしくは `kill` で実行中のコンテナを停止する。

“`prettyprinted
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1adc106db3e centos “free -s 3” 6 minutes ago Up 6 minutes insane_raman
$ sudo docker kill e1adc106db3e
e1adc106db3e
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
“`

`start` でコンテナの実行を再開できる。

“`prettyprinted
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1adc106db3e centos “free -s 3” 8 minutes ago Exited (137) 2 minutes ago insane_raman
$ sudo docker start e1adc106db3e
e1adc106db3e
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1adc106db3e centos “free -s 3” 9 minutes ago Up 5 seconds insane_raman
“`

### Container の中に入る

`run` にオプションを付与する。
`-i` オプションでインタラクティブ、`-t` オプションでターミナルの意。
`/bin/bash` でシェルを立ち上げる。

“`prettyprinted
$ sudo docker run -it centos bash
[root@3c2dc30d129a /]#
“`

### Container から Image を作成する

`commit` でイメージを作成する。
`test/hello` ユーザー名/コンテナ名で命名する慣習。

“`prettyprinted
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c2dc30d129a centos “/bin/bash” About a minute ago Exited (0) 37 seconds ago jovial_goldstine
$ sudo docker commit 3c2dc30d129a test/hello
sha256:ded330c5e1b51a221f66ea4cc4d6e8d29622a38fa0f7d130f272597aaeb499a2
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/hello latest ded330c5e1b5 39 seconds ago 196.8 MB
hello-world latest 693bce725149 3 weeks ago 967 B
centos latest 904d6c400333 3 weeks ago 196.8 MB
“`

## Dockerfile

一連の作業を Dockerfile スクリプトで書き、自動化することができる。

“`text:Dockerfile
FROM centos
MAINTAINER deadwood
# RUN: build するときに実行される
RUN yum install -y httpd
# ADD: host に存在するファイルを取り込む
ADD ./index.html /var/www/html/
# EXPOSE: Port 80 を開ける
EXPOSE 80
# CMD: RUN するときに実行される
CMD [“/usr/sbin/httpd”, “-D”, “FOREGROUND”]
“`

`build` で実行する。
`-t` でコンテナに名前を付ける。
`.` でカレントディレクトリの Dockerfile を利用する。

“`prettyprinted
$ sudo docker build -t test/httpd .
“`

確認する。

“`prettyprinted
$ sudo docker images
“`

### Error

が、下記のエラーが発生する。
`yum install -y httpd` で下記のエラーが発生する。

“`prettyprinted
Error unpacking rpm package httpd-2.4.6-40.el7.centos.1.x86_64
error: unpacking of archive failed on file /usr/sbin/suexec: cpio: cap_set_file
error: httpd-2.4.6-40.el7.centos.1.x86_64: install failed
“`

こちらに情報が。

> * [Ubuntu上のdocker build で error: unpacking of archive failed on file /usr/sbin/suexec: cpio: cap_set_file – Qiita](http://qiita.com/kaakaa_hoe/items/602f0cef1fa50ebdcbfc)

### nginx を起動する

本質ではないので nginx をここからインストールしてお茶を濁す。

> * [https://hub.docker.com/explore/](https://hub.docker.com/explore/)

“`prettyprinted
$ sudo docker run -d -p 8080:80 nginx
“`

> * [初心者による初心者のための Docker ~ Nginx を動かす ~ – Qiita](http://qiita.com/kasaharu/items/d4654193d75c67d65226)

`192.168.55.44:8080` をブラウザで表示する。

## Docker Index

> * [Docker の Container Image を Docker Index で管理する方法 – Qiita](http://qiita.com/yu-iskw/items/82c85ab36f520ec721e1)
[/markdown]