github.com/tompao/docker@v1.9.1/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](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](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](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](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      * installs VirtualBox; or updates any existing 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](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](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 `default` VM if it doesn't exists, and starts the VM after
   134      * points the terminal environment to this VM
   135  
   136      Once the launch completes, the Docker Quickstart Terminal reports:
   137  
   138       ![All finished](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` VM in VirtualBox.
   189  
   190      The command also creates a machine configuration in the
   191      `~/.docker/machine/machines/default` directory. You only need to run the
   192      `create` command once. Then, you can use `docker-machine` to start, stop,
   193      query, and otherwise manage the VM from the command line.
   194  
   195  2. List your available machines.
   196  
   197          $ docker-machine ls
   198          NAME                ACTIVE   DRIVER       STATE     URL                         SWARM
   199          default             *        virtualbox   Running   tcp://192.168.99.101:2376
   200  
   201      If you have previously installed the deprecated Boot2Docker application or
   202      run the Docker Quickstart Terminal, you may have a `dev` VM as well.  When you
   203      created `default` VM, the `docker-machine` command provided instructions
   204      for learning how to connect the VM.
   205  
   206  3. Get the environment commands for your new VM.
   207  
   208          $ docker-machine env default
   209          export DOCKER_TLS_VERIFY="1"
   210          export DOCKER_HOST="tcp://192.168.99.101:2376"
   211          export DOCKER_CERT_PATH="/Users/mary/.docker/machine/machines/default"
   212          export DOCKER_MACHINE_NAME="default"
   213          # Run this command to configure your shell:
   214          # eval "$(docker-machine env default)"
   215  
   216  4. Connect your shell to the `default` machine.
   217  
   218          $ eval "$(docker-machine env default)"
   219  
   220  5. Run the `hello-world` container to verify your setup.
   221  
   222          $ docker run hello-world
   223  
   224  
   225  ## Learn about your Toolbox installation
   226  
   227  Toolbox installs the Docker Engine binary, the Docker binary on your system. When you
   228  use the Docker Quickstart Terminal or create a `default` VM manually, Docker
   229  Machine updates the `~/.docker/machine/machines/default` folder to your
   230  system. This folder contains the configuration for the VM.
   231  
   232  You can create multiple VMs on your system with Docker Machine. Therefore, you
   233  may end up with multiple VM folders if you have more than one VM. To remove a
   234  VM, use the `docker-machine rm <machine-name>` command.
   235  
   236  ## Migrate from Boot2Docker
   237  
   238  If you were using Boot2Docker previously, you have a pre-existing Docker
   239  `boot2docker-vm` VM on your local system.  To allow Docker Machine to manage
   240  this older VM, you can migrate it.
   241  
   242  1. Open a terminal or the Docker CLI on your system.
   243  
   244  2. Type the following command.
   245  
   246          $ docker-machine create -d virtualbox --virtualbox-import-boot2docker-vm boot2docker-vm docker-vm
   247  
   248  3. Use the `docker-machine` command to interact with the migrated VM.
   249  
   250  The `docker-machine` subcommands are slightly different than the `boot2docker`
   251  subcommands. The table below lists the equivalent `docker-machine` subcommand
   252  and what it does:
   253  
   254  |  `boot2docker` | `docker-machine` | `docker-machine` description                             |
   255  |----------------|------------------|----------------------------------------------------------|
   256  | init           | create           | Creates a new docker host.                               |
   257  | up             | start            | Starts a stopped machine.                                |
   258  | ssh            | ssh              | Runs a command or interactive ssh session on the machine.|
   259  | save           | -                | Not applicable.                                          |
   260  | down           | stop             | Stops a running machine.                                 |
   261  | poweroff       | stop             | Stops a running machine.                                 |
   262  | reset          | restart          | Restarts a running machine.                              |
   263  | config         | inspect          | Prints machine configuration details.                    |
   264  | status         | ls               | Lists all machines and their status.                     |
   265  | info           | inspect          | Displays a machine's details.                            |
   266  | ip             | ip               | Displays the machine's ip address.                       |
   267  | shellinit      | env              | Displays shell commands needed to configure your shell to interact with a machine |
   268  | delete         | rm               | Removes a machine.                                       |
   269  | download       | -                | Not applicable.                                          |
   270  | upgrade        | upgrade          | Upgrades a machine's Docker client to the latest stable release. |
   271  
   272  
   273  ## Example of Docker on Mac OS X
   274  
   275  Work through this section to try some practical container tasks on a VM. At this
   276  point, you should have a VM running and be connected to it through your shell.
   277  To verify this, run the following commands:
   278  
   279      $ docker-machine ls
   280      NAME                ACTIVE   DRIVER       STATE     URL                         SWARM
   281      default             *        virtualbox   Running   tcp://192.168.99.100:2376
   282  
   283  The `ACTIVE` machine, in this case `default`, is the one your environment is pointing to.
   284  
   285  ### Access container ports
   286  
   287  1. Start an NGINX container on the DOCKER_HOST.
   288  
   289          $ docker run -d -P --name web nginx
   290  
   291      Normally, the `docker run` commands starts a container, runs it, and then
   292      exits. The `-d` flag keeps the container running in the background
   293      after the `docker run` command completes. The `-P` flag publishes exposed ports from the
   294      container to your local host; this lets you access them from your Mac.
   295  
   296  2. Display your running container with `docker ps` command
   297  
   298          CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
   299          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
   300  
   301      At this point, you can see `nginx` is running as a daemon.
   302  
   303  3. View just the container's ports.
   304  
   305          $ docker port web
   306          443/tcp -> 0.0.0.0:49156
   307          80/tcp -> 0.0.0.0:49157
   308  
   309      This tells you that the `web` container's port `80` is mapped to port
   310      `49157` on your Docker host.
   311  
   312  4. Enter the `http://localhost:49157` address (`localhost` is `0.0.0.0`) in your browser:
   313  
   314      ![Bad Address](images/bad_host.png)
   315  
   316      This didn't work. The reason it doesn't work is your `DOCKER_HOST` address is
   317      not the localhost address (0.0.0.0) but is instead the address of the
   318      your Docker VM.
   319  
   320  5. Get the address of the `default` VM.
   321  
   322          $ docker-machine ip default
   323          192.168.59.103
   324  
   325  6. Enter the `http://192.168.59.103:49157` address in your browser:
   326  
   327      ![Correct Addressing](images/good_host.png)
   328  
   329      Success!
   330  
   331  7. To stop and then remove your running `nginx` container, do the following:
   332  
   333          $ docker stop web
   334          $ docker rm web
   335  
   336  ### Mount a volume on the container
   337  
   338  When you start a container it automatically shares your `/Users/username` directory
   339  with the VM. You can use this share point to mount directories onto your container.
   340  The next exercise demonstrates how to do this.
   341  
   342  1. Change to your user `$HOME` directory.
   343  
   344          $ cd $HOME
   345  
   346  2. Make a new `site` directory.
   347  
   348          $ mkdir site
   349  
   350  3. Change into the `site` directory.
   351  
   352          $ cd site
   353  
   354  4. Create a new `index.html` file.
   355  
   356          $ echo "my new site" > index.html
   357  
   358  5. Start a new `nginx` container and replace the `html` folder with your `site` directory.
   359  
   360          $ docker run -d -P -v $HOME/site:/usr/share/nginx/html \
   361            --name mysite nginx
   362  
   363  6. Get the `mysite` container's port.
   364  
   365          $ docker port mysite
   366          80/tcp -> 0.0.0.0:49166
   367          443/tcp -> 0.0.0.0:49165
   368  
   369  7. Open the site in a browser:
   370  
   371      ![My site page](images/newsite_view.png)
   372  
   373  8. Try adding a page to your `$HOME/site` in real time.
   374  
   375          $ echo "This is cool" > cool.html
   376  
   377  9. Open the new page in the browser.
   378  
   379      ![Cool page](images/cool_view.png)
   380  
   381  10. Stop and then remove your running `mysite` container.
   382  
   383          $ docker stop mysite
   384          $ docker rm mysite
   385  
   386  
   387  ## Upgrade Docker Toolbox
   388  
   389  To upgrade Docker Toolbox, download an re-run [the Docker Toolbox
   390  installer](https://docker.com/toolbox/).
   391  
   392  
   393  ## Uninstall Docker Toolbox
   394  
   395  To uninstall, do the following:
   396  
   397  1. List your machines.
   398  
   399          $ docker-machine ls
   400          NAME                ACTIVE   DRIVER       STATE     URL                         SWARM
   401          dev                 *        virtualbox   Running   tcp://192.168.99.100:2376
   402          my-docker-machine            virtualbox   Stopped
   403          default                      virtualbox   Stopped
   404  
   405  2. Remove each machine.
   406  
   407          $ docker-machine rm dev
   408          Successfully removed dev
   409  
   410       Removing a machine deletes its VM from VirtualBox and from the
   411       `~/.docker/machine/machines` directory.
   412  
   413  3. Remove the Docker Quickstart Terminal and Kitematic from your "Applications" folder.
   414  
   415  4. Remove the `docker`, `docker-compose`, and `docker-machine` commands from the `/usr/local/bin` folder.
   416  
   417          $ rm /usr/local/bin/docker
   418  
   419  5. Delete the `~/.docker` folder from your system.
   420  
   421  
   422  ## Learning more
   423  
   424  Use `docker-machine help` to list the full command line reference for Docker Machine. For more
   425  information about using SSH or SCP to access a VM, see [the Docker Machine
   426  documentation](https://docs.docker.com/machine/).
   427  
   428  You can continue with the [Docker User Guide](../userguide). If you are
   429  interested in using the Kitematic GUI, see the [Kitematic user
   430  guide](https://docs.docker.com/kitematic/userguide/).