ログってなんぼ

日々のメモです

MacにVagrant+CoreOS+Dockerの環境を作る

作業した環境

vagrant

Vagrant インストール済み

vm保管ディレクトリへ移動

$ cd ~/vm

自分がいつもvmのイメージを置いてある任意の場所です

CoreOSの準備

$ git clone https://github.com/coreos/coreos-vagrant.git

cloneしたディレクトリへ移動

$ cd coreos-vagrant/

とりあえずREADMEを読む・・・

$ view README.md

virtual boxで起動する場合、vmware fusionで起動する場合などの説明や、共有フォルダのことなど色々書いてあるのでひと通り読んでおく。

#### Shared Folder Setup

There is optional shared folder setup.
You can try it out by adding a section to your Vagrantfile like this.


config.vm.network "private_network", ip: "172.17.8.150"
config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true,  :mount_options   => ['nolock,vers=3,udp']

なるほど、ということで・・・

共有フォルダ関連の設定

とりあえず、macの任意ディレクトリを共有できるように設定しとく

$ vim Vagrantfile

config.vm.network "private_network", ip: "172.17.8.150"

config.vm.synced_folder "/Users/okisanjp/workspace", "/home/core/share", id: "core", :nfs => true,  :mount_options   => ['nolock,vers=3,udp']

該当箇所を書き換えておく

起動

$ vagrant up

Bringing machine 'core-01' up with 'virtualbox' provider...
==> core-01: Importing base box 'coreos-alpha'...
==> core-01: Matching MAC address for NAT networking...
==> core-01: Checking if box 'coreos-alpha' is up to date...
==> core-01: Setting the name of the VM: coreos-vagrant_core-01_1413946981404_29238
==> core-01: Fixed port collision for 22 => 2222. Now on port 2200.
==> core-01: Clearing any previously set network interfaces...
==> core-01: Preparing network interfaces based on configuration...
    core-01: Adapter 1: nat
    core-01: Adapter 2: hostonly
==> core-01: Forwarding ports...
    core-01: 22 => 2200 (adapter 1)
==> core-01: Running 'pre-boot' VM customizations...
==> core-01: Booting VM...
==> core-01: Waiting for machine to boot. This may take a few minutes...
    core-01: SSH address: 127.0.0.1:2200
    core-01: SSH username: core
    core-01: SSH auth method: private key
    core-01: Warning: Connection timeout. Retrying...
==> core-01: Machine booted and ready!
==> core-01: Setting hostname...
==> core-01: Configuring and enabling network interfaces...
==> core-01: Exporting NFS shared folders...
==> core-01: Preparing to edit /etc/exports. Administrator privileges will be required...
Password:
The nfsd service does not appear to be running.
Starting the nfsd service
==> core-01: Mounting NFS shared folders...

途中でmacの管理者パスワードを求められるので入力

確認

$ vagrant status
Current machine states:

core-01                   running (virtualbox)

The VM is running. To stop this VM, you can run vagrant halt to
shut it down forcefully, or you can run vagrant suspend to simply
suspend the virtual machine. In either case, to restart it again,
simply run vagrant up.

sshしてみる

$ vagrant ssh
CoreOS (alpha)
core@core-01 ~ $

共有フォルダの確認

core@core-01 ~ $ ls -l /home/core/share
total 44
drwxr-xr-x  7 501 20 238 Oct  7 08:54 Amazon-link-builder
drwxr-xr-x  5 501 20 170 Oct  1 10:45 MyPlayground.playground
drwxr-xr-x 21 501 20 714 Oct 20 14:37 chef-recipes

macのフォルダ(自分の場合はワークスペースフォルダ)がマウントされてます

Dockerを使ってみる

CentOSをpull

core@core-01 ~ $ docker pull centos:latest
centos:latest: The image you are pulling has been verified

511136ea3c5a: Pull complete
5b12ef8fd570: Pull complete
87e5b6b3ccc1: Pull complete
Status: Downloaded newer image for centos:latest
core@core-01 ~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              87e5b6b3ccc1        2 weeks ago         224 MB

動かしてみる

core@core-01 ~ $ docker run -i -t -d centos /bin/bash
6a279fd33fd141de484c1fd704feb0a401310e12ba63c5c3cbcd7afcfc689490
core@core-01 ~ $ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6a279fd33fd1        centos:latest       "/bin/bash"         5 seconds ago       Up 4 seconds                            desperate_meitner

動いたー