Wednesday, January 13, 2016

Docker: Up and Running on OS X (deprecated)

Update 13-July-2016: This is an old article.  Use Docker Machine going forward.

Introduction

 The good news is, docker is getting easier.

This time last year, if you wanted to install docker on your Macbook, there were a few hoops to jump through. Now it's easier than ever. Much of the s implication has come through the use of Docker Machine.

The Docker toolbox installer works well. Download, point and click, and set up. The initial launch performs a pre-create check, and then automatically creates a boot-2-docker machine on your computer.


Post-Installation

In Spotlight search, start typing "docker ... "
Launch the terminal.

If this is the first time running a docker terminal, it may take a few minutes to set up.

Successful output will look something like this:
Running pre-create checks...
Creating machine...
(default) Copying /Users/craigtrim/.docker/machine/cache/boot2docker.iso to /Users/craigtrim/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting VM...
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: /usr/local/bin/docker-machine env default

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/


docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

~/workspaces $


Once you have the terminal accessible, the environment variables need to be established.

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


and then follow the instructions to run:
$ eval $(docker-machine env default)
on the terminal.

This will set environment variables that the Docker client will read which specify the TLS settings. Note that you will need to do that every time you open a new tab or restart your machine.


Testing the Installation


From there, I can run this tutorial, and get:
~/workspaces$ docker build -t mybase .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
latest: Pulling from library/ubuntu

fcee8bcfe180: Pull complete 
4cdc0cbc1936: Pull complete 
d9e545b90db8: Pull complete 
c4bea91afef3: Pull complete 
Digest: sha256:b53bb7b0d18842214ac7472c2a8801e8682c247d30f1ba4bab0083a2e2e091ea
Status: Downloaded newer image for ubuntu:latest
 ---> c4bea91afef3
Step 2 : RUN touch myfile.txt
 ---> Running in 745e3d4c316b
 ---> f5644fc04a54
Removing intermediate container 745e3d4c316b
Successfully built f5644fc04a54



Troubleshooting

  1. Error checking TLS connection: machine does not exist
    1. Occurs if the environment variables were not set correctly use docker machine
    2. Last login: Wed Jan 13 12:11:02 on ttys005
      ~/workspaces$ docker ps
      Cannot connect to the Docker daemon. Is the docker daemon running on this host?
      ~/workspaces$ docker-machine env default
      export DOCKER_TLS_VERIFY="1"
      export DOCKER_HOST="tcp://192.168.99.100:2376"
      export DOCKER_CERT_PATH="/Users/craigtrim/.docker/machine/machines/default"
      export DOCKER_MACHINE_NAME="default"
      # Run this command to configure your shell: 
      # eval $(docker-machine env default)
      ~/workspaces$ eval $(docker-machine env default)
      ~/workspaces$ docker ps
      CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
      ~/workspaces$ 
      
  2. Error checking TLS connection: Error checking and/or regenerating the certs
    1. This appears to crop up from time to time on the Mac. 
    2. I took three actions to mitigate this - the first was to regenerate the certificates, but this did not appear to work:
                              ##         .
                        ## ## ##        ==
                     ## ## ## ## ##    ===
                 /"""""""""""""""""\___/ ===
            ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
                 \______ o           __/
                   \    \         __/
                    \____\_______/
      
      
      docker is configured to use the default machine with IP 192.168.99.100
      For help getting started, check out the docs at https://docs.docker.com
      
      ]~$ docker-machine env default
      Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.99.100:2376": dial tcp 192.168.99.100:2376: i/o timeout
      You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'.
      Be advised that this will trigger a Docker daemon restart which will stop running containers.
      
      ~$ docker-machine regenerate-certs default
      Regenerate TLS machine certs?  Warning: this is irreversible. (y/n): y
      Regenerating TLS certificates
      Detecting the provisioner...
      Copying certs to the local machine directory...
      Copying certs to the remote machine...
      Setting Docker configuration on the remote daemon...
      
      This machine has been allocated an IP address, but Docker Machine could not
      reach it successfully.
      
      SSH for the machine should still work, but connecting to exposed ports, such as
      the Docker daemon port (usually <ip>:2376), may not work properly.
      
      You may need to add the route manually, or use another related workaround.
      
      This could be due to a VPN, proxy, or host file configuration issue.
      
      You also might want to clear any VirtualBox host only interfaces you are not using.
      ~$ 
      
    3. The second action was to open up VirtualBox and removing the host interfaces, in the event there was a conflict:

      I removed both of the Host-only networks listed above.
    4. The third and final action to restart my laptop, and try again with my Cisco VPN client disabled.  
      1. I've had trouble in the past with Cisco VPN and Docker on the Mac.  The VPN client does not allow split-tunneling, and this can cause issue with containers.  
      2. Having said that, I am using Docker and the Cisco VPN together now without any apparent issues.  



References

  1. https://github.com/docker/toolbox/issues/346
    1.  Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates #346

4 comments: