Vagrant and docker on windows

Setting up a Docker host on Windows with Vagrant

Apr 19, 2016 · 2 min read

Today, we’ll provision a Docker host together using Vagrant and Virtualbox on Windows.

  1. Install Babun
  2. Install VirtualBox (this guide uses 5.0.18)
  3. Install Vagrant
  4. Open a new Babun shell window and create a directory for our vagrant box:

Download the Vagrantfile for ubuntu (trusty 64-bit)

The Vagrantfile contains instructions for how Vagrant should build your virtual machine. Open the Vagrantfile with your text editor of choice and find the following sections:

Modify it so that Vagrant assigns the IP address “192.168.10.101” and installs Docker for us:

After the edits, the file should look like so:

Back in the Babun shell window, we can bring the virtual machine online by running:

Once the Vagrant box is ready, ssh to it:

Now we can start running docker images!

Remember that IP address we set earlier? Now we can alias it in our hosts file so that it’s easier to remember. Open c:\windows\system32\drivers\etc\hosts (make sure to run your editor as administrator) and add the following line:

Save your changes to the hosts file, open your browser, and navigate to http://dockerbox and you should see the nginx welcome page.

Congratulations, you just created a local docker host and deployed nginx!

Vagrant and docker on windows

Set up an Apache web server inside a Docker container using Vagrant on windows. This will use VirtualBox to create an automatic host machine to host all Docker containers Edited by Jessica Rankins on 6/15/2017

  • Use Vagrant and Docker to configure a Docker container in a Vagrantfile.
  • Demonstrate Infrastructure as code principles (script configures and provisions environments to ensure environment parity).
  • Networking: Forwarded ports to host from VM from Web container: 80 to 8080 to 8080 for apache webserver demonstration
  • Apache web server with example page

PREREQUISITES: ON WINDOWS HOST COMPUTER, HAD TO:

Install VirtualBox (any version should work)

Install Git for Windows (version 2.12.2.windows.1)

Install Vagrant (version 1.9.4)

Install DockerToolbox with Kitematic (version 17.04.0-ce)

On vagrant up kept getting error:

To solve this issue, I ran docker-machine env default which said to run the following command before using docker in the command line after a fresh restart/start up of the computer: @FOR /f «tokens=*» %i IN (‘docker-machine env default’) DO @%i

Got the following error (fixed when disabled synced folders — use Docker volumes instead)

  • Start Docker Quickstart Terminal to start Docker deamon
  • Run @FOR /f «tokens=*» %i IN (‘docker-machine env default’) DO @%i
  • make sure you have a folder public-html
  • change synced folders if necessary (HOST_SYNC_FOLDER default is the current directory, CONTAINER_SYNC_FOLDER default is /vagrant)
  • cd to directory with Vagrantfile and vagrant up
  • Must run vagrant docker-exec default — sudo service apache2 restart for apache server to work after vagrant up
  • Have to forward port from Vagrant host to Host Machine:
    • Run the command VBoxManage controlvm «boot2docker-vm» natpf1 «tcp-port8000,tcp,,8000,,8000»
    • OR if you do not have VBoxManage:
      • go to VirtualBox, Settings of default boot2docker container, Network, NAT Advanced, Port Forwarding, create new with: id — web, Host IP — 127.0.0.1, Host Port — 8080, Guest Port — 8080
  • See example web page in host browser at 127.0.0.1:8080

SOME USEFUL COMMAND LINE COMMANDS:

  • vagrant up [—provider=docker] to create/start the container corresponding to the Vagrantfile in the current directory [by using Docker as the provider and not VirtualBox if it does not detect it]
  • vagrant docker-exec [VMname] — command_to_execute to run a one-off command against a Docker container by containername (error if container not running)
  • vagrant docker-logs to see the logs of a running container
  • vagrant reload [—provision] to reload container to include new Vagrantfile commands [and reload provisions]
  • vagrant destroy to shut down and deallocate resources corresponding to container in this directory
  • vagrant ssh to start ssh session into container in this directory (end by typing «logout»); Uses Git, private key provided by Vagrant (this container does not support ssh)
Читайте также:  View client для linux

EXECUTION OF VAGRANTFILE COMMANDS:

  • «name» in «config.vm.define ‘name’ do |n|» is the same as the «config» variable.
  • Commands placed inside the «config.vm.define ‘name’ do |n|» are applied only to the defined container (name).
  • Commands placed outside this command are done to all containers.
  • Commands are executed outside-in, in the order listed in the Vagrantfile.

SPECIFIC TO DOCKER WITH VAGRANT:

  • Mac and Windows cannot run Linux containers, so Vagrant automatically creates a single «host VM» to run Docker when the first vagrant up [—provider=docker] is done. Vagrant will run multiple containers on this one VM. You can alter the host VM by specifying a Vagrantfile to use when creating the host VM. The global-status command is used to control the host VM.
  • VirtualBox uses the boot2docker image to create the VM. boot2docker does not support VirtualBox Guest Additions, so virtualbox and docker folder syncing implementations cannot be used. Read only file system on boot2docker. Use volume in container instead
  • Forwarded ports from container will be forwarded to the remote Docker host VM. If you want to access ports, forward from container in Vagrantfile and then MANUALLY FORWARD PORTS IN VIRTUALBOX FROM VM to host.

About

Set up an Apache web server inside a Docker container using Vagrant and VirtualBox on Windows.

Vagrant and docker on windows

This repo is a collection of various Vagrant environments to work with Windows Containers on a Windows Server 2019 or Windows 10 VM.

There are different Vagrantfiles for each scenario:

  • Vagrantfile — Windows Server 2019 and Docker 18.09.0
  • WSL 2
    • wsl2/server/Vagrantfile — Windows Server Core, version 2004 with WSL 2
    • wsl2/windows10/Vagrantfile — Windows 10, version 2004 with WSL 2
  • lcow/Vagrantfile — Windows 10 with nightly Docker and LCOW enabled
  • nano/Vagrantfile — Test setup to have Docker engine installed in a Windows Nanoserver VM
  • swarm-demo/Vagrantfile — some Windows Server 2016 VM’s in classical Docker Swarm
  • swarm-mode/Vagrantfile — some Windows Server 2016 VM’s in Docker Swarm-mode and overlay network
  • windows10/Vagrantfile — Windows 10 1809 and nightly Docker to test process isolation
  • docker-machine test environments
    • docker-machine/Vagrantfile — Windows 10 with docker-machine installed to test with VMware Workstation
    • docker-machine-fusion/Vagrantfile — macOS 10.13 with docker-machine installed to test with VMware Fusion

This repo has started with a single Vagrantfile that is explained below. As you can see there are several interesting setups here as well in the sub folders.

Have a look at my blog posts how to Setup a local Windows 2016 TP5 Docker VM and Adding Hyper-V support to 2016 TP5 Docker VM for more details. I also can recommend a more up to date guide Getting started with Windows Containers by @glennsarti

After provisioning the box has the following tools installed:

  • Windows Server 2019 with Docker Engine 18.09.0 and client
  • docker-machine 0.16.0
  • docker-compose 1.23.0
  • (Docker Tab completion for PowerShell (posh-docker))
  • Chocolatey
  • Git command line
  • Git Tab completion for PowerShell (posh-git)
  • SSH client

Optionally you can create a Hyper-V Docker Linux machine and have a multi architecture experience in one VM.

Tested with Vagrant 2.1.2, VMware Fusion Pro 10.1.3 on a MacBookPro with Retina display. The Vagrant box will be started in fullscreen mode also with Retina support.

Читайте также:  Asus m50s драйвера windows

You can learn and play a lot of scenarios with it:

Future work will be a Docker Swarm with both Linux and Windows Docker Engines.

Get the base box

First register to evaluate Windows 2019, but you don’t need to download the ISO manually.

The Vagrant base box is available in Vagrant Cloud https://app.vagrantup.com/StefanScherer, these are all eval versions of Windows Server 2019 or Windows 10.

Vagrant will download the base box if it’s not available locally, a vagrant box list shows which boxes you already have downloaded.

Install Vagrant reload plugin

As we need to reboot the VM once during the provisioning, you will need an additional Vagrant plugin

Spin up the box

To start the VM with Vagrant run this command

You only have to logout and login once to have the Docker tools in user vagrant’s PATH.

Create some Windows Docker Container images

You may clone my dockerfiles-windows repo and create some container images.

Test the nightly Windows Docker Engine

You can update the Docker Engine with the script

This will stop the Docker service, download the nightly build from https://master.dockerproject.org and restart the service.

Create a Linux Docker machine in Hyper-V

If you want to try out multi architecture you also use docker-machine to create a Linux Docker Engine running in Hyper-V. I have prepared a script that will set up everything as there are some known issues.

This PowerShell script creates a Docker machine and updates Docker Engine to the latest so that the Windows Docker client is able to communicate with the Linux Docker Engine.

Use the Linux Docker machine

Open a PowerShell terminal as an administrator and select the Linux Docker machine with

Now run your first busybox container with

Windows Docker Swarm demo

Use Vagrant to control your box

From your host control your Vagrant box with the usual Vagrant workflow:

  • vagrant up
  • vagrant halt
  • vagrant destroy -f
  • vagrant snap take
  • vagrant snap rollback
  • .

Writing the installation script for the Hyper-V Docker machine the snapshot functions helped me a lot to test the script again and again.

About

Various Vagrant envs with Windows 2019/10 and Docker, Swarm mode, LCOW, WSL2, .

Vagrant and docker on windows

This repository will guide you how to setup docker command to be used directly from bash using Windows Subsystem for Linux (WSL1).

  • VirtualBox installed in Windows
  • Vagrant installed in Windows
  • Plugin vbguest installed in vagrant: vagrant plugin install vagrant-vbguest (automatically installed via the .vagrantplugins file)

How to set it up

Install docker client by running the following inside bash :

Next is to append configuration to

/.bashrc for docker to use TCP instead of file:

Note! Since 18.09 it is also possible to access the Docker daemon via SSH:

Now when this is done the docker client is ready to go. It is now time to setup the VM which hosts the Docker daemon.

This guide assume you have Vagrant installed in Windows and therefore it is necessary for add an alias to

/.bash_aliases to make Linux use the Vagrant in Windows:

Time to create the VM which hosts the Docker daemon and you do this by just making sure your current working directory is in the root of this repository and run the following:

Important! Ensure that IP range 192.168.56.1-192.168.56.255 is not used and handled by any DHCP in your network because this is used for the host-only network adapter. If this IP range is in use please look into the Advanced section how to change the configuration.

After this point the VM is ready and the Docker daemon is started. Test the docker client by running the following command:

Читайте также:  Что такое дебаггер windows

General usage tip

After the you have setup everything and run the vagrant up once then from that point you can start and stop the box by explicitly specifying the ID. This ID can be found using the command vagrant global-status . Using the ID will allow you to start, stop and reprovision the box from any location (you do not need to go to the directory of the Vagrantfile).

If you would like to override any configuration defined in default-config.yml then this is possible by adding a new file called user-config.yml in this directory (will be ignored by git). The easiest way to start modifying the configuration is to look at default-config.yml and just create the user-config.yml with only the variables you want to override.

Disable host-only adapter

For example if you want to disable the host-only network adapter and only use NAT, then the user-config.yml can look like this:

You can so that the Vagrant box will use a proxy. Example below show you how to do this in your user-config.yml :

To set proxy use the environment variables which vagrant-proxyconf looks at. Read official documentation: vagrant-proxy

IMPORTANT! In Windows 10 vagrant-proxyconf plugin will work with version 1.5.2 , 2.0.1 or later.

Provision with different Ansible playbook

If you want some else than just a simple Docker CE installation then you can override ansible_playbook to point to a different playbook in a sub directory of this repo, suggest creating directory ansible-overrides which is ignored by .gitignore and put your playbook there. You can also override the Ansible galaxy requirements file by setting variable ansible_requirements .

About

Docker on Windows using Vagrant and accessible via Ubuntu on Windows (WSL)

How to use Vagrant and Docker-for-Windows on a Hyper-V system?

Back in the day, I was using VirtualBox (VB) and Vagrant (V) to run VMs. Since I installed docker-for-windows (DfW) and run V, VB complains about Hyper-V (HV).

Although this link indicates that V supports HV, the above error message is contradictive. Why does VB look incompatible with HV, why V says that it supports HV and how to prevent that HV needs to be disabled in order to use V again.

Discussion

The suggestion to switch Hyper-V on and off is not the preferred solution as it is time consuming.

If it would be possible to run V and DfW on the same system by replacing VB this could be an option.

3 Answers 3

Confirmed by this answer on SO referencing Microsoft technet once Hyper-V is activated it mask the VT-X instructions of the processor to the OS on top of it and windows run like a guest.

Docker for Windows need hyper-V to work, so disabling it is not really an option.

There’s no way to have virtual box running on top of Hyper-V and that’s quite normal, you should not run an hypervisor within an hypervisor, they’ll fight to orchestrate hardware acces.

The best workaround in my opinion is to create hyper-V virtual machine from vagrant with the hyper-V provider instead of virtual box machines, this way you can create VM or container on the same session.

Side note, vagrant already have a note about this in the hyper-v provider documentation page (found while searching the link to add above):

Warning: Enabling Hyper-V will cause VirtualBox, VMware, and any other virtualization technology to no longer work. See this blog post for an easy way to create a boot entry to boot Windows without Hyper-V enabled, if there will be times you will need other hypervisors.

Оцените статью