2
5

More than 5 years have passed since last update.

Vagrant プロビジョニング設定 - Docker

Last updated at Posted at 2017-07-02

プロビジョニングには 環境設定のためのスクリプトを実行することができます。
Dockerの環境をインストールするスクリプトを作成したので、メモとして残しておきます。


概要

仮想マシン起動後に、プロビジョニングを実行して、Dockerの利用環境を自動構築する。

[インストールするソフト]


仮想マシン(CentOS7)を起動する

下記のコマンドにて、仮想マシンを起動する。

BOX追加

CentOS7のBOXを追加する

初期化

コマンドプロンプト
$ mkdir -p ~/Documents/Vagrant/ && cd ~/Documents/Vagrant/
$ mkdir opt
$ vagrant init centos/7

Vagrantfile修正

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

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "centos7"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./opt/", "/home/vagrant/opt/"
  config.vm.provision :shell, :path => "provision.sh"
end

実行するコマンドを作成(プロビジョニング)

仮想マシンの初回起動(vagrant up)時には、自動で実行されます。
ただし、2回目以降は、実行されないようです

コマンドプロンプト
$ vi ~/Documents/Vagrant/provision.sh
provision.sh
# Tool
sudo yum -y install vim git

# Docker
sudo curl -sSL https://get.docker.com/ | sh
sudo systemctl enable docker
sudo gpasswd -a vagrant docker
sudo systemctl start docker

# Docker Compose
sudo curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

仮想マシンを起動する

コマンドプロンプト
$ vagrant up

ネットワーク確認

コマンドプロンプト
$ ping -c 1 192.168.33.10
PING 192.168.33.10 (192.168.33.10): 56 data bytes
64 bytes from 192.168.33.10: icmp_seq=0 ttl=64 time=0.555 ms

プロビジョニングの実行状況確認

コマンドプロンプト
# ssh接続
$ vagrant ssh

# Git
[vagrant@localhost ~]$ git --version
git version 1.8.3.1

# vim
$[vagrant@localhost ~] vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 21 2016 17:00:20)

# Docker
[vagrant@localhost ~]$ docker --version
Docker version 17.06.0-ce, build 02c1d87

# Docker Compose
[vagrant@localhost ~]$ docker-compose --version
docker-compose version 1.14.0, build c7bdf9e

プロビジョニングを任意に実行する

初回起動時以降に、プロビジョニングを実行したい場合は、下記のコマンドにて実行可能です。

# プロビジョニング実行
$vagrant provision

# 起動時に一緒に実行
$vagrant up --provision

# 再起動時に一緒に実行
$vagrant reload --provision

参考文献

2
5
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
2
5