LoginSignup
17
18

More than 5 years have passed since last update.

【Vagrant/Docker】Windowsでも開発できるようにVagrant + Docker の環境を作る

Last updated at Posted at 2019-03-18

Docker for WindowsVagrant/VM VirtualBoxが相容れない問題

VirtualBox 6.0 から Hyper-V と共存できるはず - Qiita
を見てやっと共存できるようになったんか~~と思って試してみたんですが、
やっぱダメだったんであきらめてVagrant上でDocker動かすことにしました。

Vagrant + Docker の環境を作っていく

前回Docker for Windowsで環境構築しましたが、今回はVagrant + Dockerで環境構築していきます。

実行環境

OS:Windows 10 Pro 64bit
RAM:8GB

Vagrantfileを用意する

以下の記事を参考にさせていただきました :bow_tone2:
参考:centos7を起動してすぐDockerできるVagrantfile - Qiita

ただ、記事に書いてある文のままだとDocker Composeのバージョンが古いので、
GitHubのReleases · docker/composeを 開き、最新のバージョンに書き換えました。

Vagrantfile
Vagrant.configure("2") do |config|

  config.vm.box = "centos/7"

  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.network "private_network", ip:"192.168.77.11"

  config.vm.synced_folder "./", "/var/www"

  config.vm.provision "shell", inline: <<-SHELL
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum makecache fast
    sudo yum install -y docker-ce
    sudo systemctl enable docker-ce
    sudo systemctl start docker-ce
    sudo curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` > docker-compose
    sudo mv docker-compose /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    sudo gpasswd -a vagrant docker
    sudo systemctl restart docker
  SHELL
end

vagrant upするとエラーが発生

vagrant upすると↓のようなエラーが発生。

Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. 

This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 var_www_html /var/www/html

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

どうやらvboxsfがないらしい

ので上記のサイトを参考にプラグインvagrant-vbguestをインストール

$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.17.2)'!

完了したもよう

続いて再びvagrant up

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically

【--- 略 ---】

    default:                                  Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
100   617    0   617    0     0    197      0 --:--:--  0:00:03 --:--:--   197
  0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0
  1 8649k    1  100k    0     0  21775      0  0:06:46  0:00:04  0:06:42 94888
 27 8649k   27 2406k    0     0   430k      0  0:00:20  0:00:05  0:00:15 1233k
 40 8649k   40 3542k    0     0   526k      0  0:00:16  0:00:06  0:00:10 1149k
 51 8649k   51 4470k    0     0   577k      0  0:00:14  0:00:07  0:00:07 1091k
100 8649k  100 8649k    0     0  1024k      0  0:00:08  0:00:08 --:--:-- 1800k
    default: Adding user vagrant to group docker

完了

vagrant sshでssh接続

$ vagrant ssh
Last login: Thu Mar 14 02:04:26 2019 from 10.0.2.2
[vagrant@localhost ~]$ 

試しにdocker run hello-worldを実行

[vagrant@localhost ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d0377a99efb77324b0fe53577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[vagrant@localhost ~]$

無事に動いた

【番外編】 docker-compose upする

※ここでは前回、【Docker】Windows10でPHP5.3の環境を作るためにDockerを使用した際の軌跡 - Qiitaで作成した
docker-compose.ymlと同じものを使用します。

docker-compose.ymlを置いたディレクトリへ移動後、docker-compose up

[vagrant@localhost ~]$ cd ../../var/www/html/
[vagrant@localhost html]$ docker-compose up
Creating network "html_default" with the default driver
Creating volume "html_mysql-db" with local driver
Pulling db (vsamov/mysql-5.1.73:latest)...
latest: Pulling from vsamov/mysql-5.1.73
30d541b48fc0: Pull complete

[~~~ 略 ~~~]

db_1   | 190314  3:11:18 [Note] mysqld: ready for connections.
db_1   | Version: '5.1.73'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
www_1  | httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3 for ServerName

動いた

上記で作成したVagrantfileだと http://192.168.77.11:8080でブラウザ上から確認できる

その他dockerコマンド

docker起動

[vagrant@localhost ~]$ service docker start

ちなみに次回起動時はservice docker startする必要がある

dockerが動いているか確認

[vagrant@localhost html]$ sudo service docker status

おわり

参考URL

17
18
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
17
18