Loading...
Loading...
Marco Franssen

In this post, I want to show you how to set up a Docker development environment on Windows or Mac using Docker Machine and VirtualBox. Docker's Linux containers require a Linux kernel, so this setup uses a VM to run the Docker environment. Here are a few quotes from the Docker website to remind you what Docker is all about, or to give you a quick introduction.
Docker provides a common framework for developer and IT teams to collaborate on applications. With a clear separation of concerns and robust tooling, organizations are able to innovate faster with greater control.
AGILITY
Developers have the freedom to define environments, and the ability to create and deploy apps faster and easier. IT ops have the flexibility to quickly respond to change.
CONTROL
Developers own all the code from infrastructure to app. IT ops have the manageability to standardize, secure, and scale the operating environment.*
PORTABILITY
Docker gives you choice without complexity, from a laptop to a team, to private infrastructure and public cloud providers.
OK, enough chatting. If you're impatient, skip to TL;DR. Otherwise, let's keep reading.
Let's see how we can get an environment running on Windows or Mac. In this tutorial, I'm going to use Chocolatey, a package manager for Windows. It lets you install software from PowerShell, which makes it handy for scripting your computer's setup. If you're unfamiliar with Chocolatey, I recommend visiting the Chocolatey website and following its installation instructions.
For Mac users, I'll point out where the approach differs slightly. Otherwise, you can run the same commands. On Mac, I'll use Homebrew, a tool similar to Chocolatey. If you're unfamiliar with it, have a look at Homebrew to learn how to set it up, or install the packages manually.
From here, I'll assume you have Chocolatey or Homebrew installed, or know how to install the required tools manually by downloading the appropriate installers. We'll use docker-machine to set up a VM in VirtualBox for our Docker environment. Let's start by installing VirtualBox, Docker Machine, and Docker using Chocolatey. If you already have these tools installed, you can skip this step.
cinst -y virtualbox
cinst -y docker-machine
cinst -y dockerbrew install docker-machine
brew install dockerNow that we've installed Docker Machine, we can create a new environment. If you already had it installed, first check which machines are available with the following command.
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.11.2In my case, I've already created a machine. If you also have one, you may want to use it. If you're starting from scratch, here's how to create one.
$ docker-machine create --driver virtualbox default
Running pre-create checks...
Creating machine...
(staging) Copying /Users/ripley/.docker/machine/cache/boot2docker.iso to /Users/ripley/.docker/machine/machines/default/boot2docker.iso...
(staging) Creating VirtualBox VM...
(staging) Creating SSH key...
(staging) Starting the VM...
(staging) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect Docker to this machine, run: docker-machine env defaultNote: To boot your machine, run the following command. If it's already running, you can skip this step.
docker-machine startThe previous command created a machine named default using the virtualbox driver. When you run docker-machine ls now, you should see output similar to the earlier example, including whether the machine is running. The last line of the creation output also points to the command that shows how to connect your Docker client to this machine (docker-machine env default). Let's configure the PowerShell environment to connect to it.
$ docker-machine env default
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.100:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\marco\.docker\machine\machines\default"
$Env:DOCKER_MACHINE_NAME = "default"
# Run this command to configure your shell:
# & "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env default | Invoke-ExpressionRemember that this command produces different output on Mac, in Bash or Cygwin on Windows, and in the regular Windows Command Prompt. Run it in your preferred shell, then execute the command shown in the output. I'll continue using PowerShell and run the command we've just seen.
Let's configure PowerShell.
& "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env default | Invoke-ExpressionFor Mac users running Bash, the command will look like this.
eval $(docker-machine env default)Now we can use Docker from our shell. Let's run the docker/whalesay image to test the environment.
$ docker run docker/whalesay cowsay "Awesome your Docker environment is running!"
____________________________________
/ Awesome your Docker environment is \
\ running! /
------------------------------------
\
\
\
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/I won't go much deeper into Docker itself in this article. Creating your own images, running multi-container applications, and other in-depth topics are outside its scope. For now, try docker --help in your shell and have a look at the Docker documentation, so I don't leave you completely in the dark. I might publish another post on running your project in Docker later.
The docker/whalesay image also has command-line help that reveals more options (docker run docker/whalesay cowsay -h). For example, the whale can think instead of talk (docker run docker/whalesay cowthink "Will he see the difference?"). Have fun playing with these options, or try some other images from Docker Hub.
When you're finished playing with Docker, you may want to shut down the VM using docker-machine stop default. Next time, run docker-machine start default and docker-machine env default to start it and get the shell configuration instructions. Last but not least, try the --help option for usage information about each command and its options.
docker-machine --help
docker-machine create --helpAs a bonus, here are the commands for setting up our environment in a PowerShell script. Feel free to save them in a ps1 file and run it from PowerShell.
cinst -y docker-machine
cinst -y docker
docker-machine create --driver virtualbox default
docker-machine start
& "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env default | Invoke-Expression
docker run docker/whalesay cowsay -W 19 "Awesome your Docker environment is running!"brew install docker-machine
brew install docker
docker-machine create --driver virtualbox default
docker-machine start
eval $(docker-machine env default)
docker run docker/whalesay cowsay -W 19 "Awesome your Docker environment is running!"Finally, when you're finished playing with Docker, you may want to shut down your VM. Run:
docker-machine stopIf you skipped straight to the commands and now realize you have no clue what just happened, here's your second chance.
References:
Thanks again for reading! As always, share your feedback and questions in the comments.
Marco Franssen
Learn how to use Docker Compose to build and run a multi-container application, connect services, and organize development with Git submodules.
Marco Franssen
Package a static Angular app in an Nginx Docker image, build it, and run it with port and log volume mappings in this historical 2016 walkthrough.
Marco Franssen
Learn how to replace Node.js error-first callbacks with Promises, flatten nested asynchronous code, and wrap callback APIs with practical examples.
Marco Franssen
Use the original Let's Encrypt client in a Vagrant VM to request an Azure website certificate through a reverse proxy, then export and upload a PFX.