1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[メモ] Windowsで、Vagrant+Virtualboxを使ってるとき、dockerが使える環境を作成

Posted at

概要

  • Windowsで、Vagrant+Virtualboxを使ってるとき、dockerが使える環境を作成。

  • docker と docker-compose が入る。

  • ベースは、ubuntu/xenial64 (16.04)

    • user: vagrant / password: vagrantで、ログインできるようになった :-)
  • Docker for Windowsは、Hyper-Vを使う (Windows 10 Pro/Ent 64bit)

  • Hyper-V と Virutalboxの共存はできない

環境

  • ホスト: Windows 10 64bit
  • VirtualBox: 5.2.6r120293
  • Vagrant: 2.0.2

手順

  1. Chocolateyにて、Virtualbox Vagrantをインストール

    cmd.exeを管理者で開いて、↓を実行

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
rem インストール
cinst -y virtualbox vagrant
rem (お好みで) cinst -y chocolateygui git VisualStudioCode bonjour


2. `Vagrantfile` 作成 (適当なフォルダにて. 例: C:\Vagrant\ubuntu16.04-docker\)

	```ruby:Vagrantfile

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

	VM_BOX="ubuntu/xenial64"
	VM_NAME="u16.04-docker"
	VM_MEMORY=2048
	VM_CORES=2

	Vagrant.configure("2") do |config|
	  config.vm.box = VM_BOX
	  #config.vm.box_check_update = false

	  #plugin: vagrant-vbguest 
	  #config.vbguest.auto_update = false

	  # forwarded port (public)
	  #config.vm.network "forwarded_port", guest: 80  , host: 80   # http
	  #config.vm.network "forwarded_port", guest: 443 , host: 443  # https
	  #config.vm.network "forwarded_port", guest: 3306, host: 3306 # MySQL

	  # forwarded port (only for 'host_ip')
	  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

	  # Host-only network
	  # config.vm.network "private_network", ip: "192.168.33.10"

	  # Bridged network
	  config.vm.network "public_network"

	  config.vm.provider "virtualbox" do |vb|
		# Display the VirtualBox GUI when booting the machine
		vb.gui = false
		vb.name = VM_NAME
		vb.cpus = VM_CORES
		vb.memory = VM_MEMORY
	  end

	  config.vm.provision "shell", inline: <<-SHELL
		echo `cat /proc/uptime | cut -f 1 -d ' '`  apt update
		# # apt cache server 
		# echo 'Acquire::http::Proxy "http://apt-cache-server:3142";' | sudo tee /etc/apt/apt.conf.d/02proxy
		apt-get -q update -q
		DEBIAN_FRONTEND=noninteractive apt-get -q -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
		apt-get install -y curl jq bash-completion
		#
		# Install: docker-ce
		echo `cat /proc/uptime | cut -f 1 -d ' '`  Installing: docker
		curl -sSL https://get.docker.com/ | sh
		# Install: docker-compose
		echo `cat /proc/uptime | cut -f 1 -d ' '`  Installing: docker-compose
		DOCKER_COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r ".tag_name")
		curl -sL https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose
		curl -L https://raw.githubusercontent.com/docker/compose/${DOCKER_COMPOSE_VERSION}/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
		docker-compose --version
	  SHELL

	  config.vm.provision "shell", privileged: false, inline: <<-SHELL
		sudo usermod -aG docker ${USER}
	  SHELL
	end
	```



3. VMの起動・再起動・ログイン

	```bash:Vagrantfileのあるところで、SHIFT+右クリック、PowerShellをひらいて
	# 起動
	vagrant up
	# 再起動
	vagrant reload
	# VMにログイン
	vagrant ssh
	```
    - git bashを使うときは、`export VAGRANT_PREFER_SYSTEM_BIN=1`をする。


4. VMにて、dockerのhello-worldを実行

    ```bash:dockerのhello-world
docker run -it --rm hello-world
```shell-session:実行例-こんなのがでてくればOK
vagrant@ubuntu-xenial:~$ docker run -it --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751
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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

vagrant@ubuntu-xenial:~$
```

その他

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?