LoginSignup
12
14

More than 5 years have passed since last update.

Vagrant+CentOS7+Ansibleで手短にDocker環境を構築したい

Last updated at Posted at 2015-12-02

必要ソフトウェア

  • Vagrantをインストールしておく
  • Virtualboxをインストールしておく

vbguestをインストールしておく

事前にVirtualBox Guest Additionsプラグインをインストールしておく。
これがないと、hashicorpのCentOS7 boxでは、synced_folderが指定できない。

vagrant plugin install vagrant-vbguest

Vagrantfileを作る

最低限、こんなかんじ。

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos/7"
  config.vm.network "private_network", ip: "192.168.11.11"
  config.vm.provision "shell", :path => "provision.sh"
  config.vm.synced_folder ".", "/home/vagrant/sync", disabled: true
  config.vm.synced_folder ".", "/vagrant"
end

プロビジョン時にAnsibleをインストールする

AnsibleはゲストOS、つまりCentOS7上で動かすようにする。そのために、Ansibleをプロビジョニング時にインストールする。

provision.sh
#!/usr/bin/env bash

set -eux

if ! [ `which ansible` ]; then
  sudo yum install -yq epel-release
  sudo yum install -yq ansible sshpass
fi

ansible-playbook -i /vagrant/ansible/hosts /vagrant/ansible/playbook.yml

AnsibleのPlaybookをつくる

次の2つのファイルを作る。

  • ./ansible/host
  • ./ansible/playbook.yml
ansible/host
127.0.0.1 ansible_connection=local

Playbookはこんなかんじに。

ansible/playbook.yml
---
- hosts: 127.0.0.1
  connection: local
  sudo: yes
  tasks:
    - name: Docker group should exist
      group: name=docker state=present

    - name: Vagrant should be a member of docker group
      user: name=vagrant groups=docker state=present

    - name: Docker should be installed
      yum: name=docker state=present

    - name: Docker should be running
      service: name=docker state=started enabled=yes

    - name: docker-compose command should be installed
      get_url: url=https://github.com/docker/compose/releases/download/1.5.1/docker-compose-Linux-x86_64 dest=/usr/local/bin/docker-compose mode=755 sha256sum=5b9b6287c95103a553f8902e1e607bf4cdae74466f66a78d25195252ba380b0e

あとは、Vagrantを起動するだけ。

vagrant up --provider=virtualbox
12
14
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
12
14