LoginSignup
3
4

More than 5 years have passed since last update.

Mac で Docker 入門

Posted at

事前準備

  • VirtualBox をインストールする

boot2docker をインストールする

Homebrew からインストールする。
公式では pkg からのインストール手順が書いてあったけど無視してやってみる。

$ brew install boot2docker
==> Installing boot2docker dependency: docker
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/docker-1.3.2.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring docker-1.3.2.mavericks.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
?  /usr/local/Cellar/docker/1.3.2: 9 files, 6.9M
==> Installing boot2docker
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/boot2docker-1.3.2.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring boot2docker-1.3.2.mavericks.bottle.tar.gz
==> Caveats
To have launchd start boot2docker at login:
    ln -sfv /usr/local/opt/boot2docker/*.plist ~/Library/LaunchAgents
Then to load boot2docker now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.boot2docker.plist
==> Summary
?  /usr/local/Cellar/boot2docker/1.3.2: 3 files, 7.2M

完了後に VirtualBox を起動してみると、boot2docker-vm が作成されていることが確認できる。

boot2docker の初期化・実行

init コマンドで初期化する。

$ boot2docker init
Latest release for boot2docker/boot2docker is v1.5.0
Downloading boot2docker ISO image...
Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.5.0/boot2docker.iso
    to ~/.boot2docker/boot2docker.iso
Generating public/private rsa key pair.

up コマンドで、VM 内で Docker デーモンを起動する。
環境変数 DOCKER_XXXX を設定しろと言われるので、言われた通り設定しておく。

$ boot2docker up
Waiting for VM and Docker daemon to start...
....................ooooooooooooooo
Started.
Writing ~/.boot2docker/certs/boot2docker-vm/ca.pem
Writing ~/.boot2docker/certs/boot2docker-vm/cert.pem
Writing ~/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2376
    export DOCKER_CERT_PATH=~/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1

ip コマンドで VM の IP アドレスが確認できる。

$ boot2docker ip

 The VM's Host only interface IP address is: 192.168.59.103

Hello, World してみる

docker run でコンテナが起動される。
hello-world というコンテナが用意されているみたい。
ローカルにイメージが存在しない場合は勝手に落としてくるよう。

$ docker run hello-world
Unable to find image 'hello-world' locally
511136ea3c5a: Pull complete
31cbccb51277: Pull complete
e45a5af57b00: Pull complete
hello-world: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.

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/

Nginx を起動してみる

$ docker run -d -P --name web nginx
Unable to find image 'nginx' locally
30d39e59ffe2: Pull complete
c90d655b99b2: Pull complete
d9ee0b8eeda7: Pull complete
3225d58a895a: Pull complete
224fea58b6cc: Pull complete
dcf288ac410a: Pull complete
738e1ae463aa: Pull complete
d354b6e23025: Pull complete
dd96e1e1328f: Pull complete
2fc09613828e: Pull complete
ba391ddcb570: Pull complete
2485b0f89951: Pull complete
511136ea3c5a: 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.


Status: Downloaded newer image for nginx:latest
bee10295b9f327322906a6e04c6a2bdcfc8140e681d1bd34a184a8cfabd74b8f

起動後、ps コマンドでポートを確認する。

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
bee10295b9f3        nginx:latest        "nginx -g 'daemon of   47 seconds ago      Up 47 seconds       0.0.0.0:49153->443/tcp, 0.0.0.0:49154->80/tcp   web

boot2docker ip で確認したアドレス(http://192.168.59.103:49154/)にブラウザからアクセスすると、nginx の画面が表示される。

Docker コンテナを削除する

$ docker stop web
web

$ docker rm web
web
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4