おれさまラボ

実際に手を動かして理解を深めるブログ。

Dockerを試す - 起動編

Dockerについて、なんとなく勉強したけど、「やっぱり手を動かさなきゃわからん!」ということで、基本的な使い方を試してみました。

検証環境説明

今回の検証環境は、以下の通りです。 * ホストOS:Windows10 Pro * 仮想化支援:Vagrant 1.8.6 * 仮想プラットフォーム:VirtualBox * Docker専用OS:Barge

今回は、Docker専用OSである「Barge」を使ってみました。Docker専用OSとしては、Atomic HostやCore OSが有名なんですかね?

今回、使用するBargeはDocker専用OSであることは、もちろん特色の一つなのですが、OS容量がわずか13MByteという驚異的な小ささのOSです。

Barge(Docker ホスト用軽量 OS)って? - Qiita

この軽さのお陰で、ダウンロード含めても検証環境構築に5分もかからなかったです。

検証環境の構築

Vagrant環境の構築

Vagrant環境がない場合は、Vagrant環境の構築からです。

VirtualBoxインストーラを公式サイトからGET&Installします。

Oracle VM VirtualBox

VirtualBoxをインストールしたら、Vagrantを公式サイトからGET&Installします。

Vagrant by HashiCorp

インストール後、コマンドプロンプトを起動して、下記コマンドを確認しましょう。バージョンが返ってくれば成功です。

>vagrant -v
Vagrant 1.8.6

bargeホストの作成

Dockerを起動するOSホストをVagrantホストとして作成します。

コマンドプロンプトを起動して、vagrantfileを起きたいディレクトリに移動しておきましょう。

vagrant addします。Choiceでは「2」を選択します。

>vagrant box add ailispaw/barge
==> box: Loading metadata for box 'ailispaw/barge'
    box: URL: https://atlas.hashicorp.com/ailispaw/barge
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) veertu
2) virtualbox

Enter your choice: 2
==> box: Adding box 'ailispaw/barge' (v2.2.6) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/ailispaw/boxes/barge/versions/2.2.6/providers/virtualbox.box
    box: Progress: 100% (Rate: 3070k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'ailispaw/barge' (v2.2.6) for 'virtualbox'!

vagrant initします。

>vagrant init -m ailispaw/barge
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

vagrant upします。これで、bargeが起動します。

>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ailispaw/barge'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ailispaw/barge' is up to date...
==> default: Setting the name of the VM: barge_default
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 2375 (guest) => 2375 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: bargee
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!

TeraTermなどのSSHクライアントから、bargeへSSH接続します。接続方法がわからない場合は、vagrant sshを実行すれば、接続方法が出てきます。UsernameとPasswordは同一です。

>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: bargee
Private key: <鍵ファイルのフルパスが表示されます>

SSH接続できたらOKです。

[bargee@barge ~]$ uname -a
Linux barge 4.4.28-barge #1 SMP Fri Oct 28 19:52:37 UTC 2016 x86_64 GNU/Linux

いよいよDockerを扱ってみる

まずは、Dockerイメージを取得します。(docker pull)

$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos

8d30e94188e7: Pull complete
Digest: sha256:<ハッシュ値が表示される>
Status: Downloaded newer image for centos:latest

イメージが取得できたことを確認します。(docker image)

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED
centos              latest              <Image IDが表示される>        7 weeks ago

dockerイメージにアクセスします。(docker run)

$ docker run -it centos /bin/bash

プロンプトが「$」から「#」に代わり、Dockerコンテナイメージに入れていることがわかります。

Dockerイメージから出ます。

# exit

プロンプトが「#」から「$」に代わり、Dockerコンテナイメージから出れていることを確認します。

まとめ

今回は、Dockerを起動するところまで。Vagrant環境があればここまで5分位でできてしまうので、Docker触ったことない方はぜひやってみると良いのでは、と思いました。とりあえず、めっちゃ便利そう。