DockerをMAC Book Air(OS v10.9 mavericks)にインストールしてみた

いま流行りのDockerをMAC OSXにインストールした時のメモです。version0.9を入れていましたが、気づいたら1.4.1まで進んでいたので入れ替えに合わせて再構築し、ついでに簡単な使い方も書いておきます。

f:id:komeiy:20141223140953j:plain

1. DockerをMACにインストールする

まず公式ページはこちらです。Dockerってなに?は省略して早速インストールです。様々なLunx OSやそれらが使えるクラウドに対応しています。

Dockerが対応するMAC OSXのバージョン

インストール手順

MAC上のDockerなので通称Docker on MACなのですが、Light WeightなVM上でDockerを動作させ、MAC OSXにインストールしたDocker Clientから操作します。

Boot2Dockerを使うと簡単に環境が構築できるということで、今回はそちらを使用して環境構築を行います。

①ダウンロード

Boot2Dockerはgithubに上がっていますので、まずはダウンロードします。

https://github.com/boot2docker/osx-installer/releases

pkgの場所は以下のURLです。

https://github.com/boot2docker/osx-installer/releases/download/v1.4.1/Boot2Docker-1.4.1.pkg

②pkg起動

ダウンロードしたpkgファイルを起動すると、以下の画面が立ち上がりますので「続ける」を選択しインストールを進めてください。

③boot2docker起動

Applicationsからboot2dockerを起動します。boot2docker statusコマンドでもrunningが表示されております。

komei:boot2docker komei$ cd ~/.boot2docker/
komei:.boot2docker komei$ ls
boot2docker-vm.sock boot2docker.iso
komei:.boot2docker komei$ boot2docker init
Virtual machine boot2docker-vm already exists
komei:.boot2docker komei$ 
komei:.boot2docker komei$ 
komei:.boot2docker komei$ boot2docker start
Waiting for VM and Docker daemon to start...
.o
Started.

To connect the Docker client to the Docker daemon, please set:
    unset DOCKER_CERT_PATH
    unset DOCKER_TLS_VERIFY
    export DOCKER_HOST=tcp://192.168.59.103:2375

komei:.boot2docker komei$ $(boot2docker shellinit)

komei:.boot2docker komei$ boot2docker status
running

2. Boot2Dockerの使い方・動作確認

Boot2Dockerの動作確認をしておきます。

- version確認

komei:.boot2docker komei$ boot2docker version
Boot2Docker-cli version: v1.4.1
Git commit: 43241cb

- 起動確認

komei:.boot2docker komei$ boot2docker status
running

Boot2Docker自体は起動していることを確認できました。次はDockerコマンドの使い方を確認しつつ動作確認します。

エラーが出てしまう方は以下も参考にしてください。

Docker on MACで「TLS-enabled daemon without TLS?」が出た時の対処法 - ぽぽぽぽーんのネットワークとOSS

3. Dockerコマンドの使い方・動作確認

以下にもデモシナリオに沿った簡単な使い方を記載しております。 ネットワークプログラマビリティ勉強会#2@六本木に参加してきた | ぽぽぽぽーんのNWとOSS

dockerの動作確認

- version確認

komei:.boot2docker komei$ docker -v
Docker version 1.4.1, build 5bc2ff8

komei:.boot2docker komei$ docker version
Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.3.3
Git commit (client): 5bc2ff8
OS/Arch (client): darwin/amd64
Server version: 1.4.1
Server API version: 1.16
Go version (server): go1.3.3
Git commit (server): 5bc2ff8

- hello-world

hello-worldというコンテナをダウンロードして動作させてみます。imageは手元にないのでgit pullが動作してその後メッセージが出ています。

komei:.boot2docker komei$ docker run hello-world
Unable to find image 'hello-world:latest' locally
hello-world:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete 
7fa0dcdc88de: Pull complete 
ef872312fe1b: Pull complete 
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.
    (Assuming it was not already locally available.)
 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

For more examples and ideas, visit:
 http://docs.docker.com/userguide/

Dockerコンテナの使い方を確認する

- Dockerコンテナ確認

docker psで起動しているコンテナを表示します。-aオプションを付けると停止しているコンテナも含めて表示します。CONTAINER IDは後々使用するため、docker psコマンドで確認できることを覚えておいてください。

komei:.boot2docker komei$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

komei:.boot2docker komei$ docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                     PORTS               NAMES
e3449df946ef        hello-world:latest   "/hello"            7 minutes ago       Exited (0) 7 minutes ago                       thirsty_poincare     

- Dockerコンテナを検索する

Nginxのコンテナを検索してみます。OFFICIAL欄に[OK]が付いているコンテナを使います。GUIからも検索することが可能です。

komei:.boot2docker komei$ docker search nginx
NAME                                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                          Official build of Nginx.                        449       [OK]       
jwilder/nginx-proxy                            Automated Nginx reverse proxy for docker c...   96                   [OK]
dockerfile/nginx                               Trusted automated Nginx (http://nginx.org/...   83                   [OK]
dockerfiles/django-uwsgi-nginx                 Dockerfile and configuration files to buil...   40                   [OK]
clue/ttrss                                     The Tiny Tiny RSS feed reader allows you t...   24                   [OK]

- DockerのNginx公式コンテナを取ってくる

komei:.boot2docker komei$ docker pull nginx

- Dockerのローカルコンテナ確認

docker imageコマンドでローカルで保有している一覧を表示します。

komei:.boot2docker komei$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nginx               latest              e46b3488b010        2 weeks ago         91.88 MB
hello-world         latest              ef872312fe1b        11 weeks ago        910 B

- dockerコンテナをCLIから操作する

先ほどhello-worldでも使用しましたが、
docker run [image name] [COMMAND] を使う事でCLIからdockerコンテナで任意のコマンドを実行することが可能です。

komei:.boot2docker komei$ docker run nginx echo "hello docker"
hello docker

komei:.boot2docker komei$ docker run nginx cat /etc/issue
Debian GNU/Linux 7 \n \l

- dockerコンテナにログインする

komei:.boot2docker komei$ docker run -t -i nginx /bin/bash
root@6a336cf80564:/# 
root@6a336cf80564:/# 
root@6a336cf80564:/# 
root@6a336cf80564:/# ls
bin   dev  home  lib64  mnt  proc  run   selinux  sys  usr
boot  etc  lib   media  opt  root  sbin  srv      tmp  var
root@6a336cf80564:/# 
root@6a336cf80564:/# cat /etc/nginx/
conf.d/         koi-win         scgi_params     
fastcgi_params  mime.types      uwsgi_params    
koi-utf         nginx.conf      win-utf         
root@6a336cf80564:/# cat /etc/nginx/conf.d/
default.conf      example_ssl.conf  
root@6a336cf80564:/# cat /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

root@6a336cf80564:/# exit
exit

- docker startで停止したコンテナを起動する

komei:.boot2docker komei$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                CREATED              STATUS                          PORTS               NAMES
6a336cf80564        nginx:latest         "/bin/bash"            About a minute ago   Exited (0) 12 seconds ago                           tender_shockley       
ba8c55aac4a4        nginx:latest         "uname -a"             About a minute ago   Exited (0) About a minute ago                       romantic_ptolemy      
58c1569051e7        nginx:latest         "cat /etc/issue"       2 minutes ago        Exited (0) 2 minutes ago                            naughty_goldstine     
432903e8f881        nginx:latest         "echo 'hello docker'   2 minutes ago        Exited (0) 2 minutes ago                            backstabbing_newton   

komei:.boot2docker komei$ docker start 6a336cf80564
6a336cf80564
komei:.boot2docker komei$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
6a336cf80564        nginx:latest        "/bin/bash"         About a minute ago   Up 3 seconds        443/tcp, 80/tcp     tender_shockley  

- Dockerコマンド一覧

komei:.boot2docker komei$ docker -h

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders from a container's filesystem to the host path
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Stream the contents of a container as a tar archive
    history   Show the history of an image
    images    List images
    import    Create a new filesystem image from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container
    kill      Kill a running container
    load      Load an image from a tar archive
    login     Register or log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    pause     Pause all processes within a container
    ps        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a Docker registry server
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image to a tar archive
    search    Search for an image on the Docker Hub
    start     Start a stopped container
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Lookup the running processes of a container
    unpause   Unpause a paused container
    version   Show the Docker version information
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.

その他

今回は勉強に使っているMAC Book AirにDockerをインストールしました。vagrant + virtualbox + knife-soloより楽なので、簡単な検証はこちらでもいいかなと思います。VMに取って替わるものではないですが、ある程度置き換えがあると考えると緩く勉強しておこうかなと思います。


シェアして頂けると嬉しいです。
参考になったという方がいれば是非お願いしますm(_ _ )m
モチベーション維持の観点で非常に励みになります。

このエントリーをはてなブックマークに追加