Few folks recently asked me how to try out Docker in Windows machine to get familiar with Docker and Containers. This is not the same as running Docker engine in Windows, there is some active work going on to achieve this. The easiest way to try out Docker is to use Docker Toolbox. I covered Toolbox in one of my earlier blogs. In this blog, I will provide the steps to create a simple environment to play with Docker engine, Swarm, Compose and Networking in Windows.
- Install Docker Toolbox from here.
- Docker Toolbox 1.9 version has the following components(Docker client, Docker machine, Virtualbox, Kitematic, Compose, Git). If you already have Virtualbox and Git bash for Windows, you can skip the installation of these 2.
- In this blog, I will use Docker CLI, so Kitematic is not needed.
- I have executed commands below from git bash shell.
Following are the versions of Docker components that gets installed with Toolbox 1.9:
$ docker --version Docker version 1.9.1, build a34a1d5 $ docker-machine --version docker-machine.exe version 0.5.6, build 61388e9 $ docker-compose --version docker-compose version 1.5.2, build e5cf49d
Run the following script to setup a Consul server and 2 node Docker cluster with Swarm pre-installed:
docker-machine create -d virtualbox mh-keystore docker $(docker-machine config mh-keystore) run -d \ -p "8500:8500" \ -h "consul" \ progrium/consul -server -bootstrap docker-machine create \ -d virtualbox \ --swarm --swarm-master \ --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \ --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \ --engine-opt="cluster-advertise=eth1:2376" \ mhs-demo0 docker-machine create -d virtualbox \ --swarm \ --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \ --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \ --engine-opt="cluster-advertise=eth1:2376" \ mhs-demo1
Consul runs in “mh-keystore” VM and is used as a key value store for the cluster nodes to communicate to each other. “mhs-demo0” and “mhs-demo1” are the 2 VMs with Docker engine and Swarm installed.
After this, we can set the environment to Swarm master and start deploying Containers. Swarm will take care of scheduling the Containers into 1 of the nodes based on the default scheduling logic.
eval $(docker-machine env --shell sh --swarm mhs-demo0)
To deploy this multi-host application below:
web: image: bfirsh/compose-mongodb-demo environment: - "MONGO_HOST=counter_mongo_1" ports: - "80:5000" mongo: image: mongo
We can simply do:
docker-compose --x-networking --x-network-driver=overlay --project-name=counter up -d
The above command will create Overlay network and create web Container in 1 host and db Container in another host. web and db Containers across the 2 hosts will be able to communicate with each other using the Overlay network.
To cleanup the environment, we can do:
docker-machine rm -f mh-keystore docker-machine rm -f mhs-demo0 docker-machine rm -f mhs-demo1
If we want to reuse the environment later, we can just stop the VMs like below and restart it later.
docker-machine stop mh-keystore docker-machine stop mhs-demo0 docker-machine stop mhs-demo1
I hit this issue once in a while and the workaround is to delete the host-only interface in Virtualbox as mentioned in the link. We can do this from File->Preferences in Virtualbox.