- 追加された行はこの色です。
- 削除された行はこの色です。
#contents
** 概要 [#f45b9b1a]
- Docker とはなにか?
-- 軽量な仮想化環境
-- Build once, run anywhere
-- 公式サイト: https://www.docker.com
- 事前知識
-- Unix
-- Vim
-- Vagrant
- 環境
-- Ubuntu (Vagrant) <- Docker
** Ubuntu ローカル環境構築 [#xe27c862]
- Ubuntu 14.04 (64bit) の box を追加
$ vagrant box add trusty64 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box
- Vagrant 用ディレクトリ作成
$ mkdir Docker
$ cd Docker
- Vagrant の初期化
$ vagrant init trusty64
- ネットワークアダプター設定
$ vi Vagrantfile
--------------------
config.vm.network "private_network", ip: "192.168.55.44"
--------------------
- Vagrant 起動
$ vagrant up
- SSH 接続確認
$ vagrant ssh
** Dockerインストール [#f97e9d85]
- インストールガイド (Docker CE for Ubuntu)
https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#uninstall-old-versions
- apt-get update
$ sudo apt-get update
- 推奨パッケージインストール
$ sudo apt-get install \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual
- 必要パッケージインストール
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
- Docker 公式 GPG キー追加
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- GPG キーのフィンガープリント確認
$ sudo apt-key fingerprint 0EBFCD88
/etc/apt/trusted.gpg
pub 4096R/0EBFCD88 2017-02-22
Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid Docker Release (CE deb) <docker@docker.com>
sub 4096R/F273FCD8 2017-02-22
- Dcoker 公式リポジトリ追加
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
- 依存パッケージインストール
$ sudo apt-get update
- Docker CE インストール
$ sudo apt-get install docker-ce
- Docker CE バージョン確認
$ sudo docker --version
Docker version 17.06.1-ce, build 874a737
- インストール確認
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
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 ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
** Docker コマンドと基本コンポーネント [#f5510293]
- Docker installed OS ... Docker がインストールされた OS
- Image ... "docker pull" で OS 上に取得
- Container .... "docker pull" で取得した Image を "docker run" で実行すると起動
- Image ... Container 上で "docker commit" を実行して再度 Image を作成可能
- Docker Hub
-- 旧 Docker Index
-- Docker が運営する Image の管理サイト
- URL: https://hub.docker.com
-- "docker commit" で作成した Image を "docker push" で Docker Index にプッシュ可能
** Image を操作 [#hdee39e6]
- Docker Hub 上の Image を "docker search" で CLI でも検索可能
$ sudo docker search centos | more
- Docker Hub から "docker pull" で centos の Image を取得
$ sudo docker pull centos
- Image 一覧を表示 &color(red){※}; IMAGE ID が Image の実態のため ID が同じであれば TAG が異なっても実態は同じ
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 328edcd84f1b 3 weeks ago 193MB
hello-world latest 1815c82652c0 2 months ago 1.84kB
- Image の詳細表示 (REPOSITORY:TAG 指定) &color(red){※}; TAG 名を省略すると自動的に "latest" となる
$ sudo docker inspect centos:latest
- Image の詳細表示 (IMAGE ID を前方一致で指定)
$ sudo docker inspect 328ed
- Image の削除
$ sudo docker rmi 328ed
** Container を操作してみよう [#qb3e39c6]
- Container 起動
$ sudo docker run centos echo "hello world"
- 実行中のコンテナ一覧
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
354a27ea31e1 centos "echo 'hello world'" 58 seconds ago Exited (0) 58 seconds ago dazzling_feynman
450df998fddf centos "echo 'hello world'" About a minute ago Exited (0) About a minute ago quizzical_lalande
eef1eb757c2e hello-world "/hello" 37 minutes ago Exited (0) 37 minutes ago sharp_agnesi
- 実行中のコンテナ一覧 (最後に実行した5つの Container を表示)
$ sudo docker ps -a -n=5
- コンテナ削除 (CONTAINER ID を前方一致で指定)
$ sudo docker rm eef
$ sudo docker ps -a
** 実行中の Container を操作 [#i40db5c3]
- centos 上でバックラグラウドで free コマンドを実行
$ sudo docker run -d centos free -s 3
1fb08aae68483f2253ed8cb84f329b48d95edaba5e99ac490dab4c869b94b74c
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fb08aae6848 centos "free -s 3" About a minute ago Up About a minute gifted_murdock
- ログ確認
$ sudo docker logs 1fb
- 実行中タスクをフォアグラウンドに表示 (Ctrl + c で抜ける)
$ sudo docker attach --sig-proxy=false 1fb
- 実行中タスクを停止
$ sudo docker kill 1fb
- 停止タスクを再開
$ sudo docker start 1fb
** Container から Image を作成 [#ucaa13c6]
- Container にログイン (インタラクティブモードで docker run 実行)
$ sudo docker run -i -t centos /bin/bash
- hello.txt を追加
[root@24b42c9b3ac1 /]# touch hello.txt
[root@24b42c9b3ac1 /]# exit
- Container 確認
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24b42c9b3ac1 centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago nervous_ardinghelli
- Image 作成 (CONTAINER ID の次に Image の名前指定)
$ sudo docker commit 24b yuji/hello
- Image 確認
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yuji/hello latest b378469ce2fc 24 seconds ago 193MB
- 作成した Image から Container を起動
$ sudo docker run -i -t yuji/hello /bin/bash
# ls hello.txt
hello.txt
** Dockerfile の利用 [#he596158]
- "docker build" コマンドで Image -> Container 起動 -> Image 再作成の一連の作業を自動化可能
- Dockerifle に処理を記述
$ vi Dockerfile
$ cat Dockerfile
FROM centos
MAINTAINER Yuji Shimojo <y.shimojo@example.com>
# RUN: build する時に実行
RUN echo "now building..."
# CMD: run する時に実行
# CMD echo "now running..."
CMD ["echo", "now running..."]
- Dcoker ビルド
$ sudo docker build -t yuji/echo .
- Container 起動確認
$ sudo docker run yuji/echo
now running...
** Web サーバー起動 [#ub6d5a10]
- Dockerfile 編集 (CentOS 6 のイメージを利用)
$ vi Dockerfile
FROM imagine10255/centos6-lnmp-php56
MAINTAINER Yuji Shimojo <y.shimojo@example.com>
RUN yum install -y httpd
ADD ./index.html /var/www/html/
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
- index.html 作成
$ vi index.html
<html>
helllo from Docker!
</html>
- Docker ビルド
$ sudo docker build -t yujishimojo/httpd .
- Docker 起動 (サーバー側の tcp/80 を Container 側の tcp/8080 にリダイレクト)
$ sudo docker run -p 8080:80 -d yujishimojo/httpd
- ブラウザ経由で http://192.168.55.44:8080/ へアクセスし確認
** Image をプッシュ &color(red){※}; Docker Hub アカウントの username と Image の TAG 名 prefix を一致させる必要あり [#j60ff8d7]
** Image をプッシュ [#y85a56b5]
- Docker Hub アカウント作成
- ターミナルからログイン
$ sudo docker login
Username:
Password:
- Image を push
- Image を push &color(red){※}; Docker Hub アカウントの username と Image の TAG 名 prefix を一致させる必要あり
$ sudo docker push yujishimojo/httpd
- Docker Hub 上で Image が push されていることを確認
- docker pull することで他環境にも同じ Image を配布することが可能