32
42

More than 5 years have passed since last update.

Docker for MacからVagrant + CoreOSに切り替えた

Last updated at Posted at 2017-06-03

きっかけ

Docker for MacをMacにインストールしてDockerを使用していました。しかし、nginx-proxyのような、/var/run/docker.sockにボリュームする必要があるコンテナとかでは、少し開発が面倒になってきました。Docker for Macでもボリュームは可能かもしれませんが、余計な手間をかけたくありませんでした。
また、docker composeも重たくて、buildやupをするたびに少々待たされるのが億劫になってきたのがきっかけです。

手順

Vagrant + VirtualBoxのインストール

VagrantVirtualBoxをホームページからダウンロード。そして、適当に同意とかしながらインストール。

CoreOSの設定(主にVagrantfile)

Vagrant+CoreOS+Dockerを参考に作成しました。
VMを扱っていくフォルダの作成
個人的にVMはまとめて保存したいのでvmというフォルダを作成して移動

$mkdir vm 
$cd vm

Vagrantfileをクローンしてくる

$ git clone https://github.com/coreos/coreos-vagrant.git
Cloning into 'coreos-vagrant'...
remote: Counting objects: 448, done.
remote: Total 448 (delta 0), reused 0 (delta 0), pack-reused 448
Receiving objects: 100% (448/448), 110.64 KiB | 0 bytes/s, done.
Resolving deltas: 100% (202/202), done.

Vagrantfileを編集してホスト側のファイルを見えるようにする。また、メモリの割り当てなども少し編集

  • メモリの変更 $vm_memory = 1024 -> $vm_memory = 2048
  • ホスト側の見えるフォルダの設定(サイト通り)
config.vm.network :private_network, ip: ip
# Uncomment below to enable NFS for sharing the host machine into the coreos-vagrant VM.
#config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true, :mount_options => ['nolock,vers=3,udp’]
 ↓
config.vm.network "private_network", ip: "172.12.8.150"
config.vm.synced_folder "ホスト側のフォルダの指定(例 ~/docker)", "/home/core/share", id: "core", :nfs => true,  :mount_options => ['nolock,vers=3,udp']

CoreOS(Vagrant)の起動

$ 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_1496498968538_60770
==> 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 (guest) => 2222 (host) (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:2222
    core-01: SSH username: core
    core-01: SSH auth method: private key
    core-01: Warning: Remote connection disconnect. Retrying...
    core-01: Warning: Connection reset. 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...

途中でパスワードを聞かれますので、いつもPCにログインする時のパスワードを入力しましょう。

ただ、VirtualBoxをアップデートした直後とかは、うまくいかないことがありました。
そんなときは、エラーの説明文通りに

$ vagrant plugin repair
Repairing currently installed plugins. This may take a few minutes...
Fetching: micromachine-2.0.0.gem (100%)
Fetching: vagrant-alpine-0.3.0.gem (100%)
Fetching: vagrant-vbguest-0.14.2.gem (100%)
Installed plugins successfully repaired!

vagrant plugin expunge --reinstall

This command permanently deletes all currently installed user plugins. It
should only be used when a repair command is unable to properly fix the
system.

Continue? [N]:

Vagrant expunge has been declined. Skipping removal of plugins.

Vagrant will now attempt to reinstall user plugins that were removed.
Installing the 'vagrant-alpine' plugin. This can take a few minutes...
Installed the plugin 'vagrant-alpine (0.3.0)'!
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.14.2)'!

をするとvagrant upができるようになりました。

Docker Compose のインストール

CoreOSにSSHでログインしてDocker Composeをインストールします。
なお、Docker Composeは最新のモノをこちらから確認してインストールしてください。(2017年7月地点で1.14.0)

$ vagrant ssh
Last login: Tue Jul 25 10:59:16 UTC 2017 from 10.0.2.2 on ssh
Container Linux by CoreOS alpha (1437.0.0)
core@core-01 ~ $
core@core-01 ~ $
core@core-01 ~ $ sudo -i
core-01 ~ # mkdir -p /opt/bin
core-01 ~ # curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /opt/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    969      0 --:--:-- --:--:-- --:--:--   979
100 8084k  100 8084k    0     0   583k      0  0:00:13  0:00:13 --:--:-- 1421k
core-01 ~ # chmod +x /opt/bin/docker-compose
core-01 ~ # docker-compose -v
docker-compose version 1.14.0, build c7bdf9e
core-01 ~ # exit
logout
core@core-01 ~ $

内容の説明としまして、まず保存するフォルダ(/opt/bin)を作成します。そして、Docker Composeをダウンロードしてきます。

curl -L https://github.com/docker/compose/releases/download/(Docker Composeのバーション)/docker-compose-`uname -s`-`uname -m`

あとは、権限を設定して、正常に動くか確認して完成です。

Docker for Mac のアンインストール

もしも不要ならば、Docker for macのアイコンをクリックして設定画面を起動。
Resetタブの中にある、Uninstallをクリックして、削除できます。
その後は、アプリケーションにあるDocker for macを削除してね、ってなるので削除して終了です。(ちょっと記憶が曖昧)

感想

作業時間が思った以上に短くて、簡単にセットアップができました。
なにより嬉しいことはDockerの起動やDocker Composeの動作が軽くなったことです。
これからは快適に作業を勧めていきたいです。
まだまだ未熟なので、他にも便利な設定や環境があれば、教えてください。

2017/7 修正項目

docker-compose のバージョンを1.14.0に修正しました。

32
42
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
32
42