|
| 1 | +<!--[metadata]> |
| 2 | ++++ |
| 3 | +title = "Provision Digital Ocean Droplets" |
| 4 | +description = "Using Docker Machine to provision hosts on Digital Ocean" |
| 5 | +keywords = ["docker, machine, cloud, digital ocean"] |
| 6 | +[menu.main] |
| 7 | +parent="cloud_examples" |
| 8 | +weight=1 |
| 9 | ++++ |
| 10 | +<![end-metadata]--> |
| 11 | + |
| 12 | +# Digital Ocean example |
| 13 | + |
| 14 | +Follow along with this example to create a Dockerized <a href="https://digitalocean.com" target="_blank">Digital Ocean</a> Droplet (cloud host). |
| 15 | + |
| 16 | +### Step 1. Create a Digital Ocean account |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +### Step 2. Generate a personal access token |
| 21 | + |
| 22 | +To generate your access token: |
| 23 | + |
| 24 | + 1. Go to the Digital Ocean administrator console and click **API** in the header. |
| 25 | + |
| 26 | +  |
| 27 | + |
| 28 | + 2. Click **Generate New Token** to get to the token generator. |
| 29 | + |
| 30 | +  |
| 31 | + |
| 32 | + 3. Give the token a clever name (e.g. "machine"), make sure the **Write (Optional)** checkbox is checked, and click **Generate Token**. |
| 33 | + |
| 34 | +  |
| 35 | + |
| 36 | + 4. Grab (copy to clipboard) the generated big long hex string and store it somewhere safe. |
| 37 | + |
| 38 | +  |
| 39 | + |
| 40 | + This is the personal access token you'll use in the next step to create your cloud server. |
| 41 | + |
| 42 | +### Step 3. Use Machine to create the Droplet |
| 43 | + |
| 44 | +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. |
| 45 | + |
| 46 | + For this example, we'll call our new Droplet "docker-sandbox". |
| 47 | + |
| 48 | + $ docker-machine create --driver digitalocean --digitalocean-access-token xxxxx docker-sandbox |
| 49 | + Running pre-create checks... |
| 50 | + Creating machine... |
| 51 | + (docker-sandbox) OUT | Creating SSH key... |
| 52 | + (docker-sandbox) OUT | Creating Digital Ocean droplet... |
| 53 | + (docker-sandbox) OUT | Waiting for IP address to be assigned to the Droplet... |
| 54 | + Waiting for machine to be running, this may take a few minutes... |
| 55 | + Machine is running, waiting for SSH to be available... |
| 56 | + Detecting operating system of created instance... |
| 57 | + Detecting the provisioner... |
| 58 | + Provisioning created instance... |
| 59 | + Copying certs to the local machine directory... |
| 60 | + Copying certs to the remote machine... |
| 61 | + Setting Docker configuration on the remote daemon... |
| 62 | + To see how to connect Docker to this machine, run: docker-machine env docker-sandbox |
| 63 | + |
| 64 | + 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. |
| 65 | + |
| 66 | +2. Go to the Digital Ocean console to view the new Droplet. |
| 67 | + |
| 68 | +  |
| 69 | + |
| 70 | +3. At the command terminal, run `docker-machine ls`. |
| 71 | + |
| 72 | + $ docker-machine ls |
| 73 | + NAME ACTIVE DRIVER STATE URL SWARM |
| 74 | + default - virtualbox Running tcp://192.168.99.100:2376 |
| 75 | + docker-sandbox * digitalocean Running tcp://45.55.139.48:2376 |
| 76 | + |
| 77 | + The new `docker-sandbox` machine is running, and it is the active host as indicated by the asterisk (*). When you create a new machine, your command shell automatically connects it. If for some reason your new machine is not the active host, you'll need to run `docker-machine env aws-sandbox`, followed by `eval $(docker-machine env docker-sandbox)` to connect to it. |
| 78 | + |
| 79 | +### Step 4. Run Docker commands on the Droplet |
| 80 | + |
| 81 | +1. 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. |
| 82 | + |
| 83 | + $ docker-machine ip docker-sandbox |
| 84 | + 104.131.43.236 |
| 85 | + |
| 86 | + $ docker-machine inspect docker-sandbox |
| 87 | + { |
| 88 | + "ConfigVersion": 3, |
| 89 | + "Driver": { |
| 90 | + "IPAddress": "104.131.43.236", |
| 91 | + "MachineName": "docker-sandbox", |
| 92 | + "SSHUser": "root", |
| 93 | + "SSHPort": 22, |
| 94 | + "SSHKeyPath": "/Users/samanthastevens/.docker/machine/machines/docker-sandbox/id_rsa", |
| 95 | + "StorePath": "/Users/samanthastevens/.docker/machine", |
| 96 | + "SwarmMaster": false, |
| 97 | + "SwarmHost": "tcp://0.0.0.0:3376", |
| 98 | + "SwarmDiscovery": "", |
| 99 | + ... |
| 100 | + |
| 101 | +2. Verify Docker Engine is installed correctly by running `docker` commands. |
| 102 | + |
| 103 | + Start with something basic like `docker run hello-world`, or for a more interesting test, run a Dockerized webserver on your new remote machine. |
| 104 | + |
| 105 | + 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. |
| 106 | + |
| 107 | + $ docker run -d -p 8000:80 --name webserver kitematic/hello-world-nginx |
| 108 | + Unable to find image 'kitematic/hello-world-nginx:latest' locally |
| 109 | + latest: Pulling from kitematic/hello-world-nginx |
| 110 | + a285d7f063ea: Pull complete |
| 111 | + 2d7baf27389b: Pull complete |
| 112 | + ... |
| 113 | + Digest: sha256:ec0ca6dcb034916784c988b4f2432716e2e92b995ac606e080c7a54b52b87066 |
| 114 | + Status: Downloaded newer image for kitematic/hello-world-nginx:latest |
| 115 | + 942dfb4a0eaae75bf26c9785ade4ff47ceb2ec2a152be82b9d7960e8b5777e65 |
| 116 | + |
| 117 | + 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. |
| 118 | + |
| 119 | +  |
| 120 | + |
| 121 | +### Step 5. Use Machine to remove the Droplet |
| 122 | + |
| 123 | +To remove a host and all of its containers and images, first stop the machine, then use `docker-machine rm`: |
| 124 | + |
| 125 | + $ docker-machine stop docker-sandbox |
| 126 | + $ docker-machine rm docker-sandbox |
| 127 | + Do you really want to remove "docker-sandbox"? (y/n): y |
| 128 | + Successfully removed docker-sandbox |
| 129 | + |
| 130 | + $ docker-machine ls |
| 131 | + NAME ACTIVE DRIVER STATE URL SWARM |
| 132 | + default * virtualbox Running tcp:////xxx.xxx.xx.xxx:xxxx |
| 133 | + |
| 134 | +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. |
| 135 | + |
| 136 | +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`. |
| 137 | + |
| 138 | +## Where to go next |
| 139 | + |
| 140 | +- [Understand Machine concepts](../concepts.md) |
| 141 | +- [Docker Machine driver reference](../drivers/index.md) |
| 142 | +- [Docker Machine subcommand reference](../reference/index.md) |
| 143 | +- [Provision a Docker Swarm cluster with Docker Machine](https://docs.docker.com/swarm/provision-with-machine/) |
0 commit comments