Deploying local Docker hosts with Docker Machine

Before we journey out into the cloud, we are going to look at the basics of Docker Machine locally by launching it, using Oracle VirtualBox to provide the VM.

To launch the machine, all you need to do is run the following command:

$ docker-machine create --driver virtualbox docker-local

This will start the deployment, during which you will get a list of tasks that Docker Machine is running. To launch your Docker host, each host launched with Docker Machine goes through the same steps.

First of all, Docker Machine runs a few basic checks, such as confirming that VirtualBox is installed:

Running pre-create checks...

Once the checks have passed, it creates the virtual machine using the selected driver:

Creating machine...
(docker-local) Copying /Users/russ/.docker/machine/cache/boot2docker.iso to /Users/russ/.docker/machine/machines/docker-local/boot2docker.iso...
(docker-local) Creating VirtualBox VM...
(docker-local) Creating SSH key...
(docker-local) Starting the VM...
(docker-local) Check network to re-create if needed...
(docker-local) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...

As you can see, Docker Machine creates a unique SSH key for the virtual machine, this means that you will be able to access the virtual machine over SSH, more on that later. Once the virtual machine has booted, Docker Machine then makes a connection to the virtual machine:

Detecting operating system of created instance...
Waiting for SSH to be available...
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...

As you can see, Docker Machine detects the operating system being used and chooses the appropriate bootstrap script to deploy Docker. Once Docker is installed, Docker Machine generates and shares certificates between your local host and the Docker host. It then configures the remote Docker installation for certificate authentication, meaning that your local client can connect to and interact with the remote Docker server:

Once Docker is installed, Docker Machine generates and shares certificates between your local host and the Docker host, it then configures the remote Docker installation for certificate authentication meaning that your local client can connect to and interact with the remote Docker server:

Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env docker-local

Finally, it checks whether your local Docker client can make the remote connection and completes the task by giving you instructions on how to configure your local client to the newly launched Docker host.

If you open VirtualBox, you should be able to see your new virtual machine:

Next, we need to configure our local Docker client to connect to the newly launched Docker host; as already mentioned in the output of launching the host, running the following command will show you how to make the connection:

$ docker-machine env docker-local

This returns the following:

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/russ/.docker/machine/machines/docker-local"
export DOCKER_MACHINE_NAME="docker-local"
# Run this command to configure your shell:
# eval $(docker-machine env docker-local)

As you can see, this overrides the local Docker installation by giving the IP address and port number of the newly launched Docker host as well as the path to the certificates used for authentication. At the end of the output, it gives you a command to run and to configure your terminal session to make the connection.

Before we run the command, let's run docker version to get information on the current setup:

As you can see, this is basically the Docker for Mac installation I am running. Running the following command and then docker version again should show some changes to the server:

$ eval $(docker-machine env docker-local)

The output of the command is given here:

As you can see, the version of Docker has changed, along with the API version and build date. From here, we can interact with the Docker host in the same way as if it were a local Docker installation.

Before we move on to launching Docker hosts in the cloud, there are a few other basic Docker Machine commands to cover.

The first is listing the currently configured Docker hosts:

$ docker-machine ls

The output of the command is given here:

As you can see, it lists the details on the machine name, the driver used and the Docker endpoint URL, and the version of Docker the hosts are running.

You will also notice that there is a * in the ACTIVE column; this indicates which Docker host your local client is currently configured to interact with. You can also find out the active machine by running docker-machine active.

The next command connects you to the Docker host using SSH:

$ docker-machine ssh docker-local

The output of the command is given here:

This is useful if you need to install additional software or configuration outside of Docker Machine. It is also useful if you need to look at logs and so on. You can find out the IP address of your Docker host by running the following:

$ docker-machine ip docker-local

We will be using this a lot later in the chapter. There are also commands to stop, start, restart, and remove your Docker host, though for now let's keep it running:

$ docker-machine stop docker-local
$ docker-machine start docker-local
$ docker-machine restart docker-local
$ docker-machine rm docker-local

There are also commands to find out more details about your Docker host:

$ docker-machine inspect docker-local
$ docker-machine config docker-local
$ docker-machine status docker-local
$ docker-machine url docker-local

Now that we have had a very quick rundown of the basics, let's try something more adventurous.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset