読者です 読者をやめる 読者になる 読者になる

ログってなんぼ

日々のメモです

Docker Machine公式サイトをナナメ読み写経しながら軽く体験

docker

Docker Machine

Docker Machine 0.3.0リリースされてるってことで。こちらの公式ページをナナメ読みしながら気になる部分だけ写経して動きを確認したりしてみた。手を動かしたほうが実感が沸くのでメモしておきますが他の人にはナナメ読み過ぎて役には立たないかも。個人的に参考になったURLを最後に貼っておきます。

Amazon EC2・Azure・DigitalOcean・OpenStack・GoogleComputeEngine・SoftLayerなどなど一般的なクラウドサービスに広く対応しているのもいいっすね

Install

Win/OSX/Linux用があるので対応Binaryをインスコします

curl -L https://github.com/docker/machine/releases/download/v0.3.0/docker-machine_darwin-amd64 > /usr/local/bin/docker-machine
chmod +x /usr/local/bin/docker-machine

dockerコマンドもInstall

$ curl -L https://get.docker.com/builds/Darwin/x86_64/docker-latest > /usr/local/bin/docker
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 7361k  100 7361k    0     0  1856k      0  0:00:03  0:00:03 --:--:-- 1856k

$ chmod +x /usr/local/bin/docker

動作確認

$ docker-machine -v
docker-machine version 0.3.0 (0a251fe)

VMを作ってみる

$ docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM

virtualboxでローカルVM作成

$ docker-machine create --driver virtualbox dev
Creating CA: /Users/okisanjp/.docker/machine/certs/ca.pem
Creating client certificate: /Users/okisanjp/.docker/machine/certs/cert.pem
Image cache does not exist, creating it at /Users/okisanjp/.docker/machine/cache...
No default boot2docker iso found locally, downloading the latest release...
Downloading https://github.com/boot2docker/boot2docker/releases/download/v1.7.0/boot2docker.iso to /Users/okisanjp/.docker/machine/cache/boot2docker.iso...
Creating VirtualBox VM...
Creating SSH key...
Starting VirtualBox VM...
Starting VM...
To see how to connect Docker to this machine, run: docker-machine env dev

できたかな

$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM
dev             virtualbox   Running   tcp://192.168.99.100:2376   

環境変数の確認

$ docker-machine env dev
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/okisanjp/.docker/machine/machines/dev"
export DOCKER_MACHINE_NAME="dev"
# Run this command to configure your shell: 
# eval "$(docker-machine env dev)"

アドバイスの通りevalコマンドでdevをアクティブにする

$ eval "$(docker-machine env dev)"

$ echo $DOCKER_MACHINE_NAME
dev

nginx起動

$ docker run -d -p 8000:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from nginx
64e5325c0d9d: Pull complete 
bf84c1d84a8f: Pull complete 
aaa04ccb1cff: Pull complete 
c25a494fd450: Pull complete 
2b4392756878: Pull complete 
a58c6ba7d446: Pull complete 
6391a6648407: Pull complete 
0b6365661275: Pull complete 
5c1f0ec6cd7b: Pull complete 
36f0b0306c21: Pull complete 
107c338c1d31: Pull complete 
319d2015d149: Already exists 
nginx: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:0952acc92b56f936b5b5b93a6b7abb9205ec7ec64b920e113a6d9dae50f6f49d
Status: Downloaded newer image for nginx:latest
285203cabc9dd18ad8549a6f09a62c36117f3c352525545e18da209813e67dcc

スクリーンショット 2015 07 02 12 57 36

止めてみる

$ docker-machine stop dev
exit status 1
$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL   SWARM
dev             virtualbox   Stopped

もう一度起動

$ docker-machine start dev
Starting VM...

sshしてみる

$ docker-machine ssh dev
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.7.0, build master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015
Docker version 1.7.0, build 0baf609

docker@dev:~$ ls -l /
total 4
drwxr-xr-x    1 docker   staff          204 Oct 17  2014 Users/
drwxr-xr-x    2 root     root          1460 Jul  2 04:00 bin/
drwxrwxr-x   14 root     staff         4400 Jul  2 04:00 dev/
drwxr-xr-x   12 root     root          1000 Jul  2 04:00 etc/
drwxrwxr-x    3 root     staff           60 Jul  2 04:00 home/
-rwxr-xr-x    1 root     root           496 Jun 18 18:31 init
drwxr-xr-x    5 root     root           860 Jul  2 04:00 lib/
lrwxrwxrwx    1 root     root             3 Jul  2 04:00 lib64 -> lib/
lrwxrwxrwx    1 root     root            11 Jul  2 04:00 linuxrc -> bin/busybox
drwxr-xr-x    4 root     root            80 Jul  2 04:00 mnt/
drwxrwsr-x    2 root     staff          180 Jul  2 04:00 opt/
dr-xr-xr-x  161 root     root             0 Jul  2 04:00 proc/
drwxrwxr-x    2 root     staff           80 Jul  2 04:00 root/
drwxrwxr-x    4 root     staff           80 Jul  2 04:00 run/
drwxrwxr-x    2 root     root          1380 Jul  2 04:00 sbin/
dr-xr-xr-x   13 root     root             0 Jul  2 04:00 sys/
lrwxrwxrwx    1 root     root            13 Jul  2 04:00 tmp -> /mnt/sda1/tmp/
drwxr-xr-x    7 root     root           140 Jul  2 04:00 usr/
drwxrwxr-x    9 root     staff          200 Jul  2 04:00 var/

$ exit

VMSSHしてコマンド実行

$ docker-machine ssh dev free
             total         used         free       shared      buffers
Mem:       1020160       166008       854152       101580          904
-/+ buffers:             165104       855056
Swap:       199316            0       199316

引数のあるコマンドは

$ docker-machine ssh dev -- df -h

Filesystem                Size      Used Available Use% Mounted on
tmpfs                   896.6M     99.2M    797.4M  11% /
tmpfs                   498.1M         0    498.1M   0% /dev/shm
/dev/sda1                18.2G    209.0M     17.0G   1% /mnt/sda1
cgroup                  498.1M         0    498.1M   0% /sys/fs/cgroup
none                    232.6G    145.5G     87.1G  63% /Users
/dev/sda1                18.2G    209.0M     17.0G   1% /mnt/sda1/var/lib/docker/aufs

$ docker-machine ssh dev "df -h"
Filesystem                Size      Used Available Use% Mounted on
tmpfs                   896.6M     99.2M    797.4M  11% /
tmpfs                   498.1M         0    498.1M   0% /dev/shm
/dev/sda1                18.2G    209.0M     17.0G   1% /mnt/sda1
cgroup                  498.1M         0    498.1M   0% /sys/fs/cgroup
none                    232.6G    145.5G     87.1G  63% /Users
/dev/sda1                18.2G    209.0M     17.0G   1% /mnt/sda1/var/lib/docker/aufs

どっちでもいけるぽ

scpしてみる

ローカルマシンにファイルがないことを確認

$ cat foo.txt
cat: foo.txt: No such file or directory

VMにファイルを作ってscpでマシンに落としてみる

$ docker-machine ssh dev pwd
/home/docker

$ docker-machine ssh dev 'echo A file created remotely! >foo.txt'
$ docker-machine scp dev:/home/docker/foo.txt .
foo.txt                                                                                                                                      100%   25     0.0KB/s   00:00    
$ cat foo.txt
A file created remotely!

参考になったURL

Docker Machine リファレンス - Qiita

Docker Machine 0.3 の新機能、generic ドライバと scp を試す | Pocketstudio.jp log3

Docker MachineでAmazon EC2をコントロールする | Developers.IO

EC2でのコンテナ作成も試して見たいので近々やろう