Getting started with Docker in Windows with Volumes and VirtualBox Shared Folder

dockerstartertest

IMPORTANT NOTE

  • Be specially careful about “CR” and “LF” in Windows formatted text files. If you have a shell script that was created in the Windows environment, take special care to remove all “CR” instances from the file. Linux will not be able to run shell script if it contains “CR” characters. Using Notepad++ you can convert a Windows formatted file into Unix easily. Open Notepad++ and go to menu “Edit”>”EOL Conversion”>”UNIX/OSX Format”.
  • Be careful about volume name path mapping between Windows and your virtual containers. Unix is case sensitive and Windows is not. When you declare volume information in “docker run” or in “docker-compose” commands use exact path with proper cases.

Introduction

This “how to” will show how to install Docker on a Windows machine with a super simple static web site. We will use a simple Docker image for Apache. This tutorial is very simple to follow and easy to build and test. The only thing you have to be careful with is sharing folders between your hosts, guests, and containers. Docker machines are Unix based machines and run native on Linux machines. Developers have to do extra steps to expose Windows folders into Docker containers and images. This extra steps are needed because Windows runs Docker machine using a virtual environment like VirtualBox.

The virtual path in this tutorial is relative to my directory settings in my Windows environment. It may be different in your environment. You should be able to add your own custom path just by changing the “-v” switch when you invoke docker run. Everything else, including adding a shared volume in VirtualBox machine, should be standard. There are a lot of different ways to share folders but this tutorial shows a convention that would reduce the developer’s pain by eliminating modification to build files and runner commands as much as possible.

Docker Books

ortant;” />

Software Installs

Install Git for Windows

Install Git for Windows project. Docker may complain about not finding the ‘ssh’ command. If it does, then install Git for Windows project and have Git binaries in your Windows “PATH” environment variable.

Install Oracle VirtualBox

I don’t clearly remember if Docker will install VirtualBox for you or if you have to install VirtualBox prior to installing Docker ToolBox. To be on the safe, install VirtualBox. If Docker ToolBox later insists on installing VirtualBox just don’t install it again.
Download and install VirtualBox for Windows Host

Install Docker with Docker ToolBox

  • Download Docker ToolBox from here
  • Install Docker ToolBox
  • Start “Docker Quickstart Terminal”

Docker Terminal

  • Test Docker by running

    docker run hello-world

  • A successful run of hello-world project will produce a screen similar to this one:
    Docker hello world

Prepare Project Folder

For this tutorial we would assume our project folder is located on the drive D: . Now we will create a project folder using Windows Explorer as shown below.

 D:\projects\bitbrushco

Clone Git Project

Open “Docker Quickstart Terminal”. Change directory to /d/Projects/BitBrush

Clone test Git project using the following command (shown below). You can also use any other Windows Git client like TortoisGit or SourceTree.

$ git clone https://github.com/BitBrushCo/dockerstartertest.git

Share Windows Project Folder from VirtualBox

Open VirtualBox GUI. There should be a VM named “default”. This is the VM created by Docker. This “default” VM acts the docker-machine which uses boot2docker to simulate the Docker host machine. Docker containers run off of the docker-machine. We will need to setup our project folder to be accessible by the docker-machine. By default, the docker-machine (aka boot2docker) mounts some specifically named shared folder if it finds it through VirtualBox’s shared folder. More information about boot2docker and VirtualBox shared folder can be found here. Here we provide an excerpt related to shared folder.

The first of the following share names that if they exist they will be automatically mounted at the location specified:

  1. Users share at /Users
  2. /Users share at /Users
  3. c/Users share at /c/Users
  4. /c/Users share at /c/Users
  5. c:/Users share at /c/Users

You will use /Users share and map it to D:\Projects folder. “D:\” drive. This arrangement makes it easier and more convenient for developers to share and update their source code between Windows and Docker containers.

VirtualBox Shared Folder

Docker Run

Now we will try to run a container and map our project folder which will contain a index.html page. The page will be served directly from the container.

  • First make sure the container has been stopped.
    docker stop $(docker ps -q)

  • Remove container
    docker rm $(docker ps -aq)

  • Start docker machine
    docker-machine start

  • Run docker. This command is supposed to start an apache session.
    docker run -it --rm --name myapache -p 8080:80 -v /Users/projects/bitbrushco/dockerstartertest:/usr/local/apache2/htdocs/ httpd:2.4

  • Test apache by opening a browser and trying out http://192.168.99.100:8080/

NOTE:
The run statement is simple. We have mapped our Windows folder that contains index.html file as “D:\projects\bitbrushco\dockerstartertest”. Docker machine (i.e. boot2docker) sees the directory as “/Users/projects/bitbrushco/dockerstartertest”. Remember that in previous steps we shared D:\ drive to /Users in VirtualBox. That step enabled boot2docker vm (the docker host vm) to use the directory as volumes (i.e. the -v switch) to the running Docker container vm. This in turn runs inside another vm boot2docker. This can be confusing since it involves multiple layers of nested virtual machines. Try it with a different working folder and try to expose it to your container and this will be clear to you eventually.

  • This concludes a successful install test of Docker environment in your machine.

Add a Comment

Your email address will not be published. Required fields are marked *