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