05.26.18

Deploy Docker containers in Google Cloud Platform

TL;DR: Check out a working example project of how to deploy Docker containers (boxes) to Google Cloud Platform.

Delivering complex software solutions inside a neat container is exactly the promise of Docker, and Machine Box has truly benefited from this.

This article shows you how to spin up Machine Box in Google Cloud Platform, but the technique works for any Docker image you want to deploy.

You will need:

1. Create an account and sign in

Head over to the Google Cloud Platform Console and sign into it. You’ll need a Google account to agree to some terms and conditions etc.

2. Create a project

In the project dropdown, select Create new project. For first time users, you will likely be guided to do this with a wizard.

Creating a new project in Google Cloud Platform Console

Choose a project name and setup a billing account before clicking Create.

TIP: Keep a note of the project ID which might differ from the project name.

While your project is being setup, you can get the project ready for deployment.

3. Your project files

Create a folder on your development machine, ideally matching the name of the project you just created.

TIP: These files are expected to go into your source code repository, but follow best practices about storing secrets in there.

Create two text files called app.yaml and Dockerfile.

Dockerfile

The Dockerfile describes which image you want to add. In the simplest cast, just specify which image you want to generate:

FROM machinebox/facebox

In the case of Facebox, we also need to include the MB_KEY environment variable, which we can do in this file:

FROM machinebox/facebox
ENV MB_KEY=YOUR_KEY_HERE

TIP: This is just a normal Dockerfile, so you can do whatever you need to here.

app.yaml

The app.yaml file is where we will configure the deployment.

runtime: custom
env: flex
service: default
threadsafe: yes
network:
forwarded_ports:
- 80:8080
automatic_scaling:
  min_num_instances: 1
  max_num_instances: 10
  cool_down_period_sec: 120 # default value
  cpu_utilization:
  target_utilization: 0.5
resources:
  cpu: 1
  memory_gb: 2
  disk_size_gb: 10
# volumes:
# - name: ramdisk1
#   volume_type: tmpfs
#   size_gb: 0.5

This file describes an autoscaling image (between 1 and 10 instances at a time) with a single CPU, 2GB of RAM and a 10GB disk, forwarding the Machine Box (container) port 8080 to 80, so it’s accessible on the web.

TIP: You can dive a bit deeper on what you can do with the app.yaml file by checking out the official documentation.

4. Deploy

Deploy your first version with the following command line in the terminal:

gcloud app deploy app.yaml -v v1

After a while, the image will be deployed and available.

5. Access your container

Head over to https://your-project-id.appspot.com/ to access the image.

In our case, we see that the Facebox console is available:

Facebox running in Google Cloud Platform