github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/docs/installation/cloud/cloud-ex-machine-ocean.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Example: Use Docker Machine to provision cloud hosts"
     4  description = "Example of using Docker Machine to install Docker Engine on a cloud provider, using Digital Ocean."
     5  keywords = ["cloud, docker, machine, documentation,  installation, digitalocean"]
     6  [menu.main]
     7  parent = "install_cloud"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Example: Use Docker Machine to provision cloud hosts
    12  
    13  Docker Machine driver plugins are available for many cloud platforms, so you can use Machine to provision cloud hosts. When you use Docker Machine for provisioning, you create cloud hosts with Docker Engine installed on them.
    14  
    15  You'll need to install and run Docker Machine, and create an account with the cloud provider.
    16  
    17  Then you provide account verification, security credentials, and configuration options for the providers as flags to `docker-machine create`. The flags are unique for each cloud-specific driver.  For instance, to pass a Digital Ocean access token, you use the `--digitalocean-access-token` flag.
    18  
    19  As an example, let's take a look at how to create a Dockerized <a href="https://digitalocean.com" target="_blank">Digital Ocean</a> _Droplet_ (cloud server).
    20  
    21  ### Step 1. Create a Digital Ocean account and log in
    22  
    23  If you have not done so already, go to <a href="https://digitalocean.com" target="_blank">Digital Ocean</a>, create an account, and log in.
    24  
    25  ### Step 2. Generate a personal access token
    26  
    27  To generate your access token:
    28  
    29    1. Go to the Digital Ocean administrator console and click **API** in the header.
    30  
    31      ![Click API in Digital Ocean console](../images/ocean_click_api.png)
    32  
    33    2. Click **Generate New Token** to get to the token generator.
    34  
    35      ![Generate token](../images/ocean_gen_token.png)
    36  
    37    3. Give the token a clever name (e.g. "machine"), make sure the **Write (Optional)** checkbox is checked, and click **Generate Token**.
    38  
    39      ![Name and generate token](../images/ocean_token_create.png)
    40  
    41    4. Grab (copy to clipboard) the generated big long hex string and store it somewhere safe.
    42  
    43      ![Copy and save personal access token](../images/ocean_save_token.png)
    44  
    45      This is the personal access token you'll use in the next step to create your cloud server.
    46  
    47  ### Step 3. Install Docker Machine
    48  
    49  1. If you have not done so already, install Docker Machine on your local host.
    50  
    51    * <a href="https://docs.docker.com/engine/installation/mac/" target="_blank"> How to install Docker Machine on Mac OS X</a>
    52  
    53    * <a href="https://docs.docker.com/engine/installation/windows/" target="_blank">How to install Docker Machine on Windows</a>
    54  
    55    * <a href="https://docs.docker.com/machine/install-machine/" target="_blank">Install Docker Machine directly</a> (e.g., on Linux)
    56  
    57  2. At a command terminal, use `docker-machine ls` to get a list of Docker Machines and their status.
    58  
    59          $ docker-machine ls
    60          NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
    61          default   *        virtualbox   Running   tcp:////xxx.xxx.xx.xxx:xxxx  
    62  
    63  6. Run some Docker commands to make sure that Docker Engine is also up-and-running.
    64  
    65      We'll run `docker run hello-world` again, but you could try `docker ps`,  `docker run docker/whalesay cowsay boo`, or another command to verify that Docker is running.
    66  
    67          $ docker run hello-world
    68  
    69          Hello from Docker.
    70          This message shows that your installation appears to be working correctly.
    71          ...
    72  
    73  ### Step 4. Use Machine to Create the Droplet
    74  
    75  1. Run `docker-machine create` with the `digitalocean` driver and pass your key to the `--digitalocean-access-token` flag, along with a name for the new cloud server.
    76  
    77      For this example, we'll call our new Droplet "docker-sandbox".
    78  
    79          $ docker-machine create --driver digitalocean --digitalocean-access-token xxxxx docker-sandbox
    80          Running pre-create checks...
    81          Creating machine...
    82          (docker-sandbox) OUT | Creating SSH key...
    83          (docker-sandbox) OUT | Creating Digital Ocean droplet...
    84          (docker-sandbox) OUT | Waiting for IP address to be assigned to the Droplet...
    85          Waiting for machine to be running, this may take a few minutes...
    86          Machine is running, waiting for SSH to be available...
    87          Detecting operating system of created instance...
    88          Detecting the provisioner...
    89          Provisioning created instance...
    90          Copying certs to the local machine directory...
    91          Copying certs to the remote machine...
    92          Setting Docker configuration on the remote daemon...
    93          To see how to connect Docker to this machine, run: docker-machine env docker-sandbox
    94  
    95        When the Droplet is created, Docker generates a unique SSH key and stores it on your local system in `~/.docker/machines`. Initially, this is used to provision the host. Later, it's used under the hood to access the Droplet directly with the `docker-machine ssh` command. Docker Engine is installed on the cloud server and the daemon is configured to accept remote connections over TCP using TLS for authentication.
    96  
    97  2. Go to the Digital Ocean console to view the new Droplet.
    98  
    99      ![Droplet in Digital Ocean created with Machine](../images/ocean_droplet.png)
   100  
   101  3. At the command terminal, run `docker-machine ls`.
   102  
   103          $ docker-machine ls
   104          NAME             ACTIVE   DRIVER         STATE     URL                         SWARM
   105          default          *        virtualbox     Running   tcp://192.168.99.100:2376   
   106          docker-sandbox   -        digitalocean   Running   tcp://45.55.139.48:2376     
   107  
   108      Notice that the new cloud server is running but is not the active host. Our command shell is still connected to the default machine, which is currently the active host as indicated by the asterisk (*).
   109  
   110  4. Run `docker-machine env docker-sandbox` to get the environment commands for the new remote host, then run `eval` as directed to re-configure the shell to connect to `docker-sandbox`.
   111  
   112          $ docker-machine env docker-sandbox
   113          export DOCKER_TLS_VERIFY="1"
   114          export DOCKER_HOST="tcp://45.55.222.72:2376"
   115          export DOCKER_CERT_PATH="/Users/victoriabialas/.docker/machine/machines/docker-sandbox"
   116          export DOCKER_MACHINE_NAME="docker-sandbox"
   117          # Run this command to configure your shell:
   118          # eval "$(docker-machine env docker-sandbox)"
   119  
   120          $ eval "$(docker-machine env docker-sandbox)"
   121  
   122  5. Re-run `docker-machine ls` to verify that our new server is the active machine, as indicated by the asterisk (*) in the ACTIVE column.
   123  
   124          $ docker-machine ls
   125          NAME             ACTIVE   DRIVER         STATE     URL                         SWARM
   126          default          -        virtualbox     Running   tcp://192.168.99.100:2376   
   127          docker-sandbox   *        digitalocean   Running   tcp://45.55.222.72:2376     
   128  
   129  6. Run some `docker-machine` commands to inspect the remote host. For example, `docker-machine ip <machine>` gets the host IP adddress and `docker-machine inspect <machine>` lists all the details.
   130  
   131          $ docker-machine ip docker-sandbox
   132          104.131.43.236
   133  
   134          $ docker-machine inspect docker-sandbox
   135          {
   136              "ConfigVersion": 3,
   137              "Driver": {
   138              "IPAddress": "104.131.43.236",
   139              "MachineName": "docker-sandbox",
   140              "SSHUser": "root",
   141              "SSHPort": 22,
   142              "SSHKeyPath": "/Users/samanthastevens/.docker/machine/machines/docker-sandbox/id_rsa",
   143              "StorePath": "/Users/samanthastevens/.docker/machine",
   144              "SwarmMaster": false,
   145              "SwarmHost": "tcp://0.0.0.0:3376",
   146              "SwarmDiscovery": "",
   147              ...
   148  
   149  7. Verify Docker Engine is installed correctly by running `docker` commands.
   150  
   151      Start with something basic like `docker run hello-world`, or for a more interesting test, run a Dockerized webserver on your new remote machine.
   152  
   153      In this example, the `-p` option is used to expose port 80 from the `nginx` container and make it accessible on port `8000` of the `docker-sandbox` host.
   154  
   155          $ docker run -d -p 8000:80 --name webserver kitematic/hello-world-nginx
   156          Unable to find image 'kitematic/hello-world-nginx:latest' locally
   157          latest: Pulling from kitematic/hello-world-nginx
   158          a285d7f063ea: Pull complete
   159          2d7baf27389b: Pull complete
   160          ...
   161          Digest: sha256:ec0ca6dcb034916784c988b4f2432716e2e92b995ac606e080c7a54b52b87066
   162          Status: Downloaded newer image for kitematic/hello-world-nginx:latest
   163          942dfb4a0eaae75bf26c9785ade4ff47ceb2ec2a152be82b9d7960e8b5777e65
   164  
   165      In a web browser, go to `http://<host_ip>:8000` to bring up the webserver home page. You got the `<host_ip>` from the output of the `docker-machine ip <machine>` command you ran in a previous step. Use the port you exposed in the `docker run` command.
   166  
   167      ![nginx webserver](../images/nginx-webserver.png)
   168  
   169  #### Understand the defaults and options on the create command
   170  
   171  For convenience, `docker-machine` will use sensible defaults for choosing settings such as the image that the server is based on, but you override the defaults using the respective flags (e.g. `--digitalocean-image`). This is useful if, for example, you want to create a cloud server with a lot of memory and CPUs (by default `docker-machine` creates a small server). For a full list of the flags/settings available and their defaults, see the output of `docker-machine create -h` at the command line. See also <a href="https://docs.docker.com/machine/drivers/os-base/" target="_blank">Driver options and operating system defaults</a> and information about the <a href="https://docs.docker.com/machine/reference/create/" target="_blank">create</a> command in the Docker Machine documentation.
   172  
   173  
   174  ### Step 5. Use Machine to remove the Droplet
   175  
   176  To remove a host and all of its containers and images, first stop the machine, then use `docker-machine rm`:
   177  
   178      $ docker-machine stop docker-sandbox
   179      $ docker-machine rm docker-sandbox
   180      Do you really want to remove "docker-sandbox"? (y/n): y
   181      Successfully removed docker-sandbox
   182  
   183      $ docker-machine ls
   184      NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
   185      default   *        virtualbox   Running   tcp:////xxx.xxx.xx.xxx:xxxx
   186  
   187  If you monitor the Digital Ocean console while you run these commands, you will see it update first to reflect that the Droplet was stopped, and then removed.
   188  
   189  If you create a host with Docker Machine, but remove it through the cloud provider console, Machine will lose track of the server status. So please use the `docker-machine rm` command for hosts you create with `docker-machine --create`.
   190  
   191  ## Where to go next
   192  
   193  * [Docker Machine driver reference](https://docs.docker.com/machine/drivers/)
   194  
   195  * [Docker Machine Overview](https://docs.docker.com/machine/overview/)
   196  
   197  * [Use Docker Machine to provision hosts on cloud providers](https://docs.docker.com/machine/get-started-cloud/)
   198  
   199  *  [Install Docker Engine](../../installation/index.md)
   200  
   201  * [Docker User Guide](../../userguide/intro.md)