Share folders and bind ports between a docker container and windows 7

In one of the projects I work on I need an apache http server installed locally on my windows development system. The installation and additional configuration are documented in our confluence but I still need to do them manually. One way to automatically install and configure apache to do that job is to use docker. I still use windows 7 on my laptop so the only way for me to have docker was to install docker toolbox. In this case docker engine is installed in a boot2docker vm that runs in virtualbox. Docker documentation describes docker volumes and port binding, but on windows it is very easy to forget that the host is not the windows system but the boot2docker vm. In the next lines I’ll describe what you have to do to share folders and bind ports between a docker container and windows 7.

Configure shared folders between boot2docker vm and windows

Open virtualbox, find boot2docker vm and click on settings.

virtualbox-default-image

Click on Shared Folders and add a new one. Folder Name will be used to mount the folder in boot2docker vm.

virtualbox-default-settings-sharedfolders-image

virtualbox-default-settings-sharedfolders-add-image

I checked Make Permanent option to have the shared folder available after boot2docker vm reboot and I checked Auto-mount to have it automatically mounted. Unfortunately Auto-mount does not work for boot2docker vm and I have to mount it after each reboot.

Open Docker Quickstart Terminal and run the following commands:

Shared folder Work is mounted now in /media/HostVM-Work folder in boot2docker vm.

Create a file in Work folder in windows. I created a text file named windows.txt.

Do a ssh in boot2docker vm and check the content of the shared folder.

You should see the windows.txt file created previously. Create a file in boot2docker vm and check the folder again.

You should see both files now.

docker-machine-ssh-shared-folder-image

Configure port binding between boot2docker vm and windows

Open virtualbox, find boot2docker vm and click on settings.

virtualbox-default-image

Click on Network, expand Advanced and click on Port Forwarding. I bound boot2docker vm ports 80 and 443 (Guest port) to windows ports 80 and 443 (Host port).

virtualbox-default-settings-network-image

virtualbox-default-settings-network-portforwardingrules-image

Share folders and bind ports between a docker container and boot2docker vm

This can be done with command line arguments when running the container.

This command starts a container with ubuntu:16.10 image, binds boot2docker vm port 80 to port 80 of the container and mounts /media/HostVM-Work boot2docker vm’s folder to /media/Container-Work container’s folder. You can verify the folder with the following commands.

You should see windows.txt and hostvm.txt files created previously. Create another file in /media/Container-Work folder and check it again.

You should see all three files now.

container-shared-folder-image

In order to verify port binding you need to install a server that listens on port 80.

Open a browser in windows and type localhost. You should see the default web page displayed by apache.

container-forward-port-80-windows-image

These are steps needed to share folders and bind ports between a docker container and windows 7, please let me know if you encounter any issues following them.