LoginSignup
12
8

More than 3 years have passed since last update.

vagrant+docker-compose環境を構築

Last updated at Posted at 2019-11-23

主に自分用メモです。

仮想マシン(VirtualMachine / VM)とDockerの違いについては以下を参照ください。
「Docker」を全く知らない人のために「Docker」の魅力を伝えるための「Docker」入門

また、使い方の参考は入門 Dockerが良いです。こちらはDocker技術のキャッチアップにも役立ちます。
しかも設計についても解説しており非常に勉強になります。
入門ですがしっかりした内容なのでより軽いものであれば他のものでも。

ゴール

Vagrantfileを記述し、vagrantでssh接続した際にdockerとdocker-composeが動作できる環境を構築します。

目的

vagrantを用いることで、仮想マシンを構築することができます。
また、Vagrantfileを用いることで、その仮想環境の共有が容易にできるようになります。

dockerとdocker-composeも同時にセットアップし、
今後に向けてdocker-compose.ymlによって複数コンテナの自動セットアップまでの導線を用意します。

余談ですが、筆者はvagrantを用いず、windows環境で直にdocker-composeを動かした際に
volumeのオプションのパスがうまく設定できずハマりました。
dockerはlinux技術を元に構築されており、それをwindows上で無理やり起動しているため少し躓きます。
そんな悲しいことにもならないように、開発環境はvagrant環境の上で構築したいものです。

筆者環境

macOS Catalina 10.15.1

ディレクトリ
❯ tree
.
├── Vagrantfile
└── data

事前準備

下記をインストールしておくこと

Vagrantのプラグインをインストール

以下をターミナルで実行します。
vagrant plugin install vagrant-vbguest
vagrant plugin install vagrant-docker-compose

❯ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Fetching: micromachine-3.0.0.gem (100%)
Fetching: vagrant-vbguest-0.21.0.gem (100%)
Installed the plugin 'vagrant-vbguest (0.21.0)'!

❯ vagrant plugin install vagrant-docker-compose
Installing the 'vagrant-docker-compose' plugin. This can take a few minutes...
Fetching: vagrant-docker-compose-1.5.1.gem (100%)
Installed the plugin 'vagrant-docker-compose (1.5.1)'!

プラグインインストール確認

❯ vagrant plugin list
vagrant-docker-compose (1.5.1, global)
vagrant-vbguest (0.21.0, global)

Vagrantfileの作成

カレントディレクトリのVagrantfileに基づいてvagrantはセットアップを行います。
そのため、適当なフォルダを作り、その中にVagrantfileの作成すると良いでしょう。

Vagrantfileの中身は今回以下です。(備忘録の記述が多いです。)

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

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.box_version = "201910.20.0"

  config.ssh.insert_key = false

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  config.vm.network "forwarded_port", guest: 80, host: 5000

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./data", "/vagrant"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    # vb.gui = true

    # Customize the amount of memory on the VM:
    vb.memory = "4096"
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    sudo timedatectl set-timezone Asia/Tokyo
    sudo apt-get update
    # sudo apt-get install -y apache2
  SHELL

  # if Vagrant.has_plugin?("vagrant-proxyconf")
  #   config.proxy.http     = "http://hogeproxy:8080/"
  #   config.proxy.https    = "https://hogeproxy:8080/"
  #   config.proxy.no_proxy = "localhost,127.0.0.1,.example.com"
  # end

  # install Docker
  config.vm.provision :docker
  # Install the following plug-ins in advance
  # $ vagrant plugin install vagrant-docker-compose
  config.vm.provision :docker_compose, compose_version: "1.24.1" #, yml: "docker-compose.yml"
end

こちらを参考にして作成。

共有フォルダについて補足

config.vm.synced_folderでホストOSとゲストOS間で共有フォルダを設定しています。

上記の設定では、ローカルマシンの Vagrantfile があるディレクトリ内の data フォルダと
仮想マシン内の /vagrant ディレクトリを共有しています。
書式config.vm.synced_folder {host_path}, {guest_path}, option...

起動

Vagrantfileの配置されているディレクトリで
vagrant upで起動します。

起動確認

vagrant statusで現在の起動状態が確認できます。「running」であれば成功です。

❯ vagrant status
Current machine states:

default                   running (virtualbox)

ゲストOSへのSSHアクセス方法

以下のVagrantfileの配置されているディレクトリで
vagrant sshで接続します。

またはVagrantfileで設定したIP(デフォルト: 192.168.33.10)へSSHでアクセスできます。
デフォルトでは以下の設定になります。
ID: vagrant
PW: vagrant

docker, docker-compose確認

vagrant@vagrant:~$ docker -v
Docker version 19.03.5, build 633a0ea838

vagrant@vagrant:~$ docker-compose -v
docker-compose version 1.24.1, build 4667896b

停止

vagrant haltで停止します。

VM削除

vagrant環境を作り直したい場合に利用
vagrant destroy

vagrant関連のプロセスをキルしたい場合
ps -ef |grep VBox | awk '{print $2}' | xargs kill
参考: https://stackoverflow.com/a/16876318

12
8
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
8