概要 †
- 環境
- Ubuntu (Vagrant) <- Docker
- OS X 10.11 El Capitan
- Vagrant 1.9.7
- VirtualBox 5.1.26
- Docker for Mac (Version 17.06.1-ce-mac24)
Ubuntu ローカル環境構築 †
- Ubuntu 14.04 (64bit) の box を追加
$ vagrant box add trusty64 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box
$ mkdir Docker
$ cd Docker
$ vagrant init trusty64
$ vi Vagrantfile
--------------------
config.vm.network "private_network", ip: "192.168.55.44"
--------------------
$ vagrant up
$ vagrant ssh
Dockerインストール †
$ 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
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ 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
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo apt-get update
$ sudo apt-get install 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 コマンドと基本コンポーネント †
- 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 を操作 †
- Docker Hub 上の Image を "docker search" で CLI でも検索可能
$ sudo docker search centos | more
- Docker Hub から "docker pull" で centos の Image を取得
$ sudo docker pull centos
- Image 一覧を表示 ※ 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 指定) ※ TAG 名を省略すると自動的に "latest" となる
$ sudo docker inspect centos:latest
- Image の詳細表示 (IMAGE ID を前方一致で指定)
$ sudo docker inspect 328ed
$ sudo docker rmi 328ed
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 を操作 †
- 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 を作成 †
- Container にログイン (インタラクティブモードで docker run 実行)
$ sudo docker run -i -t centos /bin/bash
[root@24b42c9b3ac1 /]# touch hello.txt
[root@24b42c9b3ac1 /]# exit
$ 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
$ 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 の利用 †
- "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..."]
$ sudo docker build -t yuji/echo .
$ sudo docker run yuji/echo
now running...
Web サーバー起動 †
- 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"]
$ vi index.html
<html>
helllo from Docker!
</html>
$ sudo docker build -t yujishimojo/httpd .
- Docker 起動 (サーバー側の tcp/80 を Container 側の tcp/8080 にリダイレクト)
$ sudo docker run -p 8080:80 -d yujishimojo/httpd
Image をプッシュ †
- Docker Hub アカウント作成
- ターミナルからログイン
$ sudo docker login
Username:
Password:
- Image を push ※ Docker Hub アカウントの username と Image の TAG 名 prefix を一致させる必要あり
$ sudo docker push yujishimojo/httpd
- Docker Hub 上で Image が push されていることを確認
- docker pull することで他環境にも同じ Image を配布することが可能
Docker for Mac †
Amazon Linux Image †
$ docker search amazonlinux | more
$ docker pull amazonlinux
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
amazonlinux latest 766ebb052d4f 4 months ago 162MB
- Container にインタラクティブモードでログイン
$ docker run -i -t amazonlinux /bin/bash
# cat /etc/system-release
Amazon Linux AMI release 2017.03
- Yum リポジトリ一覧表示 (enabled のみ抜粋)
# sudo yum repolist all
repo id repo name status
amzn-main/latest amzn-main-Base enabled: 5668
amzn-updates/latest amzn-updates-Base enabled: 776
# yum -y update
# date
Mon Aug 28 03:39:31 UTC 2017
- SELinux ステータス確認 ※ Amazon Linux ではデフォルト Disabled になっているが公式 Docker Image ではコマンドが存在しない
# getenforce
bash: getenforce: command not found
- iptables ステータス確認 ※ Amazon Linux ではデフォルト OFF になっているが公式 Docker Image ではコマンドが存在しない
# etc/init.d/iptables status
bash: etc/init.d/iptables: No such file or directory
Heroku Ruby Image †
$ docker search heroku | more
$ docker pull heroku/ruby
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
heroku/ruby latest dc7ebd538ae6 4 months ago 1.47GB
- Container にインタラクティブモードでログイン
$ docker run -i -t heroku/ruby /bin/bash
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
# git --version
git version 1.9.1
- ruby / gem バージョン確認 ※ rbenv / rails はインストールされていない
# ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
# gem --version
2.4.5.1
# gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.5.1
- RUBY VERSION: 2.2.3 (2015-08-18 patchlevel 173) [x86_64-linux]
- INSTALLATION DIRECTORY: /app/heroku/ruby/bundle/ruby/2.2.0
- RUBY EXECUTABLE: /app/heroku/ruby/ruby-2.2.3/bin/ruby
- EXECUTABLE DIRECTORY: /app/heroku/ruby/bundle/ruby/2.2.0/bin
- SPEC CACHE DIRECTORY: /root/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /app/vendor/ruby-2.2.3/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /app/heroku/ruby/bundle/ruby/2.2.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /app/user/bin
- /app/heroku/ruby/bundle/ruby/2.2.0/bin
- /app/heroku/ruby/node-0.12.7/bin
- /app/heroku/ruby/ruby-2.2.3/bin
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
- /sbin
- /bin
# psql --version
psql (PostgreSQL) 9.5.5
# node -v
v0.12.7
# python --version
Python 2.7.6
# java -version
java version "1.7.0_121"
OpenJDK Runtime Environment (IcedTea 2.6.8) (7u121-2.6.8-1ubuntu0.14.04.1)
OpenJDK 64-Bit Server VM (build 24.121-b00, mixed mode)
Heroku Cedar Image †
$ docker search heroku | more
$ docker pull heroku/cedar
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
heroku/cedar latest cfcf6ab6adde 3 weeks ago 1.27GB
- Container にインタラクティブモードでログイン
$ docker run -i -t heroku/cedar /bin/bash
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
# git --version
git version 1.9.1
- ruby / gem バージョン確認 ※ rbenv / rails はインストールされていない
# ruby --version
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
# gem --version
1.8.23
# gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.23
- RUBY VERSION: 1.9.3 (2013-11-22 patchlevel 484) [x86_64-linux]
- INSTALLATION DIRECTORY: /var/lib/gems/1.9.1
- RUBY EXECUTABLE: /usr/bin/ruby1.9.1
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /var/lib/gems/1.9.1
- /root/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
# psql --version
psql (PostgreSQL) 9.6.4
# python --version
Python 2.7.6
# java -version
java version "1.7.0_131"
OpenJDK Runtime Environment (IcedTea 2.6.9) (7u131-2.6.9-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode)
- ※ node.js / php はインストールされていない
Google Images †
$ docker search google | more
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
google/cadvisor Analyzes resource usage and performance ch... 243
google/cloud-sdk Google Cloud SDKbundle with all components... 129 [OK]
google/golang 99 [OK]
google/nodejs-runtime 75 [OK]
google/nodejs 73 [OK]
google/debian 52 [OK]
google/golang-runtime 49 [OK]
google/python Please use gcr.io/google-appengine/python ... 36 [OK]
google/docker-registry Docker Registry w/ Google Cloud Storage dr... 28
google/python-runtime Please use gcr.io/google-appengine/python ... 19 [OK]
google/mysql MySQL server for Google Compute Engine 18 [OK]
google/nodejs-hello 15 [OK]
google/golang-hello 8 [OK]
google/ruby 5 [OK]
google/pause 3
google/ruby-hello 2 [OK]
google/guestbook-python-redis A simple guestbook example written in Pyth... 0