我希望通过使用Vagrant、CentOS7和Ansible来快速构建Docker环境

必需軟體

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

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

提前安装vbguest

提前安装VirtualBox Guest Additions插件。
如果没有这个插件,在hashicorp的CentOS7 box中无法指定synced_folder。

vagrant plugin install vagrant-vbguest

创建一个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

在 provision 时安装 Ansible

为了在CentOS7上运行,将Ansible安装在guest OS上。因此,在进行软件配置时,需要在Provisioning过程中安装Ansible。

#!/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

创建以下两个文件。

    • ./ansible/host

 

    ./ansible/playbook.yml
127.0.0.1 ansible_connection=local

Playbook 是这样的。

---
- 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
广告
将在 10 秒后关闭
bannerAds