05.23.18

Deploy Docker Containers Into Digital Ocean

Machine Box delivers the services via Docker containers, and Digital Ocean is a great place to host these containers in production.

This guide will walk you through the steps required to deploy Facebox with Digital Ocean.

TIP: Before you start, it’s important to remember that hosting on Digital Ocean comes with a small cost — it’s very competitive, but it isn’t free.

1. Create a digital ocean account

Head over to https://www.digitalocean.com/ and create an account.

2. Generate a personal access token

In order for us to access Digital Ocean’s services via the API, we are going to need a Personal access token.

  • Click the API navigation
  • In the Personal Access Tokens section, click Generate new token
You can do a lot via the Digital Ocean web UI: This is how you create Personal access tokens.
  • Give your token a name, and click the Generate Token button

Once it’s created, copy it and create an environment variable

3. Create a droplet

In Digital Ocean, the machines are called droplets. And we are going to need to create one so that we can run our Machine Box docker images.

We will use the docker-machine command, which should already be installed on your development machine if you have been working with Docker.

TIP: Type docker-machine into a terminal, if you get an error you may need to install it yourself.

Type this single line into a terminal remembering to replace the PERSONAL_ACCESS_TOKEN string with the Personal access token you created in Step 2:

docker-machine create --digitalocean-size "s-2vcpu-4gb" --driver digitalocean --digitalocean-access-token PERSONAL_ACCESS_TOKEN facebox-prod-1
  • –digitalocean-size “s-2vcpu-4gb” creates an image with 2 CPUs and 4 GB of RAM — you can use any size you feel is appropriate
  • facebox-prod-1 is the name of our droplet

All being well, this will create a droplet. You can verify this by typing:

docker-machine ls

You should see output something similar to:

facebox-prod-1 * digitalocean Running tcp://152.63.254.5:2376 v18.05.0-ce

4. Connect to the new droplet

To connect to the droplet, run the following command:

eval $(docker-machine env facebox-prod-1)

This is actually just setting some environment variables that the docker-machine and docker commands will use.

TIP: You can run just docker-machine env facebox-prod-1 to see what the values are.

5. Start a Machine Box service

Now we have our droplet running and we are connected to it, it is time to run one of the Machine Box boxes.

In this example, I am going to run Facebox — but you can run any of the other boxes.

docker run -d -p 80:8080 -e "MB_WORKERS=2" -e "MB_KEY=$MB_KEY" machinebox/facebox

This is slightly different to how you might run Facebox in development for a number of reasons:

  • We are using the -d flag to tell Docker to run this as a daemon (background task) — you can omit this while you’re setting things up and your terminal will stay connected like during development
  • We are mapping the box port 8080 (where the services run) to port 80 — this is what makes it accessible on the web without specifying a port number
  • We are using the -e flag to set the MB_WORKERS environment variable to 2 — to match the number of CPUs we have

WATCH OUT: If you see an error complaining about the MB_KEY, ensure you have assigned the environment variable with the value from the Machine Box website.

6. Get the machine’s IP address

In the same terminal, run the following command to get the machine’s public IP address:

docker-machine ip facebox-prod-1
152.65.253.2

In a web browser, head over to that IP address to confirm that Facebox is indeed running:

After a few steps, Facebox is running in the cloud.

Now you can update your client code to use the new endpoint instead of localhost.

That’s all folks

We have successfully put Facebox into production in Digital Ocean.

Specifically we:

  1. Created a Digital Ocean account and obtained a Personal access token
  2. Created a droplet to host our box
  3. Spun up an instance of Facebox
  4. Made the box publicly accessible

Turning the box off

The easiest way to turn a box off is to sign into the Digital Ocean console and access the Droplets section, from there you can Destroy it using the UI.

What next?

Once you are up and running, you might want to think about the following:

  • For scale and redundancy, consider using a load balancer to split traffic over many instances of Facebox
  • Secure the box using a firewall
  • You’ll need to do a little more work to support SSL/HTTPS — probably with an nginx image that proxies the traffic to the appropriate endpoints

 

Deploy Docker containers into Digital Ocean was originally published in Machine Box on Medium, where people are continuing the conversation by highlighting and responding to this story.