github.com/0xfoo/docker@v1.8.2/docs/installation/mac.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Installation on Mac OS X"
     4  description = "Instructions for installing Docker on OS X using boot2docker."
     5  keywords = ["Docker, Docker documentation, requirements, boot2docker, VirtualBox, SSH, Linux, OSX, OS X,  Mac"]
     6  [menu.main]
     7  parent = "smn_engine"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Mac OS X
    12  
    13  > **Note**: This release of Docker deprecates the Boot2Docker command line in
    14  > favor of Docker Machine.  Use the Docker Toolbox to install Docker Machine as
    15  > well as the other Docker tools.
    16  
    17  You install Docker using Docker Toolbox. Docker Toolbox includes the following Docker tools:
    18  
    19  * Docker Machine for running the `docker-machine` binary
    20  * Docker Engine for running the `docker` binary
    21  * Docker Compose for running the `docker-compose` binary
    22  * Kitematic, the Docker GUI
    23  * a shell preconfigured for a Docker command-line environment
    24  * Oracle VM VirtualBox 
    25  
    26  Because the Docker daemon uses Linux-specific kernel features, you can't run
    27  Docker natively in OS X. Instead, you must use `docker-machine` to create and
    28  attach to a virtual machine (VM). This machine is a Linux VM that hosts Docker
    29  for you on your Mac.
    30  
    31  **Requirements**
    32  
    33  Your Mac must be running OS X 10.8 "Mountain Lion" or newer to install the
    34  Docker Toolbox.
    35  
    36  ### Learn the key concepts before installing
    37  
    38  In a Docker installation on Linux, your physical machine is both the localhost
    39  and the Docker host. In networking, localhost means your computer. The Docker
    40  host is the computer on which the containers run.
    41  
    42  On a typical Linux installation, the Docker client, the Docker daemon, and any
    43  containers run directly on your localhost. This means you can address ports on a
    44  Docker container using standard localhost addressing such as `localhost:8000` or
    45  `0.0.0.0:8376`.
    46  
    47  ![Linux Architecture Diagram](/installation/images/linux_docker_host.svg)
    48  
    49  In an OS X installation, the `docker` daemon is running inside a Linux VM called
    50  `default`. The `default` is a lightweight Linux VM made specifically to run
    51  the Docker daemon on Mac OS X. The VM runs completely from RAM, is a small ~24MB
    52  download, and boots in approximately 5s.
    53  
    54  ![OSX Architecture Diagram](/installation/images/mac_docker_host.svg)
    55  
    56  In OS X, the Docker host address is the address of the Linux VM. When you start
    57  the VM with `docker-machine` it is assigned an IP address. When you start a
    58  container, the ports on a container map to ports on the VM. To see this in
    59  practice, work through the exercises on this page.
    60  
    61  
    62  ### Installation
    63  
    64  If you have VirtualBox running, you must shut it down before running the
    65  installer. 
    66  
    67  1. Go to the [Docker Toolbox](https://www.docker.com/toolbox) page.
    68  
    69  2. Click the installer link to download.
    70  
    71  3. Install Docker Toolbox by double-clicking the package or by right-clicking
    72  and choosing "Open" from the pop-up menu.
    73  
    74      The installer launches the "Install Docker Toolbox" dialog.
    75  
    76      ![Install Docker Toolbox](/installation/images/mac-welcome-page.png)
    77  
    78  4. Press "Continue" to install the toolbox.
    79  
    80      The installer presents you with options to customize the standard
    81      installation. 
    82  
    83      ![Standard install](/installation/images/mac-page-two.png)
    84  
    85      By default, the standard Docker Toolbox installation:
    86  
    87      * installs binaries for the Docker tools in `/usr/local/bin` 
    88      * makes these binaries available to all users 
    89      * updates any existing VirtualBox installation 
    90  
    91      Change these defaults by pressing "Customize" or "Change
    92      Install Location."  
    93  
    94  5. Press "Install" to perform the standard installation.
    95  
    96       The system prompts you for your password.
    97  
    98       ![Password prompt](/installation/images/mac-password-prompt.png)
    99  
   100  6. Provide your password to continue with the installation.
   101  
   102       When it completes, the installer provides you with some information you can
   103       use to complete some common tasks.
   104  
   105       ![All finished](/installation/images/mac-page-finished.png)
   106  
   107  7. Press "Close" to exit.
   108  
   109  
   110  ## Running a Docker Container
   111  
   112  To run a Docker container, you:
   113  
   114  * create a new (or start an existing) Docker virtual machine 
   115  * switch your environment to your new VM
   116  * use the `docker` client to create, load, and manage containers
   117  
   118  Once you create a machine, you can reuse it as often as you like. Like any
   119  VirtualBox VM, it maintains its configuration between uses.
   120  
   121  There are two ways to use the installed tools, from the Docker Quickstart Terminal or
   122  [from your shell](#from-your-shell).
   123  
   124  ### From the Docker Quickstart Terminal
   125  
   126  1. Open the "Applications" folder or the "Launchpad".
   127  
   128  2. Find the Docker Quickstart Terminal  and double-click to launch it.
   129  
   130      The application:
   131  
   132      * opens a terminal window
   133      * creates a VM called `default` if it doesn't exists, starts the VM if it does
   134      * points the terminal environment to this VM
   135  
   136      Once the launch completes, the Docker Quickstart Terminal reports:
   137  
   138       ![All finished](/installation/images/mac-success.png)
   139  
   140      Now, you can run `docker` commands. 
   141  
   142  3. Verify your setup succeeded by running the `hello-world` container.
   143  
   144          $ docker run hello-world
   145          Unable to find image 'hello-world:latest' locally
   146          511136ea3c5a: Pull complete
   147          31cbccb51277: Pull complete
   148          e45a5af57b00: Pull complete
   149          hello-world:latest: The image you are pulling has been verified.
   150          Important: image verification is a tech preview feature and should not be
   151          relied on to provide security.
   152          Status: Downloaded newer image for hello-world:latest
   153          Hello from Docker.
   154          This message shows that your installation appears to be working correctly.
   155  
   156          To generate this message, Docker took the following steps:
   157          1. The Docker client contacted the Docker daemon.
   158          2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
   159             (Assuming it was not already locally available.)
   160          3. The Docker daemon created a new container from that image which runs the
   161             executable that produces the output you are currently reading.
   162          4. The Docker daemon streamed that output to the Docker client, which sent it
   163             to your terminal.
   164  
   165          To try something more ambitious, you can run an Ubuntu container with:
   166          $ docker run -it ubuntu bash
   167  
   168          For more examples and ideas, visit:
   169          http://docs.docker.com/userguide/
   170  
   171  
   172  A more typical way to interact with the Docker tools is from your regular shell command line.
   173  
   174  ### From your shell
   175  
   176  This section assumes you are running a Bash shell. You may be running a
   177  different shell such as C Shell but the commands are the same.
   178  
   179  1. Create a new Docker VM.
   180  
   181          $ docker-machine create --driver virtualbox default
   182          Creating VirtualBox VM...
   183          Creating SSH key...
   184          Starting VirtualBox VM...
   185          Starting VM...
   186          To see how to connect Docker to this machine, run: docker-machine env default
   187  
   188      This creates a new `default` in VirtualBox.
   189  
   190      ![default](/installation/images/default.png)
   191  
   192      The command also creates a machine configuration in the
   193      `~/.docker/machine/machines/default` directory. You only need to run the
   194      `create` command once. Then, you can use `docker-machine` to start, stop,
   195      query, and otherwise manage the VM from the command line.
   196  
   197  2. List your available machines.
   198  
   199          $ docker-machine ls
   200          NAME                ACTIVE   DRIVER       STATE     URL                         SWARM
   201          default             *        virtualbox   Running   tcp://192.168.99.101:2376  
   202  
   203      If you have previously installed the deprecated Boot2Docker application or
   204      run the Docker Quickstart Terminal, you may have a `dev` VM as well.  When you
   205      created `default`, the `docker-machine` command provided instructions
   206      for learning how to connect the VM.
   207  
   208  3. Get the environment commands for your new VM.
   209  
   210          $ docker-machine env default
   211          export DOCKER_TLS_VERIFY="1"
   212          export DOCKER_HOST="tcp://192.168.99.101:2376"
   213          export DOCKER_CERT_PATH="/Users/mary/.docker/machine/machines/default"
   214          export DOCKER_MACHINE_NAME="default"
   215          # Run this command to configure your shell: 
   216          # eval "$(docker-machine env default)"
   217  
   218  4. Connect your shell to the `default` machine.
   219  
   220          $ eval "$(docker-machine env default)"
   221  
   222  5. Run the `hello-world` container to verify your setup.
   223  
   224          $ docker run hello-world
   225  
   226  
   227  ## Learn about your Toolbox installation
   228  
   229  Toolbox installs the Docker Engine binary, the Docker binary on your system. When you
   230  use the Docker Quickstart Terminal or create a `default` manually, Docker
   231  Machine updates the `~/.docker/machine/machines/default` folder to your
   232  system. This folder contains the configuration for the VM.
   233  
   234  You can create multiple VMs on your system with Docker Machine. So, you may have
   235  more than one VM folder if you have more than one VM. To remove a VM, use the
   236  `docker-machine rm <machine-name>` command.
   237  
   238  ## Migrate from Boot2Docker
   239  
   240  If you were using Boot2Docker previously, you have a pre-existing Docker
   241  `boot2docker-vm` VM on your local system.  To allow Docker Machine to manage
   242  this older VM, you can migrate it.
   243  
   244  1. Open a terminal or the Docker CLI on your system.
   245  
   246  2. Type the following command.
   247  
   248          $ docker-machine create -d virtualbox --virtualbox-import-boot2docker-vm boot2docker-vm docker-vm
   249  
   250  3. Use the `docker-machine` command to interact with the migrated VM.  
   251  
   252  The `docker-machine` subcommands are slightly different than the `boot2docker`
   253  subcommands. The table below lists the equivalent `docker-machine` subcommand
   254  and what it does:
   255  
   256  |  `boot2docker` | `docker-machine` | `docker-machine` description                             |
   257  |----------------|------------------|----------------------------------------------------------|
   258  | init           | create           | Creates a new docker host.                               |
   259  | up             | start            | Starts a stopped machine.                                |
   260  | ssh            | ssh              | Runs a command or interactive ssh session on the machine.|
   261  | save           | -                | Not applicable.                                          |
   262  | down           | stop             | Stops a running machine.                                 |
   263  | poweroff       | stop             | Stops a running machine.                                 |
   264  | reset          | restart          | Restarts a running machine.                              |
   265  | config         | inspect          | Prints machine configuration details.                    |
   266  | status         | ls               | Lists all machines and their status.                     |
   267  | info           | inspect          | Displays a machine's details.                            |
   268  | ip             | ip               | Displays the machine's ip address.                       |
   269  | shellinit      | env              | Displays shell commands needed to configure your shell to interact with a machine |
   270  | delete         | rm               | Removes a machine.                                       |
   271  | download       | -                | Not applicable.                                          |
   272  | upgrade        | upgrade          | Upgrades a machine's Docker client to the latest stable release. |
   273  
   274  
   275  ## Example of Docker on Mac OS X
   276  
   277  Work through this section to try some practical container tasks on a VM. At this
   278  point, you should have a VM running and be connected to it through your shell.
   279  To verify this, run the following commands:
   280  
   281      $ docker-machine ls
   282      NAME                ACTIVE   DRIVER       STATE     URL                         SWARM
   283      dev                 *        virtualbox   Running   tcp://192.168.99.100:2376   
   284  
   285  The `ACTIVE` machine, in this case `dev`, is the one your environment is pointing to.
   286  
   287  ### Access container ports
   288  
   289  1. Start an NGINX container on the DOCKER_HOST.
   290  
   291          $ docker run -d -P --name web nginx
   292  
   293      Normally, the `docker run` commands starts a container, runs it, and then
   294      exits. The `-d` flag keeps the container running in the background
   295      after the `docker run` command completes. The `-P` flag publishes exposed ports from the
   296      container to your local host; this lets you access them from your Mac.
   297  
   298  2. Display your running container with `docker ps` command
   299  
   300          CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
   301          5fb65ff765e9        nginx:latest        "nginx -g 'daemon of   3 minutes ago       Up 3 minutes        0.0.0.0:49156->443/tcp, 0.0.0.0:49157->80/tcp   web  
   302  
   303      At this point, you can see `nginx` is running as a daemon.
   304  
   305  3. View just the container's ports.
   306  
   307          $ docker port web
   308          443/tcp -> 0.0.0.0:49156
   309          80/tcp -> 0.0.0.0:49157
   310  
   311      This tells you that the `web` container's port `80` is mapped to port
   312      `49157` on your Docker host.
   313  
   314  4. Enter the `http://localhost:49157` address (`localhost` is `0.0.0.0`) in your browser:
   315  
   316      ![Bad Address](/installation/images/bad_host.png)
   317  
   318      This didn't work. The reason it doesn't work is your `DOCKER_HOST` address is
   319      not the localhost address (0.0.0.0) but is instead the address of the
   320      your Docker VM.
   321  
   322  5. Get the address of the `dev` VM.
   323  
   324          $ docker-machine ip dev
   325          192.168.59.103
   326  
   327  6. Enter the `http://192.168.59.103:49157` address in your browser:
   328  
   329      ![Correct Addressing](/installation/images/good_host.png)
   330  
   331      Success!
   332  
   333  7. To stop and then remove your running `nginx` container, do the following:
   334  
   335          $ docker stop web
   336          $ docker rm web
   337  
   338  ### Mount a volume on the container
   339  
   340  When you start a container it automatically shares your `/Users/username` directory
   341  with the VM. You can use this share point to mount directories onto your container.
   342  The next exercise demonstrates how to do this.
   343  
   344  1. Change to your user `$HOME` directory.
   345  
   346          $ cd $HOME
   347  
   348  2. Make a new `site` directory.
   349  
   350          $ mkdir site
   351  
   352  3. Change into the `site` directory.
   353  
   354          $ cd site
   355  
   356  4. Create a new `index.html` file.
   357  
   358          $ echo "my new site" > index.html
   359  
   360  5. Start a new `nginx` container and replace the `html` folder with your `site` directory.
   361  
   362          $ docker run -d -P -v $HOME/site:/usr/share/nginx/html \
   363            --name mysite nginx
   364  
   365  6. Get the `mysite` container's port.
   366  
   367          $ docker port mysite
   368          80/tcp -> 0.0.0.0:49166
   369          443/tcp -> 0.0.0.0:49165
   370  
   371  7. Open the site in a browser:
   372  
   373      ![My site page](/installation/images/newsite_view.png)
   374  
   375  8. Try adding a page to your `$HOME/site` in real time.
   376  
   377          $ echo "This is cool" > cool.html
   378  
   379  9. Open the new page in the browser.
   380  
   381      ![Cool page](/installation/images/cool_view.png)
   382  
   383  10. Stop and then remove your running `mysite` container.
   384  
   385          $ docker stop mysite
   386          $ docker rm mysite
   387  
   388  
   389  ## Upgrade Docker Toolbox
   390  
   391  To upgrade Docker Toolbox, download an re-run [the Docker Toolbox
   392  installer](https://docker.com/toolbox/).
   393  
   394  
   395  ## Uninstall Docker Toolbox 
   396  
   397  To uninstall, do the following:
   398  
   399  1. List your machines.
   400  
   401          $ docker-machine ls
   402          NAME                ACTIVE   DRIVER       STATE     URL                         SWARM
   403          dev                 *        virtualbox   Running   tcp://192.168.99.100:2376   
   404          my-docker-machine            virtualbox   Stopped                               
   405          default                      virtualbox   Stopped  
   406  
   407  2. Remove each machine.
   408  
   409          $ docker-machine rm dev
   410          Successfully removed dev
   411  
   412       Removing a machine deletes its VM from VirtualBox and from the
   413       `~/.docker/machine/machines` directory.
   414  
   415  3. Remove the Docker Quickstart Terminal and Kitematic from your "Applications" folder.
   416  
   417  4. Remove the `docker`, `docker-compose`, and `docker-machine` commands from the `/usr/local/bin` folder.
   418  
   419          $ rm /usr/local/bin/docker
   420  
   421  5. Delete the `~/.docker` folder from your system.
   422  
   423  
   424  ## Learning more 
   425  
   426  Use `docker-machine help` to list the full command line reference for Docker Machine. For more
   427  information about using SSH or SCP to access a VM, see [the Docker Machine
   428  documentation](https://docs.docker.com/machine/).
   429  
   430  You can continue with the [Docker User Guide](/userguide). If you are
   431  interested in using the Kitematic GUI, see the [Kitermatic user
   432  guide](/kitematic/userguide/).