Menu
Estimated reading time: 13 minutes ![Off Off](/uploads/1/2/6/3/126301622/292030686.png)
Let’s take a look at using
docker-machine
to create, use and manage aDocker host inside of a local virtual machine.Before you can start playing with Oracle VM VirtualBox, you have to install it on your host operating system (the latest release of Oracle VM VirtualBox can be found on the Oracle VM VirtualBox downloads page). It installs on any of the following operating systems: Windows, Mac OS X, Oracle Solaris, and Linux.
Prerequisite information
With the advent of Docker Desktop for Mac and Docker Desktop forWindows as replacements for DockerToolbox, we recommend that you use these for your primaryDocker workflows. You can use these applications to run Docker natively on yourlocal system without using Docker Machine at all. (See Docker Desktop for Mac vs.Docker Toolbox for an explanation on the Macside.)
For now, however, if you want to create multiple local machines, you stillneed Docker Machine to create and manage machines for multi-nodeexperimentation. Both Docker Desktop for Mac and Docker Desktop for Windows include the newestversion of Docker Machine, so when you install either of these, you get
docker-machine
.The new solutions come with their own native virtualization solutions ratherthan Oracle VirtualBox, so keep the following considerations in mind when usingMachine to create local VMs.
- Docker Desktop for Mac - You can use
docker-machine create
with thevirtualbox
driver to create additional local machines. - Docker Desktop for Windows - You can use
docker-machine create
with thehyperv
driver to create additional local machines.
If you are using Docker Desktop for Windows
Docker Desktop for Windows uses MicrosoftHyper-Vfor virtualization, and Hyper-V is not compatible with Oracle VirtualBox.Therefore, you cannot run the two solutions simultaneously. But you can stilluse
docker-machine
to create more local VMs by using the Microsoft Hyper-Vdriver.The prerequisites are:
- Have Docker Desktop for Windows installed, and running (which requires that virtualization and Hyper-V are enabled, as described in What to know before you install Docker Desktop for Windows).
- Set up the Hyper-V driver to use an external virtual network switch Seethe Docker Machine driver for Microsoft Hyper-V topic,which includes an example of how to do this.
If you are using Docker Desktop for Mac
Docker Desktop for Mac uses HyperKit, alightweight macOS virtualization solution built on top of theHypervisor.framework.
Currently, there is no
docker-machine create
driver for HyperKit, souse the virtualbox
driver to create local machines. (See the Docker Machinedriver for Oracle VirtualBox.) You can runboth HyperKit and Oracle VirtualBox on the same system. To learn more, seeDocker Desktop for Mac vs. Docker Toolbox.- Make sure you have the latest VirtualBoxcorrectly installed on your system (either as part of an earlier Toolbox install,or manual install).
If you are using Docker Toolbox
Docker Desktop for Mac and Docker Desktop for Windows both require newer versions of theirrespective operating systems, so users with older OS versions must use DockerToolbox.
- If you are using Docker Toolbox on either Mac or an older version Windows system (without Hyper-V), use the
virtualbox
driver to create a localmachine based on Oracle VirtualBox. (See the Docker Machine driver for OracleVirtualBox.) - If you are using Docker Toolbox on a Windows system that has Hyper-V but cannot run Docker Desktop for Windows (for example Windows 8 Pro), you must use the
hyperv
driver to create local machines. (See the Docker Machine driver forMicrosoft Hyper-V.) - Make sure you have the latest VirtualBoxcorrectly installed on your system. If you usedToolboxor Docker Desktop for Windowsto install Docker Machine, VirtualBox isautomatically installed.
- If you used the Quickstart Terminal to launch your first machine and set yourterminal environment to point to it, a default machine was automaticallycreated. If so, you can still follow along with these steps, butcreate another machine and name it something other than
default
.
Use Machine to run Docker containers
To run a Docker container, you:
- create a new (or start an existing) Docker virtual machine
- switch your environment to your new VM
- use the docker client to create, load, and manage containers
Once you create a machine, you can reuse it as often as you like. Like any VirtualBox VM, it maintains its configuration between uses.
The examples here show how to create and start a machine, run Docker commands, and work with containers.
Create a machine
- Open a command shell or terminal window.These command examples shows a Bash shell. For a different shell, such as C Shell, the same commands are the same except where noted.
- Use
docker-machine ls
to list available machines.In this example, no machines have been created yet. - Create a machine.Run the
docker-machine create
command, pass the appropriate driver to the--driver
flag and provide a machine name. If this is your first machine, nameitdefault
as shown in the example. If you already have a “default” machine,choose another name for this new machine.- If you are using Toolbox on Mac, Toolbox on older Windows systems without Hyper-V, or Docker Desktop for Mac, use
virtualbox
as the driver, as shown in this example. (The Docker Machine VirtualBox driver reference is here.) (See prerequisites above to learn more.) - On Docker Desktop for Windows systems that support Hyper-V, use the
hyperv
driver as shown in the Docker Machine Microsoft Hyper-V driver reference and follow the example, which shows how to use an external network switch and provides the flags for the full command. (See prerequisites above to learn more.)This command downloads a lightweight Linux distribution (boot2docker) with the Docker daemon installed, and creates and starts a VirtualBox VM with Docker running.
- List available machines again to see your newly minted machine.
- Get the environment commands for your new VM.As noted in the output of the
docker-machine create
command, you need to tell Docker to talk to the new machine. You can do this with thedocker-machine env
command. - Connect your shell to the new machine.Note: If you are using
fish
, or a Windows shell such as Powershell/cmd.exe
, the above method does not work as described. Instead, see theenv
command’s documentation to learn how to set the environment variables for your shell.This sets environment variables for the current shell that the Docker client reads which specify the TLS settings. You need to do this each time you open a new shell or restart your machine. (See also, how to unset environment variables in the current shell.)You can now run Docker commands on this host.
Run containers and experiment with Machine commands
Run a container with
docker run
to verify your set up.- Use
docker run
to download and runbusybox
with a simple ‘echo’ command. - Get the host IP address.Any exposed ports are available on the Docker host’s IP address, which you can get using the
docker-machine ip
command: - Run a Nginx webserver in a container with the following command:When the image is finished pulling, you can hit the server at port 8000 on the IP address given to you by
docker-machine ip
. For instance:
You can create and manage as many local VMs running Docker as your local resources permit; just run
docker-machine create
again. All created machines appear in the output of docker-machine ls
.Start and stop machines
If you are finished using a host for the time being, you can stop it with
docker-machine stop
and later start it again with docker-machine start
.Operate on machines without specifying the name
Some
docker-machine
commands assume that the given operation should be run on a machine named default
(if it exists) if no machine name is specified. Because using a local VM named default
is such a common pattern, this allows you to save some typing on the most frequently used Machine commands.For example:
Commands that follow this style are:
For machines other than
default
, and commands other than those listed above, you must always specify the name explicitly as an argument.Unset environment variables in the current shell
![Off Off](/uploads/1/2/6/3/126301622/292030686.png)
You might want to use the current shell to connect to a different Docker Engine.This would be the case if, for example, you are running Docker Desktop for Macconcurrent with Docker Toolbox and want totalk to two different Docker Engines.In both scenarios, you have the option to switch the environment for the currentshell to talk to different Docker engines.
- Run
env|grep DOCKER
to check whether DOCKER environment variables are set.If it returns output (as shown in the example), you can unset theDOCKER
environment variables. - Use one of two methods to unset DOCKER environment variables in the current shell.
- Run the
unset
command on the followingDOCKER
environment variables. - Alternatively, run a shortcut command
docker-machine env -u
to show the command you need to run to unset all DOCKER variables:Runeval $(docker-machine env -u)
to unset all DOCKER variables in the current shell.
- Now, after running either of the above commands, this command should return no output.If you are running Docker Desktop for Mac, you can run Docker commands to talk to the Docker Engine installed with that app.Since Docker Desktop for Windows is incompatible with Toolbox, this scenario isn’t applicable because Docker Desktop for Windows uses the Docker Engine and Docker Machine that come with it.
Start local machines on startup
To ensure that the Docker client is automatically configured at the start ofeach shell session, you can embed
eval $(docker-machine env default)
in yourshell profiles, by adding it to the ~/.bash_profile
file or the equivalentconfiguration file for your shell. However, this fails if a machine calleddefault
is not running. You can configure your system to start the default
machine automatically. The following example shows how to do this in macOS.Create a file called
com.docker.machine.default.plist
in the~/Library/LaunchAgents/
directory, with the following content:You can change the
default
string above to make this LaunchAgent
start adifferent machine.Where to go next
- Provision multiple Docker hosts on your cloud provider